16

I am in this situation where I have to display a button which says "Open myApp" (if myApp is installed on the device) or it says "Download myApp" (if myApp is not installed on the device) in an iphone app. To do this, I need to detect whether an app (with a known custom URL) has been installed on the device. How can I do this? Thanks in advance.

dda
  • 6,030
  • 2
  • 25
  • 34
Amarsh
  • 11,214
  • 18
  • 53
  • 78

5 Answers5

33

UPDATED 8th January 2014 - 3 things you can do

I actually had to do this for a client again. They wanted users to be able to open their second app from the main app if it had been installed.

This is my finding. Use the canOpenURL method to check if an app is installed or/and then use the openURL method to

  1. Open the application installed on the iOS device
  2. Take the user to the app store directly pointing them to the app/your list of developer apps
  3. Take them to a website instead

All code samples available for each scenario

//Find out if the application has been installed on the iOS device
- (BOOL)isMyAppInstalled { 
    return [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"nameOfMyApp:"]]; 
} 

- (IBAction)openOrDownloadApp { 
    //This will return true if the app is installed on the iOS device
    if ([self myAppIsInstalled]){
        //Opens the application
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"nameOfMyApp:"]]; 
    } 
    else { //App is not installed so do one of following:

        //1. Take the user to the apple store so they can download the app
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms://itunes.com/apps/nameOfMyApp"]]; 

        //OR

        //2. Take the user to a list of applications from a developer
        //or company exclude all punctuation and space characters. 
        //for example 'Pavan's Apps'
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itms://itunes.com/apps/PavansApps"]];

        //OR

        //3. Take your users to a website instead, with maybe instructions/information
         [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"http://www.pavan.com/WhyTheHellDidTheAppNotOpen_what_now.html"]];

    } 
}

Choose one option, I've just spoiled you with choice. Choose one that fits your requirements. In my case I had to use all three options in different areas of the program.

Pavan
  • 17,840
  • 8
  • 59
  • 100
  • The plist is not found in iOS 4. Do you know where has it been moved to? – samwize Feb 04 '11 at 08:00
  • Hi, sorry for the late reply. No problem. The file is located in the '/private/var/mobile/Library/Caches/' folder. Hope that helps. Cheers – Pavan Apr 30 '11 at 05:43
  • That is the path in switch-case 2. But the file is not found (on iOS 4.3). – samwize May 03 '11 at 06:17
  • aww man... im still on 4.2.1 *sighs* Sam ill do some research if anything upgrade to 4.3 soon and try finding it for you. When I do you'll be the first I contact. – Pavan May 03 '11 at 07:01
  • Is there a similar method for Android? – Zennichimaro Sep 03 '12 at 03:41
  • @Zennichimaro. there certainly is although it may be best to post a new question and I'll post my answer there for others to see. – Pavan Sep 03 '12 at 03:53
  • @Pavan, I have posted the question here: http://stackoverflow.com/questions/12273907/detecting-programmatically-whether-an-app-is-installed-on-android Thanks! – Zennichimaro Sep 05 '12 at 02:54
  • it gives me an error and it talks about permission while debugging. Could it be possible that it doesn't work while debugging on a device, but it works if the app is from the AppStore? – LolaRun Jan 08 '14 at 16:58
  • @LolaRun hey lolrun, i've updated my answer to accommodate you, this is the way to do it. It's a cleaner solution. – Pavan Jan 09 '14 at 00:05
20

If the URL scheme for your app is "myapp:", then

BOOL myAppInstalled = [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:@"myapp:"]];

(Requires iOS 3.0.)

Mr.Kushwaha
  • 491
  • 5
  • 14
Mark T.
  • 301
  • 2
  • 4
6

To check app is install in device or not

1) In info.plist add LSApplicationQueriesSchemes as below example

enter image description here

2) and in URL Types

enter image description here

3) Now to check app is install or not

- (IBAction)openAppPressed:(UIButton *)sender {
    NSString *urlString = @"XYZAPP://";
    NSURL *url = [NSURL URLWithString:urlString];

    if ([[UIApplication sharedApplication] canOpenURL:url]) {
        [[UIApplication sharedApplication] openURL:url];
    }
    else {
        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"itunes link for download app"]];
    }
}
Hardik Thakkar
  • 15,269
  • 2
  • 94
  • 81
0

You can add a simple meta tag in the head of any page that needs this app-sniffing.

For more info, go here:

http://developer.apple.com/library/ios/#documentation/AppleApplications/Reference/SafariWebContent/PromotingAppswithAppBanners/PromotingAppswithAppBanners.html

  • 1
    Perhaps, summarizing your URL's content in your post would help the user more then just posting a link. Other then that, good job on your first post :-). –  Mar 26 '13 at 19:20
0

For those using canOpenURL it is always safe to migrate from this to openURL:options:completionHandler:

NSString *urlString = @"XYZAPP://";
NSURL *url = [NSURL URLWithString:urlString];
[[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success) {
        if (!success) {
            [[UIApplication sharedApplication] openURL:appStoreUrl options:@{} completionHandler:nil];
        }
}];

because that doesn't require you to declare the scheme ahead of time.

canOpenURL which is deprecated already has some odd limitations because Twitter used it to detect hundreds of apps long ago.

user16780334
  • 422
  • 5
  • 20