Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

ios

swift

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.

like image 304
Aman Jain Avatar asked May 21 '15 05:05

Aman Jain


3 Answers

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

enter image description here

like image 163
gabriel Avatar answered Oct 20 '22 00:10

gabriel


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

}
like image 45
Memon Irshad Avatar answered Oct 20 '22 01:10

Memon Irshad


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.

like image 4
Nirav Bhatt Avatar answered Oct 20 '22 01:10

Nirav Bhatt