如何以编程方式检查并打开iOS 8中的现有应用程序?

我想检查一个应用程序是否存在于我的iphone或不。 如果存在,我想启动它,否则打​​开应用程序的App Store链接。

请build议所需的参数和方法来实现它。

我有应用程序商店链接来打开应用程序,但我需要检查应用程序是否已经存在于手机中。

试试这个代码

迅速2.2

@IBAction func openInstaApp(sender: AnyObject) { var instagramHooks = "instagram://user?username=your_username" var instagramUrl = NSURL(string: instagramHooks) if UIApplication.sharedApplication().canOpenURL(instagramUrl!) { UIApplication.sharedApplication().openURL(instagramUrl!) } else { //redirect to safari because the user doesn't have Instagram println("App not installed") UIApplication.sharedApplication().openURL(NSURL(string: "https://itunes.apple.com/in/app/instagram/id389801252?m")!) } } 

迅速3

  @IBAction func openInstaApp(sender: AnyObject) { let instagramHooks = "instagram://user?username=your_username" let instagramUrl = URL(string: instagramHooks) if UIApplication.shared.canOpenURL(instagramUrl! as URL) { UIApplication.shared.open(instagramUrl!) } else { //redirect to safari because the user doesn't have Instagram print("App not installed") UIApplication.shared.open(URL(string: "https://itunes.apple.com/in/app/instagram/id389801252?m")!) } } 

要从另一个应用程序中打开应用程序,您需要从要打开的应用程序公开一个URLscheme。

如果可以使用openURL函数(使用公开的自定义URLscheme)打开应用程序,用户可以直接访问您的应用程序。

如果没有,您可以使用openURL函数打开app store应用程序页面,提供iTunes应用程序URL。

这里是完整的想法如何实现它。

您可以使用URLschemeiOSfunction检查应用程序是否可用,然后打开iTunesurl。尝试使用以下代码解决问题:

首先在您的iTunes应用程序中设置URLscheme“testapp”,然后在您想要打开您的应用程序的其他应用程序中使用以下代码:

 -(IBAction) openApp:(id)sender { // Opens the app if installed, otherwise displays an error UIApplication *yourApplication = [UIApplication sharedApplication]; NSString *URLEncodedText = [self.textBox.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; NSString *ourPath = [@"testapp://" stringByAppendingString:URLEncodedText]; NSURL *ourURL = [NSURL URLWithString:ourPath]; if ([yourApplication canOpenURL:ourURL]) { [yourApplication openURL:ourURL]; } else { //The App is not installed. It must be installed from iTunes code. NSString *iTunesLink = @"//Your itunes Url"; [[UIApplication sharedApplication] openURL:[NSURL URLWithString:iTunesLink]]; } } 

您可以使用以下方式检查各个设备是否安装了应用程序:

 // url = "example" (check the following image to understand) let canOpen = UIApplication.sharedApplication().canOpenURL(NSURL(string: url as String)!) 

在哪里url被预先configuration(在你的Info.plist中有一个为你的应用程序注册的URLscheme)

所以,如果canOpen是true意味着应用程序已安装,您可以打开使用:

 UIApplication.sharedApplication().openURL(NSURL(string:url)!) 

在这里输入图像说明

您需要使用URLscheme。 这个想法如下:

为您的应用程序设置URL标识符可以让其他应用程序识别。 就像这样,你想打开的应用程序需要有一个自定义的URL标识符。 Google将其用于所需应用的自定义url标识。 使用下面的链接参考如何使用URLscheme。

供参考

网站检查应用程序的URL标识符

 let naviURL = NSURL(string: "yandexnavi://build_route_on_map?lat_from=55.751802&lon_from=37.586684&lat_to=55.758192&lon_to=37.642817")! let res = UIApplication.sharedApplication().openURL(naviURL) if !res { let appStoreURL = NSURL(string: "itms://itunes.apple.com/ru/app/andeks.navigator/id474500851?mt=8")! UIApplication.sharedApplication().openURL(appStoreURL) }