无法以编程方式打开手机应用程序

时间:2012-07-18 12:32:00

标签: iphone ios

 NSString *phoneStr = @"tel:";
 phoneStr = [phoneStr stringByAppendingFormat:@"%@" , self.ContactPhone];
 NSURL * phoneURL = [NSURL URLWithString:phoneStr];
 [[UIApplication sharedApplication] openURL:phoneURL]; 

此代码无效,无法打开手机应用程序,当我打印phoneURL的值时,它始终为null

为什么会这样?

THX

4 个答案:

答案 0 :(得分:3)

您需要转义电话号码:

[NSURL URLWithString:[phoneStr stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]

答案 1 :(得分:1)

您在 tel:

之后错过了 //
NSString *phoneStr = @"tel://";
 phoneStr = [phoneStr stringByAppendingFormat:@"%@" , self.ContactPhone];
 NSURL * phoneURL = [NSURL URLWithString:phoneStr];
 [[UIApplication sharedApplication] openURL:phoneURL];

答案 2 :(得分:0)

首先检查self.ContactPhone是否为空并使用 @“telprompt://” 在结束通话后返回或重新启动您的应用。< / p>

NSString *phoneStr = @"telprompt://";
if([self.ContactPhone length]>0)
{
phoneStr = [phoneStr stringByAppendingFormat:@"%@" , self.ContactPhone];
NSURL * phoneURL = [NSURL URLWithString:phoneStr];
[[UIApplication sharedApplication] openURL:phoneURL];
}

答案 3 :(得分:0)

这里是 Swift 4.1

的代码
if let url = URL(string: "tel://" + phoneString), UIApplication.shared.canOpenURL(url) {
    UIApplication.shared.open(url, options: [:], completionHandler: nil)
}