我遇到了一个奇怪的问题。 我正在使用 xcode 7.2、iOS 9,在真实设备 iphone 4S(不是模拟器)上工作。
我有 2 个应用程序,app1 和 app2。 app1 应该使用 url 方案将数据发送到 app2。 app2 已经很好地声明了方案 app1 引用了 plist 中的方案(因为它在 iOS9 中是必需的)
<key>LSApplicationQueriesSchemes</key>
<array>
<array>
<string>OpenLinkMyData</string>
</array>
</array>
这是我使用的代码:
- (BOOL)applicationUIApplication *)application didFinishLaunchingWithOptionsNSDictionary *)launchOptions
{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0) , ^{
// build the url, using the scheme name, and the data
// note that json is escaped from bad url chars.
NSString * MyJsonDataWellEscaped = [[SomeClass getJSonDataToExport] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL * url = [NSURL URLWithString:[NSString stringWithFormat"OpenLinkMyData://%@",MyJsonDataWellEscaped]];
// Next line should allow test if the app able to manage that scheme is installed.
// BUT in our case, this allways returning false.
bool can = [[UIApplication sharedApplication] canOpenURL:url];
NSLog(@"canOpenUrl = %@", can?@"true""false");
});
// code of the app that do stuff...
}
我得到以下日志: -canOpenURL:URL 失败:“OpenLinkMyData://(myJsonSuff)”- 错误:“不允许此应用查询方案 OpenLinkMyData” canOpenUrl = false
但是如果我使用下面的代码:
- (BOOL)applicationUIApplication *)application didFinishLaunchingWithOptionsNSDictionary *)launchOptions
{ dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0) , ^{
// build the url, using the scheme name, and the data
// not that json is escaped from bad url chars.
NSString * MyJsonDataWellEscaped = [[Move2MyMHelper getJSonDataToExport] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL * url = [NSURL URLWithString:[NSString stringWithFormat"OpenLinkMyData://%@",MyJsonDataWellEscaped]];
if([[UIApplication sharedApplication] openURL:url])
{
NSLog(@"App launched OK");
}
else
{
NSLog(@"App not launched");
}
});
// code of the app that do stuff...
}
如果不检查scheme是否可用,直接使用,App2很好打开,按要求获取所有数据。 (否则,如果未安装 app2,我会收到“App not launched”日志)。
这是用于接收数据的 App2 源(按预期工作):
-(BOOL)applicationUIApplication *)application openURLNSURL *)url sourceApplicationNSString *)sourceApplication annotationid)annotation {
NSString *prefixToRemove = @"OpenLinkMyData://";
if(url != nil && [[url absoluteString] hasPrefix:prefixToRemove])
{
NSString * urlStr = [url absoluteString];
NSString * json = [urlStr substringFromIndex:[prefixToRemove length]];
json = [json stringByRemovingPercentEncoding];
NSLog(@"OpenLinkMyData with json : %@", json);
}
return YES;
}
在我的案例中,canOpenUrl 有什么问题?
感谢您的帮助。
使 LSApplicationQueriesSchemes
成为字符串数组而不是字符串数组:
<key>LSApplicationQueriesSchemes</key>
<array>
<string>OpenLinkMyData</string>
</array>
关于ios - canOpenUrl 失败,但 openUrl 成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36016275/
欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://jike.in/) | Powered by Discuz! X3.4 |