5

I have 2 apps, which are meant for different purpose, where I should not allow user to use both apps in same device. How can I check whether other app installed or not? I think I can do it using Open URL as following by putting app bundle id which is not working, I am stuck to get url for my app EX : "fb://"

       if ([[UIApplication sharedApplication] canOpenURL:@"My App URL"]) {
            //App installed
        }
        else{
            //App Not installed
        }
Poles
  • 3,497
  • 8
  • 41
  • 89
Sunil Kumar
  • 81
  • 1
  • 1
  • 8

5 Answers5

6

You have 2 Apps.Now you want to open First App from the Second App.I will give you the step by step instruction please follow that.

My First application name is LinkAppSample and Second application Name is LinkSecondApp

STEP 1: Add the below things in first app LinkAppSample

<key>CFBundleURLTypes</key>
  <array>
    <dict>
        <key>CFBundleURLName</key>
        <string>com.example.com</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>chatapp</string>
        </array>
    </dict>
  </array>

Then you need to add

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>chatapp</string>
</array>

See the below screenshot below

enter image description here

STEP 2: Go to Second App.My Second App Name is LinkSecondApp.Now click LinkSecondApp project.Click TARGETS and click info.Then Click URL Type and add or write or set name in URL Schemes Target->Info->URL types->Add url types

First Click Info of Targets

enter image description here

Then click URL Tpes.Once you click it shos you the + symbol.

enter image description here

Now click + symbol and give the name of the app in URL Schemes.You must set Rote as Viewer

enter image description here

NOTE:Your URL Schemes name must be same(First App Plist Name and Second App URL Schemes name is chatapp here).

STEP 3: Now you must add code in Second application LinkSecondApp for opening the First app.

LinkSecondApp.m

-(IBAction)actionOpenFirstApp:(id)sender
{
   NSString *customURL = @"chatapp://";
   UIApplication *application = [UIApplication sharedApplication];
   NSURL *URL = [NSURL URLWithString:@"chatapp://"];
   if ([application respondsToSelector:@selector(openURL:options:completionHandler:)]) 
   {
    [application openURL:URL options:@{}
       completionHandler:^(BOOL success) {
           NSLog(@"Open %@: %d",customURL,success);
       }];
    } 
    else {
       UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"Error!" message:@"No Custom URL is defined" preferredStyle:UIAlertControllerStyleAlert];
       UIAlertAction* ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:nil];
       [alertController addAction:ok];
       [self presentViewController:alertController animated:YES completion:nil];
    }
}
user3182143
  • 9,165
  • 3
  • 28
  • 36
  • thanks for response, i have check first app installed or not, I'm trying following code, – Sunil Kumar Jan 31 '17 at 12:59
  • 1
    if ([application canOpenURL:URL]) { NSLog(@"Success"); }else{ NSLog(@"Failed"); } Even if app is not installed also I'm getting success – Sunil Kumar Jan 31 '17 at 13:05
  • Did you install first app? – user3182143 Jan 31 '17 at 13:11
  • Yes, if i install both the app and open first app it opens, once i uninstalled first app, and check for can openUrl:"firstApp" I'm getting true in second app – Sunil Kumar Jan 31 '17 at 13:14
  • First install and run first app only after that install and run second app. – user3182143 Jan 31 '17 at 13:18
  • that is not i wanted, my requirement is to check First app is installed or not from second app, if first app is already installed i should not allow user to register in second app, so I'm checking this using CANOPENURL Method, where I'm getting always true, even if first app is not installed – Sunil Kumar Jan 31 '17 at 13:35
  • Sunil my answer works perfectly as I created sample one and I worked out for you.Above things are absolutely correct. – user3182143 Feb 01 '17 at 12:46
  • but by hitting the url in mail it cannot open because that url convert like this chatapp/ – lokesh Apr 15 '17 at 07:49
  • Please follow my answer carefully – user3182143 Apr 15 '17 at 09:33
  • @user3182143: Sunil is correct. This answer works until the first app is deleted. Coz when the first app is deleted and when you hit the button action, it still returns YES, telling the user that "the app you just deleted" is still installed. – The X-Coder Jul 06 '17 at 09:36
  • Try it first and then tell me. – user3182143 Aug 05 '17 at 11:02
3

Swift 3.0 working Answer :

Everything remains same as Selected answer ( above answer which is answered by user3182143 as this causes few issue by other user so i answered this way , only code implementation is different rest follow same step as answered by above correct answer )

But code implementation is different now :

let kCustomURLScheme = "yourappscheme://"

 func openCustomApp() {
    if (UIApplication.shared.canOpenURL(URL(string:kCustomURLScheme)!)) {
        print("true")
    }

    if openCustomURLScheme(customURLScheme: kCustomURLScheme) {
        // app was opened successfully
    } else {
        // handle unable to open the app, perhaps redirect to the App Store
        print("unable to open , go to itune store to download it ")
    }
}

This function using APPSCHEME to check whether any installed app with specific APPSCHEME can be launched or not , if yes then it will launch else if you having application iTune link then it will open app link (above function calling below method for final launching ):

 func openCustomURLScheme(customURLScheme: String) -> Bool {
        let customURL = URL(string: customURLScheme)!

        if UIApplication.shared.canOpenURL(customURL) {
            if #available(iOS 10.0, *) {
                UIApplication.shared.open(customURL)
                UIApplication.shared.open(URL(string: "https://itunes.apple.com/in/app/appname/id404249815?mt=8")!, options: [:], completionHandler: nil)
            } else {
                UIApplication.shared.openURL(customURL)
            }
            return true
        }

        return false
    }

This code working fine for Swift3.0 and iOS 10 , feel free to share your feedback and comment .

Shobhakar Tiwari
  • 7,520
  • 4
  • 35
  • 67
1

To know whether an app is installed or not

-(BOOL) isAppInstalled:(NSString *)appUrl{

return [[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:appUrl]]; // @ is not needed as you are passing input of function as a parameter.
}

To know whether any app got installed or not, invoke this function

if( [self isAppInstalled:@"MyApp2://"]) {

     NSLog("The second app is installed");  //Perform your actions
 }

Remember to include the second app's url scheme in First Apps LSApplicationURLScheme in its Info.plist.

Naresh Reddy M
  • 1,066
  • 1
  • 10
  • 27
Madhu Avinash
  • 903
  • 1
  • 7
  • 26
  • what should be the Url string @"MyApp2://" is app name or bundle id – Sunil Kumar Jan 30 '17 at 12:58
  • It can be anything you want, place this key value pairs in Info.plist CFBundleTypeRole Viewer CFBundleURLName Test CFBundleURLSchemes MyApp2 – Madhu Avinash Jan 30 '17 at 13:01
  • Go through this link for better understanding of URLSchemas http://stackoverflow.com/questions/30987986/ios-9-not-opening-instagram-app-with-url-scheme – Madhu Avinash Jan 30 '17 at 13:04
1

go to the Project > Target > Info > URL Types section in Xcode. Here we must add two URL schemes that the Sign-In SDK needs to be properly set up so as to properly work. Use the plus button, and in the URL Schemes field just type the app bundle ID value (e.g. com.admin.myApp).

Then in your code use this

if ([[UIApplication sharedApplication] canOpenURL:@"com.admin.myApp://"]) {
            //App installed
        }
        else{
            //App Not installed
        }
Sargis
  • 1,072
  • 1
  • 17
  • 29
1

I've just went thru this and found the accepted answer very confusing so I'm making mine.

To open app1 from app2

1) In app1 : declare the custom schemes the app can handle

You can do that in text directly in the app1.plist file OR via the visual info Editor (Target > Info > URLTypes)

<key>CFBundleURLTypes</key>
  <array>
    <dict>
        <key>CFBundleTypeRole</key>
        <string>Viewer</string>
        <key>CFBundleURLName</key>
        <string>com.mycompany.myapp</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>myscheme</string>
        </array>
    </dict>
  </array>

2) In app2 : declare the schemes that the application can call (white list)

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>myscheme</string>
</array>

3) In app2 : call this method somewhere

- (void)callApp1
{
    NSString *customURL = @"myscheme://";
    UIApplication *application = [UIApplication sharedApplication];
    NSURL *URL = [NSURL URLWithString:customURL];
    if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"10.0")) {
        if ([application respondsToSelector:@selector(openURL:options:completionHandler:)]) {
            [application openURL:URL options:@{}
               completionHandler:^(BOOL success) {
                   NSLog(@"Open %@: %d",customURL,success);
               }];
        } else {
            BOOL success = [application openURL:URL];
            NSLog(@"Open %@: %d",customURL,success);
        }
    }
    else {
        if ([application canOpenURL:URL]) {
            [application openURL:URL];
        }
    }
}

Of course the scheme (here myscheme must match in the three parts).

Pretty easy and to my knowledge it's all that's needed !

Jul
  • 31
  • 4