canOpenURL无法使用系统范围的URLscheme

我正在运行iOS 9b5。

在我的应用程序中,如果设备可以拨打电话,我想将文本颜色设置为蓝色,以使其看起来可以点击。 如果不是,我把它留下黑色。

为了确定设备function,我使用:

[[UIApplcation sharedApplication] canOpenURL:@"telprompt://5555555555"] 

众所周知,iOS 9要求将我们将在我们的应用中使用的任何URLscheme列为白名单,作为隐私措施。

我在Info.plist中有这个:

 <key>LSApplicationQueriesSchemes</key> <array> <string>telprompt</string> </array> 

无论我做什么,我仍然canOpenURL:URL失败:“telprompt:/ /” – 错误:“(空)”。 我试过电话:/和短信:/ /我似乎无法避免该系统日志警告。

有谁知道一种方法来检测设备是否可以拨打电话,而不会触发这些警告?

我到目前为止发现的是,如果控制台日志-canOpenURL: failed for URL: "xxx://" - error: "(null)" ,它实际上工作。 一旦有任何其他错误比null ,它可能无法正常工作。 如果错误是"This app is not allowed to query for scheme xxx" ,那么您必须将此计划添加到您的应用程序的.plist中:

 <key>LSApplicationQueriesSchemes</key> <array> <string>xxx</string> </array> 

奇怪的行为,控制台输出看起来像一个错误,虽然没有,确实。

我想你可能需要在实际的设备上试试这个,或者再试一次。 我刚刚在iPhone 5上工作,看起来你甚至不需要把它添加到LSApplicationQueriesSchemes。 如果应用程序是使用Xcode 7 Beta 6构build的,并且像下面这样使用canOpenURL或openURL,那么它似乎可以在设备上正常工作。

[[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"tel:555-555-5555"]]

[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"tel:555-555-5555"]]

在iOS SIM上我仍然得到错误:
LaunchServices:错误:URL计划没有注册处理程序电话
-canOpenURL:URL失败:“tel:555-555-5555” – 错误:“此应用程序不允许查询计划tel”

我在IOS9设备中遇到了同样的错误。 所以我使用下面的代码片段来避免这个错误。

 NSString *cleanedString = [[[PHONE NUMBER] componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789-+()"] invertedSet]] componentsJoinedByString:@""]; NSString *escapedPhoneNumber = [cleanedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSString *phoneURLString = [NSString stringWithFormat:@"telprompt:%@", escapedPhoneNumber]; NSURL *phoneURL = [NSURL URLWithString:phoneURLString]; if ([[UIApplication sharedApplication] canOpenURL:phoneURL]) { [[UIApplication sharedApplication] openURL:phoneURL]; } 

由于iOS9弃用了stringByAddingPercentEscapesUsingEncoding,因此可以使用以下命令清除telprompt:URL。

 NSString *cleanedString = [[[PHONE NUMBER] componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789-+()"] invertedSet]] componentsJoinedByString:@""]; //NSString *escapedPhoneNumber = [cleanedString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSString *escapedPhoneNumber = [cleanedString stringByAddingPercentEncodingWithAllowedCharacters: [NSCharacterSet URLQueryAllowedCharacterSet]]; NSString *phoneURLString = [NSString stringWithFormat:@"telprompt:%@", escapedPhoneNumber]; NSURL *phoneURL = [NSURL URLWithString:phoneURLString]; if ([[UIApplication sharedApplication] canOpenURL:phoneURL]) { [[UIApplication sharedApplication] openURL:phoneURL]; } 

试试这个:

 NSString *phone_number = [[yourPhoneNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789-+()"] invertedSet]] componentsJoinedByString:@""]; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"telprompt://%@", phone_number]]]; 

在iOS9我使用这个代码,它的工作原理:

 NSString *assistanceNumber = [[NSUserDefaults standardUserDefaults] objectForKey:@"AssistanceCallMISDN"]; assistanceNumber= [[assistanceNumber componentsSeparatedByCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:@"0123456789-+()"] invertedSet]] componentsJoinedByString:@""]; assistanceNumber = [assistanceNumber stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSURL *phoneUrl = [NSURL URLWithString:[@"telprompt://" stringByAppendingString:assistanceNumber]]; NSURL *phoneFallbackUrl = [NSURL URLWithString:[@"tel://" stringByAppendingString:assistanceNumber]]; if ([UIApplication.sharedApplication canOpenURL:phoneUrl]) { [UIApplication.sharedApplication openURL:phoneUrl]; } else if ([UIApplication.sharedApplication canOpenURL:phoneFallbackUrl]) { [UIApplication.sharedApplication openURL:phoneFallbackUrl]; } else { [[[UIAlertView alloc] initWithTitle:@"" message:[NSString stringWithFormat:@"No se ha podido realizar la llamada a través de la aplicación. Puede llamar usted al %@", assistanceNumber] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil] show]; [_viewEmergency setHidden:YES]; } 

我的Info.plist

 <key>LSApplicationQueriesSchemes</key> <array> <string>telprompt</string> <string>tel</string> </array>