[英]iOS: phone number dialing not working
我正在构建一个应用程序,让您可以拨打荷兰的不同服务中心。 问题是它们中的一些具有不同的商业格式(如0800-xxxx),并且设备无法拨打电话。 代码如下所示:
if ([[UIApplication sharedApplication] canOpenURL:phoneURL]) {
[[UIApplication sharedApplication] openURL:phoneURL];
}
您知道如何格式化号码或拨打电话,无论格式如何?
编辑:这是phoneNumber的创建方式:
NSString *phoneNumberString = phoneNumber; // dynamically assigned
NSString *phoneURLString = [NSString stringWithFormat:@"telprompt:%@", phoneNumberString];
NSURL *phoneURL = [NSURL URLWithString:phoneURLString];
我使用了这段代码并且它有效:
NSString *cleanedString = [[phoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789-+()"] invertedSet]] componentsJoinedByString:@""];
NSString *escapedPhoneNumber = [cleanedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *phoneURLString = [NSString stringWithFormat:@"telprompt:%@", escapedPhoneNumber];
NSURL *phoneURL = [NSURL URLWithString:phoneURLString];
if ([[UIApplication sharedApplication] canOpenURL:phoneURL]) {
[[UIApplication sharedApplication] openURL:phoneURL];
}
NSString *strm = (NSString *) CFURLCreateStringByAddingPercentEscapes(kCFAllocatorDefault, (CFStringRef)mob, NULL, CFSTR(":/?#[]@!$&’()*+,;="), kCFStringEncodingUTF8);
NSString *strMob = [[NSString alloc] initWithFormat:@"tel://%@",strm];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:strMob]];
试试这段代码。 这适用于任何类型的格式。它将转换为完美的调用URL。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.