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

11
我想检查我的 iPhone 上是否安装了某个应用。如果已安装,我想要启动它;否则,需要打开该应用的 App Store 链接。
请建议所需的参数并提供实现方式。
我有打开应用的 App Store 链接,但需要检查该应用是否已经在手机上安装。

可能是重复的问题:如何链接到应用商店上的应用程序 - Abubakr Dar
6个回答

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
这里的"Instagram"是什么?它是关键词还是什么? - Heshan Sandeepa
it's keyword don't change it - Memon Irshad

4

要在另一个应用程序中打开应用程序,您需要从要打开的应用程序中公开URL方案。

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

如果不能,您可以使用openURL函数打开应用商店应用程序页面,提供iTunes应用程序URL。

这里包含完整的想法关于如何实现它。


2

您可以使用以下代码检查所需设备是否已安装应用程序:

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

url被预配置时(在您的Info.plist中为您的应用程序注册了URL方案),请注意:

因此,如果canOpen为true,则表示该应用已安装,您可以使用以下方式打开:

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

enter image description here


不明白为什么在重定向URL(com.example)时会删除一个斜杠。 - lokesh

0
你需要使用URL Scheme。其想法如下:
将URL标识符设置为您的应用程序,使其被其他应用程序识别。就像这样,您想要打开的应用程序需要有一个自定义的URL标识符。搜索您所需应用程序的自定义URL标识符,请使用以下链接了解如何使用URL Scheme。
参考此处
检查应用程序URL标识符的网站。

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)

                }

0

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

首先在您的iTunes应用程序中设置URL方案“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]];
    }

}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接