4
 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?

stuart_gunn
  • 311
  • 2
  • 9
  • refer this tutorial [The Complete Tutorial on iOS/iPhone Custom URL Schemes](http://iosdevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html), it may help you. or Possible duplicate [Launch an app from within another (iPhone)](http://stackoverflow.com/questions/419119/launch-an-app-from-within-another-iphone) – Dipen Panchasara Dec 08 '15 at 07:27

3 Answers3

10

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.

Roy K
  • 3,319
  • 2
  • 27
  • 43
  • Thanks for replying, LSApplicationQueriesScheme is specified in the app. Entering a URL other than the one specified in it results in a NO for canOpenURL. Yes, the target app scheme has previously been installed on both the sim and the phone. – stuart_gunn Dec 08 '15 at 07:26
  • @stuart_gunn try to restart device and yet again. – Roy K Dec 08 '15 at 10:01
  • 2
    @stuart_gunn if you open safari browser app in the device, then type the app scheme url string in the url field (appscheme://), press enter, will it open the app or not? – Roy K Dec 08 '15 at 11:38
  • 3
    Sooooo it turns out the URL scheme was registered for BOTH apps - the calling and callee. Entering the scheme in Safari opened up the wrong app. Thanks for your help. Feeling slightly stupid. – stuart_gunn Dec 08 '15 at 11:43
5

Ensure that you are using LSApplicationQueriesSchemes instead of URL types

It works well only for LSApplicationQueriesSchemes

This will not work

URL types

This will work

LSApplicationQueriesSchemes

Musa almatri
  • 5,596
  • 2
  • 34
  • 33
-1

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