Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 {
    ...
 }

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?

like image 797
stuart_gunn Avatar asked Dec 11 '22 19:12

stuart_gunn


1 Answers

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:

enter image description here

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.

like image 89
Roy K Avatar answered Dec 13 '22 11:12

Roy K