gpt4 book ai didi

ios - canOpenUrl 失败,但 openUrl 成功

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:38:49 27 4
gpt4 key购买 nike

我遇到了一个奇怪的问题。我正在使用 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)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:URL 失败:“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...
}

如果不检查scheme是否可用,直接使用,App2很好打开,按要求获取所有数据。(否则,如果未安装 app2,我会收到“App not launched”日志)。

这是用于接收数据的 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 有什么问题?

感谢您的帮助。

最佳答案

使 LSApplicationQueriesSchemes 成为字符串数组而不是字符串数组:

<key>LSApplicationQueriesSchemes</key>
<array>
<string>OpenLinkMyData</string>
</array>

关于ios - canOpenUrl 失败,但 openUrl 成功,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36016275/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com