Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Open URL Schemes in IOS

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
        }
like image 605
Sunil Kumar Avatar asked Jan 30 '17 12:01

Sunil Kumar


People also ask

What is URL scheme in iOS?

URL schema is used as an identifier in launching applications and performing a set of commands in iOS devices. The schema name of a URL is the first part of a URL. (e.g. schemaname:// ). For web pages, the schemas are usually http or https.

How do I add a custom URL scheme to my iOS app?

Register your URL schemeRegister your scheme in Xcode from the Info tab of your project settings. Update the URL Types section to declare all of the URL schemes your app supports, as shown in the following illustration. In the URL Schemes box, specify the prefix you use for your URLs.

How do I open URL on Iphone?

Search for the page. In search results, tap the title of the page. At the top, tap the address bar to select the entire URL.

What are the various types of URL schemes?

There are two types of URL: Absolute URL. Relative URL.


1 Answers

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];
    }
}
like image 162
user3182143 Avatar answered Oct 09 '22 01:10

user3182143