ios - canOpenUrl 失败,但 openUrl 成功
<p><p>我遇到了一个奇怪的问题。
我正在使用 xcode 7.2、iOS 9,在真实设备 iphone 4S(不是模拟器)上工作。</p>
<p>我有 2 个应用程序,app1 和 app2。 app1 应该使用 url 方案将数据发送到 app2。
app2 已经很好地声明了方案
app1 引用了 plist 中的方案(因为它在 iOS9 中是必需的)</p>
<pre><code><key>LSApplicationQueriesSchemes</key>
<array>
<array>
<string>OpenLinkMyData</string>
</array>
</array>
</code></pre>
<p>这是我使用的代码:</p>
<pre><code> - (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 = [ stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL * url = ];
// 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 = [ canOpenURL:url];
NSLog(@"canOpenUrl = %@", can?@"true":@"false");
});
// code of the app that do stuff...
}
</code></pre>
<p>我得到以下日志:
-canOpenURL:URL 失败:“OpenLinkMyData://(myJsonSuff)”- 错误:“不允许此应用查询方案 OpenLinkMyData”
canOpenUrl = false</p>
<p>但是如果我使用下面的代码:</p>
<pre><code> - (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 = [ stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL * url = ];
if([ openURL:url])
{
NSLog(@"App launched OK");
}
else
{
NSLog(@"App not launched");
}
});
// code of the app that do stuff...
}
</code></pre>
<p>如果不检查scheme是否可用,直接使用,App2很好打开,按要求获取所有数据。
(否则,如果未安装 app2,我会收到“App not launched”日志)。</p>
<p>这是用于接收数据的 App2 源(按预期工作):</p>
<pre><code>-(BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
NSString *prefixToRemove = @"OpenLinkMyData://";
if(url != nil && [ hasPrefix:prefixToRemove])
{
NSString * urlStr = ;
NSString * json = ];
json = ;
NSLog(@"OpenLinkMyData with json: %@", json);
}
return YES;
}
</code></pre>
<p>在我的案例中,canOpenUrl 有什么问题?</p>
<p>感谢您的帮助。</p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>使 <code>LSApplicationQueriesSchemes</code> 成为字符串数组而不是字符串数组:</p>
<pre><code><key>LSApplicationQueriesSchemes</key>
<array>
<string>OpenLinkMyData</string>
</array>
</code></pre></p>
<p style="font-size: 20px;">关于ios - canOpenUrl 失败,但 openUrl 成功,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/36016275/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/36016275/
</a>
</p>
页:
[1]