简体   繁体   English

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

[英]How to Programmatically check and open an existing app in iOS 8?

I want to check whether an app is present in my iphone or not. 我想检查我的iphone中是否有应用程序。 If it is present I want to launch it else open the App Store link of the app. 如果它存在,我想启动它,否则打​​开应用程序的App Store链接。

Please suggest the required parameters to do so and way to implement it. 请建议所需的参数以及实现方法。

I have the App Store link to open the app but I need to check if app is already present in the phone. 我有App Store链接打开应用程序,但我需要检查手机中是否已有应用程序。

try this code 试试这段代码

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

}

For opening an app from inside another app, you need to expose a URL scheme from the app you want to open. 要从另一个应用程序内部打开应用程序,您需要从要打开的应用程序中公开URL方案。

If the app can be opened using openURL function (which uses the custom URL scheme exposed), user can directly access your app. 如果可以使用openURL函数(使用公开的自定义URL方案)打开应用程序,则用户可以直接访问您的应用程序。

If not, you can open app store app page using the openURL function, supplying itunes app URL. 如果没有,您可以使用openURL功能打开应用商店应用页面,提供itunes应用URL。

Here lies complete idea about how to achieve it. 这里有关于如何实现它的完整想法

You can check if the respective device has an app is installed using: 您可以使用以下命令检查相应设备是否安装了应用程序:

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

Where url is pre configured (have a URL scheme registered for your app in your Info.plist) url是否已预先配置(在Info.plist中为您的应用注册了一个URL方案)

So, if canOpen is true means that app is installed and you can open using: 因此,如果canOpen为true表示安装了应用程序,您可以使用以下命令打开:

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

在此输入图像描述

You can check whether app is available or not using URL scheme iOS feature and then open iTunes url.Try with following code for your solution: 您可以使用URL方案iOS功能检查应用程序是否可用,然后打开iTunes url。尝试使用以下代码获取解决方案:

First set URL scheme "testapp" in your itunes app and then use following code in other app from where you want to open your app : 首先在您的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]];
    }

}

You need to use URL Schemes. 您需要使用URL Schemes。 The idea is as follows: 这个想法如下:

Setting URL identifier to your apps makes it recognize by other apps. 为您的应用设置网址标识符可让其他应用识别。 Just like that, the app you want to open need to have one custom URL identifier. 就像那样,您要打开的应用程序需要有一个自定义URL标识符。 Google it for custom URL identifier of your desired app. 谷歌它为您所需的应用程序的自定义URL标识符。 Use below link for reference on how to use URL Schemes. 使用以下链接可参考如何使用URL方案。

For Reference 参考

Site to check URL identifiers for apps 用于检查应用的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)

                }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM