ios - canOpenUrl 失败,但 openUrl 成功

标签 ios

我遇到了一个奇怪的问题。 我正在使用 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/

相关文章:

ios - webview后如何返回应用程序?

ios - MSMessages 扩展 iOS 10 删除消息 UITextField

ios - Swift 2.1 中的嵌套闭包

iphone - 将 UIView 添加到 cell.contentView 时出现 UITableView 性能问题

objective-c - iOS - 数学帮助 - 使用捏合手势缩放基础图像需要覆盖图像调整相对的 X/Y 坐标

ios - Swift 4 - 'substring(to:)' 已弃用 - 替代我的代码

objective-c - iOS – NSMutableArray 和 block 复制

ios - 如何在 Xcode 项目之间共享文件

javascript - 我们如何处理 TVML 的逻辑以及它可以与 UIKit 一起使用吗?

ios - 我应该为这个完整的场景使用操作队列吗?