Skip to content

Instantly share code, notes, and snippets.

@hhyyy9
Created September 29, 2012 01:29
Show Gist options
  • Save hhyyy9/3802855 to your computer and use it in GitHub Desktop.
Save hhyyy9/3802855 to your computer and use it in GitHub Desktop.
程序内调用本地打电话功能-make a phone call
- (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];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment