在iOS应用程序中拨打电话

| 我有一些代码试图在应用程序中进行调用,但似乎不起作用:
    UIApplication *myApp = [UIApplication sharedApplication];
    NSString *theCall = [NSString stringWithFormat:@\"tel://%@\",phone];
    NSLog(@\"making call with %@\",theCall);
    [myApp openURL:[NSURL URLWithString:theCall]];
有时,变量“ 1”是诸如“ 2”之类的东西。如何使用这样的电话号码拨打电话?我是否需要手动从中提取数字并消除所有多余的标点符号?     
已邀请:
        对。您需要自己将它们取出。或者您可以使用下面的代码段...
NSString *cleanedString = [[phoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@\"0123456789-+()\"] invertedSet]] componentsJoinedByString:@\"\"];
NSURL *telURL = [NSURL URLWithString:[NSString stringWithFormat:@\"tel:%@\", cleanedString]];
注意:您可能会想使用
-stringByTrimmingCharactersInSet:
,但是只能删除字符串开头和结尾的字符,而不是出现在中间的字符。     
        要返回原始应用,您可以使用telprompt://而不是tel://-提示提示符将首先提示用户,但是通话结束后,它将返回您的应用:
NSString *phoneNumber = [@\"telprompt://\" stringByAppendingString:mymobileNO.titleLabel.text];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:phoneNumber]];
只是上述答案的更新。     
        这是一个简单的方法,可用于拨打电话并在通话结束后返回应用程序。 将以下内容添加到您的.m文件中
- (void) dialNumber:(NSString*) number{
number = [@\"telprompt://\" stringByAppendingString:number];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:number]];
}
然后,在您要从哪里拨打电话的任何地方添加以下代码:
[self dialNumber:@\"5031234567\"];
    

要回复问题请先登录注册