Open URL Schemes in IOS
Asked Answered
S

5

6

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
        }
Strychnine answered 30/1, 2017 at 12:29 Comment(1)
check this answer #30366026Ambulator
M
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];
    }
}
Margy answered 31/1, 2017 at 10:57 Comment(0)
D
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 .

Diogenes answered 6/7, 2017 at 17:4 Comment(0)
O
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.

Okubo answered 30/1, 2017 at 12:46 Comment(3)
what should be the Url string @"MyApp2://" is app name or bundle idStrychnine
It can be anything you want, place this key value pairs in Info.plist <array> <dict> <key>CFBundleTypeRole</key> <string>Viewer</string> <key>CFBundleURLName</key> <string>Test</string> <key>CFBundleURLSchemes</key> <array> <string>MyApp2</string> </array> </dict> </array> </plist>Okubo
Go through this link for better understanding of URLSchemas #30988486Okubo
P
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
        }
Plethora answered 30/1, 2017 at 14:25 Comment(0)
S
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 !

Spadiceous answered 9/11, 2017 at 18:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.