首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >canOpenUrl失败,但openUrl成功

canOpenUrl失败,但openUrl成功
EN

Stack Overflow用户
提问于 2016-03-15 15:59:39
回答 2查看 1.9K关注 0票数 3

我面临着一个奇怪的问题。我正在使用xcode 7.2,iOS 9,在实际设备iPhone4S(而不是模拟器)上工作。

我有两个应用程序,app1和app2。app1应该使用url方案向app2发送数据。app2很好地声明了方案app1在plist中引用了该方案(按照iOS9中的要求)

代码语言:javascript
运行
复制
<key>LSApplicationQueriesSchemes</key>
    <array>
        <array>
            <string>OpenLinkMyData</string>
        </array>
    </array>

下面是我使用的代码:

代码语言:javascript
运行
复制
 - (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

但是,如果我使用以下代码:

代码语言:javascript
运行
复制
 - (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源(这是等待的):

代码语言:javascript
运行
复制
-(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有什么问题?

谢谢你的帮助。

EN

Stack Overflow用户

发布于 2019-07-01 14:19:35

关于这个话题的一个附带说明..。

对于未注册的协议,有50项请求限制。

苹果( 在这次讨论中 )提到,对于一个特定版本的应用程序,你只能对canOpenUrl进行有限次数的查询,而在50次未声明的计划调用之后就会失败。我还看到,如果协议被添加,一旦进入失败状态,它仍然会失败。

意识到这一点,可能对某人有用。

票数 1
EN
查看全部 2 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36016275

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档