返回顶部

收藏

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

更多

利用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];
}
//该片段来自于http://outofmemory.cn

标签:objective-c,手机开发

收藏

0人收藏

支持

0

反对

0

发表评论