开发中我们调用系统的打电话方法,一般都是用数字电话号去调用,如果遇到号码中包含特殊字符时直接调用拨打电话是行不通的
实验发现就算转换UTF8编码也是不可拨打
可用下面的三种方法对号码字符串做处理
第一种 :
// 特殊字符处理
NSString *commandStr = @"*#21#";
NSString *str1 = [commandStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
第二种 :
NSString *commandStr = @"*#21#";
NSString *str2 = CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)commandStr, (CFStringRef)@"!$&'()*+,-./:;=?@_~%#[]", NULL, kCFStringEncodingUTF8));
第三种 :
NSString *commandStr = @"*#21#";
NSString *reportName = [commandStr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet characterSetWithCharactersInString:@"?!@#$^&%*+,:;='\"`<>()[]{}/\\| "].invertedSet];
再拨打就欧了
NSMutableString *str = [[NSMutableString alloc] initWithFormat:@"telprompt://%@",str1];
NSURL *callUrl = [NSURL URLWithString:str];
if (@available(iOS 10.0, *)) {
[[UIApplication sharedApplication] openURL:callUrl options:@{} completionHandler:nil];
} else {
// Fallback on earlier versions
if([[UIApplication sharedApplication] canOpenURL:callUrl]){
[[UIApplication sharedApplication] openURL:callUrl];
}
}
要是给您解决问题了,来给俺点个赞👍🏻