2012-03-14 48 views
12

简单的问题,我正在开发一个应用程序,它将注册自己的URL方案。我计划通过一个人最喜爱的QRCode阅读器使用QRCode启动应用程序。未安装的应用的URL方案

我的问题:如果我的应用程序尚未安装在他们的iPhone/iPad上,会发生什么情况?他们会被定向到App Store下载我的应用程序吗?或者它会失败?

我现在不在我的Mac上,否则我会构建一个测试应用程序,以启动我知道未安装的应用程序的URL。如果我在别人回答之前回到我的Mac,我会用我的结果更新问题。

回答

11

如果您直接链接到您的应用程序方案,则会失败。但是,如果您链​​接到网址上的代码为on this StackOverFlow question的网页,它将尝试打开您的应用,然后尝试打开应用商店(如果失败)。

6

您可以简单地读取方法-(BOOL)openURL:(NSURL*)url的返回值。如果不是,则表示目标应用程序未安装。下面的代码提供了使用NAVIGON URL方案的示例:

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

更新夫特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!) 
}