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

时间:2015-05-21 05:49:34

标签: ios swift

我想查看我的iphone中是否有应用程序。如果它存在,我想启动它,否则打​​开应用程序的App Store链接。

请建议执行此操作所需的参数以及实施方法。

我有App Store链接打开应用程序,但我需要检查手机中是否已有应用程序。

6 个答案:

答案 0 :(得分:13)

试试这段代码

swift 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")!)

    }

}

swift 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")!)
    }

}

答案 1 :(得分:4)

要从其他应用内部打开应用,您需要从要打开的应用中公开URL方案。

如果可以使用openURL功能(使用公开的自定义网址方案)打开应用,则用户可以直接访问您的应用。

如果没有,您可以使用openURL功能打开应用商店应用页面,提供itunes应用网址。

Here lies complete idea关于如何实现它。

答案 2 :(得分:2)

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

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

预配置url的位置(在您的Info.plist中为您的应用注册了一个网址)

因此,如果canOpen为true表示已安装应用,您可以使用以下命令打开:

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

enter image description here

答案 3 :(得分:0)

您可以使用URL方案iOS功能检查应用是否可用,然后打开iTunes网址。尝试使用以下代码解决方案:

首先在您的itunes应用中设置网址方案“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]];
    }

}

答案 4 :(得分:0)

您需要使用网址计划。这个想法如下:

为您的应用设置网址标识符可让其他应用识别。就像那样,您要打开的应用程序需要有一个自定义URL标识符。谷歌它为您所需的应用程序的自定义URL标识符。使用以下链接可参考如何使用URL方案。

Reference

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

答案 5 :(得分:0)

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)

                }
相关问题