14

Simple question, I'm developing an application that will register its own URL Scheme. I plan to launch the application using a QRCode via a person's favourite QRCode reader.

My question: if my application is not yet installed on their iPhone/iPad, what will happen? Will they be directed to the App Store to download my app? Or will it just fail?

I'm not at my Mac right now, otherwise I'd build a test app to launch a URL for an app I know is not installed. If I get back to my Mac before someone answers, I'll update the question with my results.

ThaDon
  • 7,826
  • 9
  • 52
  • 84

2 Answers2

12

If you link directly to your app scheme, it will fail. However, if you link to a page on a website with the code available on this StackOverFlow question, it will attempt to open your app, then the app store if it fails.

Community
  • 1
  • 1
Ali Hamze
  • 1,590
  • 13
  • 26
5

You can simply read the return value of the method -(BOOL)openURL:(NSURL*)url. If it's NO, it means that the target application isn't installed. The following code gives an example using the navigon url scheme:

NSString *stringURL = @"navigon://coordinate/NaviCard/19.084443/47.573305";
NSURL *url = [NSURL URLWithString:stringURL];
if([[UIApplication sharedApplication] openURL:url]) {
    NSLog(@"Well done!");
} else {
    stringURL = @"https://itunes.apple.com/it/app/navigon-europe/id320279293?mt=8";
    url = [NSURL URLWithString:stringURL];
    [[UIApplication sharedApplication] openURL:url];
}

Update Swift 3

var stringURL = "navigon://coordinate/NaviCard/19.084443/47.573305"
var url = URL.init(string: stringURL)
if !UIApplication.shared.canOpenURL(url!) {
    stringURL = "https://itunes.apple.com/it/app/navigon-europe/id320279293?mt=8"
    url = URL.init(string: stringURL) 
}
if #available(iOS 10.0, *) {
    UIApplication.shared.open(url!, options: [:], completionHandler: nil)
} else {
    UIApplication.shared.openURL(url!)
}
Zeb
  • 1,715
  • 22
  • 34
  • when I am trying to use this my link is giving me error "Safari cannot open the page because the address is not valid" in the case when the app is not installed. Can you please help, I will appreciate it. Thanks!! – Amanpreet Sep 10 '19 at 12:25