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

社会主义新天地 提交于 2019-12-06 00:16:42

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:

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
}

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.

Ensure that you are using instead LSApplicationQueriesSchemes of URL types

It works well only for LSApplicationQueriesSchemes

This will not work

This will work

This is what i have used to open Uber app if it is installed else open Uber Website

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"]];
}

Do not forget to add this LSApplicationQueriesSchemes in your info.plist file

Like this (the names of the app uber and twitter has been included in this) info.plist screenshot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!