繁体   English   中英

即使未安装应用程序,canOpenURL 也会为自定义 URL 方案返回 true

[英]canOpenURL returning true for custom URL scheme even if app is not installed

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

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

即使未安装公开自定义 URL 的目标应用程序,应用程序也会为“canOpenURL”返回 true。 此行为发生在手机和模拟器上。 openURL 然后默默地失败。 任何想法为什么会发生这种情况/如何捕捉这种情况?

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

在此处输入图片说明

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

另外,请使用以下逻辑,因为它更安全:

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。 从结果中得出如何进行。

确保您使用的是LSApplicationQueriesSchemes而不是URL 类型

它仅适用于LSApplicationQueriesSchemes

行不通

网址类型

这将工作

LSApplicationQueriesSchemes

这是我用来打开 Uber 应用程序(如果已安装)的方法,否则打开 Uber 网站

if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"uber://"]])
{
    //Uber is installed
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"uber://"]];
}
else
{
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"https://uber.com"]];
}

不要忘记在 info.plist 文件中添加这个 LSApplicationQueriesSchemes

像这样(应用程序 uber 和 twitter 的名称已包含在此) info.plist 截图

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM