您的位置:首页 > 移动开发 > IOS开发

IOS 程序内调用本地打电话功能-make a phone call

2013-03-29 14:01 357 查看
利用openURL接口来调用打电话功能。代码还具备根据正则表达式判断电话好吗输入的合法性。

- (void)makeCall:(NSString *)number
{
NSString *txt = number;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[0-9]{4}[-]{0,1}[0-9]{4}?" options:NSRegularExpressionSearch error:nil];
NSTextCheckingResult *result = [regex firstMatchInString:txt options:0 range:NSMakeRange(0, [txt length])];
NSString *cleanedString = [[[txt substringWithRange:[result range]] componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789-+()"] invertedSet]] componentsJoinedByString:@""];
NSString *escapedPhoneNumber = [cleanedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *telURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", escapedPhoneNumber]];
[[UIApplication sharedApplication] openURL:telURL];
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: