简体   繁体   中英

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. If it is present I want to launch it else open the App Store link of the app.

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.

try this code

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

}

For opening an app from inside another app, you need to expose a URL scheme from the app you want to open.

If the app can be opened using openURL function (which uses the custom URL scheme exposed), user can directly access your app.

If not, you can open app store app page using the openURL function, supplying itunes app 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)

So, if canOpen is true means that app is installed and you can open using:

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:

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 :

-(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. 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. Google it for custom URL identifier of your desired app. Use below link for reference on how to use URL Schemes.

For Reference

Site to check URL identifiers for apps

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)

                }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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