IOS 程序内调用本地打电话功能-make a phone call
利用openURL接口来调用打电话功能。代码还具备根据正则表达式判断电话好吗输入的合法性。
1 - (void)makeCall:(NSString *)number 2 { 3 NSString *txt = number; 4 NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[0-9]{4}[-]{0,1}[0-9]{4}?" options:NSRegularExpressionSearch error:nil]; 5 NSTextCheckingResult *result = [regex firstMatchInString:txt options:0 range:NSMakeRange(0, [txt length])]; 6 NSString *cleanedString = [[[txt substringWithRange:[result range]] componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789-+()"] invertedSet]] componentsJoinedByString:@""]; 7 NSString *escapedPhoneNumber = [cleanedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 8 NSURL *telURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", escapedPhoneNumber]]; 9 [[UIApplication sharedApplication] openURL:telURL]; 10 }