iOS调用打电话功能

使用openURL接口来调用打电话功能。代码还保有根据正则表达式判断电话好吧输入的合法性。

NSString* str =@”tel://1xxxxxxxxxx”;

- (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

UIWebView*callWebView = [[UIWebView  alloc]init];

[callWebView  loadRequest:[NSURLRequest  requestWithURL:[NSURL
 URLWithString:str]]];

[self.view  addSubview:callWebView];

发表评论

电子邮件地址不会被公开。 必填项已用*标注

网站地图xml地图