我面临着一个奇怪的问题。我正在使用xcode 7.2,iOS 9,在实际设备iPhone4S(而不是模拟器)上工作。
我有两个应用程序,app1和app2。app1应该使用url方案向app2发送数据。app2很好地声明了方案app1在plist中引用了该方案(按照iOS9中的要求)
<key>LSApplicationQueriesSchemes</key>
<array>
<array>
<string>OpenLinkMyData</string>
</array>
</array>
下面是我使用的代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)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: canOpenUrl:"OpenLinkMyData://(myJsonSuff)“-错误:”此应用程序不允许查询方案OpenLinkMyData“canOpenUrl= false
但是,如果我使用以下代码:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)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...
}
如果我不检查方案是否可用并直接使用它,App2就会被很好地打开,并根据需要获取所有数据。(否则,如果没有安装app2,我就会得到“”日志)。
以下是用于接收数据的App2源(这是等待的):
-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)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有什么问题?
谢谢你的帮助。
发布于 2019-07-01 14:19:35
关于这个话题的一个附带说明..。
对于未注册的协议,有50项请求限制。
苹果( 在这次讨论中 )提到,对于一个特定版本的应用程序,你只能对canOpenUrl
进行有限次数的查询,而在50次未声明的计划调用之后就会失败。我还看到,如果协议被添加,一旦进入失败状态,它仍然会失败。
意识到这一点,可能对某人有用。
https://stackoverflow.com/questions/36016275
复制相似问题