即使未安装应用,canOpenURL对于自定义URL方案也返回true [英] canOpenURL returning true for custom URL scheme even if app is not installed

查看:226
本文介绍了即使未安装应用,canOpenURL对于自定义URL方案也返回true的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 NSString *customURL = @"mycustomurl://"; 

 if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:customURL]]) {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:customURL]];
 } else {
    ...
 }

即使未安装公开自定义URL的目标应用程序,该应用程序也会对'canOpenURL'返回true.在电话和电话上均会发生这种情况.模拟器.然后,openURL静默失败.为什么会发生这种情况/如何捕捉这种状况的任何想法?

The app returns true for 'canOpenURL', even if the target app that exposes the custom URL is not installed. This behaviour occurs on both phone & simulator. openURL then silently fails. Any ideas why this is happening/how to catch this condition?

推荐答案

如果使用SDK 9.0及更高版本的应用程序,则必须确保在主应用程序的info.plist中添加要打开的应用程序方案. :

If using an app with SDK 9.0 and up, then you will have to make sure to add the app schemes you want to open in your main app's info.plist:

不将以上内容添加到主应用程序的info.plist中(相应地更改方案),canOpenURL将始终返回NO.除非使用iOS SDK低于9.0的应用程序,否则它不会发生.

Without adding the above to the main app's info.plist (change schemes accordingly) canOpenURL will always return NO. Unless using an app with iOS SDK lower then 9.0 then it won't happen.

也请使用以下逻辑,因为这样做更安全:

Also, use the following logic as it is safer:

NSString * urlStr = @"mycustomurl://"; 
NSURL * url = [NSURL URLWithString:urlStr];

if ([[UIApplication sharedApplication] canOpenURL:url]) {
    if([[UIApplication sharedApplication] openURL:url]) {
       // App opened
    } else {
       // App not opened
    }
} else {
    // Can not open URL
}

最后检查一次,我建议是在设备中打开Safari应用程序,在url字段中输入应用程序方案url字符串,然后按Enter.从结果中总结出如何进行.

Last check I suggest is to open Safari app in the device, enter the app scheme url string in the url field, press enter. Conclude from the result how to proceed.

这篇关于即使未安装应用,canOpenURL对于自定义URL方案也返回true的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆