在iOS应用程序中拨打电话

Cod*_*Guy 33 objective-c phone-call ios

我有一些代码尝试在应用程序中进行调用,但它似乎没有工作:

    UIApplication *myApp = [UIApplication sharedApplication];
    NSString *theCall = [NSString stringWithFormat:@"tel://%@",phone];
    NSLog(@"making call with %@",theCall);
    [myApp openURL:[NSURL URLWithString:theCall]];
Run Code Online (Sandbox Code Playgroud)

有时,变量phone就是这样的@"(102) 222-2222".如何使用这样的电话号码拨打电话?我是否需要手动从中提取数字并摆脱所有额外的标点符号?

Joh*_*ool 80

对.你需要自己解决这些问题.或者您可以使用下面的代码段...

NSString *cleanedString = [[phoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789-+()"] invertedSet]] componentsJoinedByString:@""];
NSURL *telURL = [NSURL URLWithString:[NSString stringWithFormat:@"tel:%@", cleanedString]];
Run Code Online (Sandbox Code Playgroud)

注意:你可能很想使用-stringByTrimmingCharactersInSet:,但是只能删除字符串开头和结尾的字符,而不是它们出现在中间.


D-e*_*per 23

要返回原始应用程序,您可以使用telprompt://而不是tel:// - tell提示符将首先提示用户,但是当调用完成后,它将返回到您的应用程序:

NSString *phoneNumber = [@"telprompt://" stringByAppendingString:mymobileNO.titleLabel.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
Run Code Online (Sandbox Code Playgroud)

只是对上述答案的更新.