Launch an app from within another (iPhone)
Asked Answered
P

14

183

Is it possible to launch any arbitrary iPhone application from within another app?, For example in my application if I want the user to push a button and launch right into the Phone app (close the current app, open the Phone app).

would this be possible? I know this can be done for making phone calls with the tel URL link, but I want to instead just have the Phone app launch without dialing any specific number.

Plessor answered 7/1, 2009 at 3:41 Comment(0)
S
84

As Kevin points out, URL Schemes are the only way to communicate between apps. So, no, it's not possible to launch arbitrary apps.

But it is possible to launch any app that registers a URL Scheme, whether it's Apple's, yours, or another developer's. The docs are here:

Defining a Custom URL Scheme for Your App

As for launching the phone, looks like your tel: link needs to have least three digits before the phone will launch. So you can't just drop into the app without dialing a number.

Sweepstake answered 7/1, 2009 at 5:35 Comment(5)
You can check whether the application in question is installed by using UIApplication's - (BOOL)canOpenURL:(NSURL *)urlBolero
what happens if 2 apps register the same url handler and then the url is called? I know in Android you will be presented w/ a list so you can choose which of the two you want to run. How does ios handle this?Cattima
Note: If more than one third-party app registers to handle the same URL scheme, there is currently no process for determining which app will be given that scheme. Ref: developer.apple.com/library/ios/#documentation/iPhone/…Opacity
This is the updated link which mentions the note about more-than one third party app registering to handle the same URL-scheme: Apple DocsExoergic
Out of date again. Updated link: developer.apple.com/documentation/uikit/…Transfusion
T
80

I found that it's easy to write an app that can open another app.

Let's assume that we have two apps called FirstApp and SecondApp. When we open the FirstApp, we want to be able to open the SecondApp by clicking a button. The solution to do this is:

  1. In SecondApp

    Go to the plist file of SecondApp and you need to add a URL Schemes with a string iOSDevTips(of course you can write another string.it's up to you).

enter image description here

2 . In FirstApp

Create a button with the below action:

- (void)buttonPressed:(UIButton *)button
{
  NSString *customURL = @"iOSDevTips://";

  if ([[UIApplication sharedApplication] canOpenURL:[NSURL URLWithString:customURL]])
  {
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:customURL]];
  }
  else
  {
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"URL error"
                              message:[NSString stringWithFormat:@"No custom URL defined for %@", customURL]
                              delegate:self cancelButtonTitle:@"Ok" 
                              otherButtonTitles:nil];
    [alert show];
  }

}

That's it. Now when you can click the button in the FirstApp it should open the SecondApp.

Thunderclap answered 4/6, 2014 at 9:31 Comment(8)
You can see the full explanation here iosdevelopertips.com/cocoa/…Gallbladder
@Lee I am following your post , safari can open the application but another app not open (button pressed method ) else block is executed , what is the problem can you please help meSaguaro
Please give me more some detail of your problemThunderclap
@srinivas n I believe you need to replace @"iOSDevTips://"; with @"iOSDevTips://location?id=1"];Proofread
People who can't get this to work, add a array of strings named 'LSApplicationQueriesSchemes' in Info.plist of the FirstApp. Then add the schemes that your app want to open, in this case the Item 0 would be iOSDevTipsKalynkam
See also KIO's answer below (https://mcmap.net/q/135893/-launch-an-app-from-within-another-iphone). FirstApp needs to add LSApplicationQueriesSchemes to its Info.plist to be able to open SecondApp.Folkway
Nice answer, now how to get bundle id of First App in Second App?Sami
Hi. My App name has a space in between. How do I write it? if the other app name is my developer tips.? how do i create an url?With
L
31

The lee answer is absolutely correct for iOS prior to 8.

In iOS 9+ you must whitelist any URL schemes your App wants to query in Info.plist under the LSApplicationQueriesSchemes key (an array of strings):

enter image description here

Lila answered 17/11, 2015 at 17:35 Comment(1)
The best answer should be a merge between @KIO and villy393Cyprinid
A
24

In Swift

Just in case someone was looking for a quick Swift copy and paste.

if let url = URL(string: "app://"), UIApplication.shared.canOpenURL(url) {
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
        } else if let itunesUrl = URL(string: appLink), UIApplication.shared.canOpenURL(itunesUrl) {
            UIApplication.shared.open(itunesUrl, options: [:], completionHandler: nil)
        }
Anemophilous answered 19/1, 2016 at 13:30 Comment(0)
L
18

In order to let you open your application from another, you'll need to make changes in both applications. Here are the steps using Swift 3 with iOS 10 update:

1. Register your application that you want to open

Update the Info.plist by defining your application's custom and unique URL Scheme.

enter image description here

Note that your scheme name should be unique, otherwise if you have another application with the same URL scheme name installed on your device, then this will be determined runtime which one gets opened.

2. Include the previous URL scheme in your main application

You'll need to specify the URL scheme you want the app to be able to use with the canOpenURL: method of the UIApplication class. So open the main application's Info.plist and add the other application's URL scheme to LSApplicationQueriesSchemes. (Introduced in iOS 9.0)

enter image description here

3. Implement the action that opens your application

Now everything is set up, so you're good to write your code in your main application that opens your other app. This should looks something like this:

let appURLScheme = "MyAppToOpen://"

guard let appURL = URL(string: appURLScheme) else {
    return
}

if UIApplication.shared.canOpenURL(appURL) {

    if #available(iOS 10.0, *) {
        UIApplication.shared.open(appURL)
    }
    else {
        UIApplication.shared.openURL(appURL)
    }
}
else {
    // Here you can handle the case when your other application cannot be opened for any reason.
}

Note that these changes requires a new release if you want your existing app (installed from AppStore) to open. If you want to open an application that you've already released to Apple AppStore, then you'll need to upload a new version first that includes your URL scheme registration.

Loaning answered 25/5, 2017 at 8:7 Comment(2)
This is the right answer. I noticed that only adding "LSApplicationQueriesSchemes" did not work, the "URL Scheme" also needs to be added in info.plist.Indiscreet
This answer is 100% correct. In the else part of "if UIApplication.shared.canOpenURL(appURL)" you can add this: let appStoreURL = URL(string: "apps.apple.com/app/app-name/id123456789") // Replace with the actual App Store URL of the app if let appStoreURL = appStoreURL { UIApplication.shared.open(appStoreURL, options: [:], completionHandler: nil) }Gastropod
B
11

Here is a good tutorial for launching application from within another app:
iOS SDK: Working with URL Schemes
And, it is not possible to launch arbitrary application, but the native applications which registered the URL Schemes.

Bobine answered 26/12, 2011 at 11:4 Comment(0)
S
9

To achieve this we need to add few line of Code in both App

App A: Which you want to open from another App. (Source)

App B: From App B you want to open App A (Destination)

Code for App A

Add few tags into the Plist of App A Open Plist Source of App A and Past below XML

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLName</key>
        <string>com.TestApp</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>testApp.linking</string>
        </array>
    </dict>
</array>

In App delegate of App A - Get Callback here

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url
  sourceApplication:(NSString *)sourceApplication annotation:(id)annotation
{
// You we get the call back here when App B will try to Open 
// sourceApplication will have the bundle ID of the App B
// [url query] will provide you the whole URL 
// [url query] with the help of this you can also pass the value from App B and get that value here 
}

Now coming to App B code -

If you just want to open App A without any input parameter

-(IBAction)openApp_A:(id)sender{

    if(![[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"testApp.linking://?"]]){
         UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"App is not available!" message:nil delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            [alert show];

        }
}

If you want to pass parameter from App B to App A then use below Code

-(IBAction)openApp_A:(id)sender{
    if(![[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"testApp.linking://?userName=abe&registered=1&Password=123abc"]]){
         UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"App is not available!" message:nil delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
            [alert show];

        }
}

Note: You can also open App with just type testApp.linking://? on safari browser

Stephie answered 19/7, 2017 at 11:9 Comment(0)
W
7

You can only launch apps that have registered a URL scheme. Then just like you open the SMS app by using sms:, you'll be able to open the app using their URL scheme.

There is a very good example available in the docs called LaunchMe which demonstrates this.

LaunchMe sample code as of 6th Nov 2017.

Wootan answered 7/1, 2009 at 11:43 Comment(2)
Updated answer. Hope that helpsWootan
Thanks; by any chance, do you belong in the iOS Developer Family chatrooms?Mechelle
A
7

Try the following code will help you to Launch an application from your application

Note: Replace the name fantacy with actual application name

NSString *mystr=[[NSString alloc] initWithFormat:@"fantacy://location?id=1"];
NSURL *myurl=[[NSURL alloc] initWithString:mystr];
[[UIApplication sharedApplication] openURL:myurl];
Anthiathia answered 4/9, 2014 at 13:54 Comment(0)
J
5

In Swift 4.1 and Xcode 9.4.1

I have two apps 1)PageViewControllerExample and 2)DelegateExample. Now i want to open DelegateExample app with PageViewControllerExample app. When i click open button in PageViewControllerExample, DelegateExample app will be opened.

For this we need to make some changes in .plist files for both the apps.

Step 1

In DelegateExample app open .plist file and add URL Types and URL Schemes. Here we need to add our required name like "myapp".

enter image description here

Step 2

In PageViewControllerExample app open .plist file and add this code

<key>LSApplicationQueriesSchemes</key>
<array>
    <string>myapp</string>
</array>

Now we can open DelegateExample app when we click button in PageViewControllerExample.

//In PageViewControllerExample create IBAction
@IBAction func openapp(_ sender: UIButton) {

    let customURL = URL(string: "myapp://")
    if UIApplication.shared.canOpenURL(customURL!) {

        //let systemVersion = UIDevice.current.systemVersion//Get OS version
        //if Double(systemVersion)! >= 10.0 {//10 or above versions
            //print(systemVersion)
            //UIApplication.shared.open(customURL!, options: [:], completionHandler: nil)
        //} else {
            //UIApplication.shared.openURL(customURL!)
        //}

        //OR

        if #available(iOS 10.0, *) {
            UIApplication.shared.open(customURL!, options: [:], completionHandler: nil)
        } else {
            UIApplication.shared.openURL(customURL!)
        }
    } else {
         //Print alert here
    }
}
Jacobite answered 19/7, 2018 at 8:4 Comment(1)
Great, it helps. Now how to pass the argument from source app to destination app?Marcum
P
4

No it's not. Besides the documented URL handlers, there's no way to communicate with/launch another app.

Parshall answered 7/1, 2009 at 4:59 Comment(0)
C
3

I also tried this a while ago (Launch iPhone Application with Identifier), but there definitely is no DOCUMENTED way to do this. :)

Cobia answered 26/12, 2011 at 22:3 Comment(2)
It is documented... Check the Apple documentation. developer.apple.com/library/IOs/#documentation/iPhone/…Fulguration
I know that URL schemes are possible, but what I asked in my question and what Jeff asked, is how to open Apps that do not support url schemes...Cobia
E
1

Swift 3 quick and paste version of @villy393 answer for :

if UIApplication.shared.canOpenURL(openURL) {
    UIApplication.shared.openURL(openURL)
} else if UIApplication.shared.canOpenURL(installUrl) 
    UIApplication.shared.openURL(installUrl)
}
Ellary answered 3/5, 2017 at 12:34 Comment(0)
V
1

A side note regarding this topic...

There is a 50 request limit for protocols that are not registered.

In this discussion apple mention that for a specific version of an app you can only query the canOpenUrl a limited number of times and will fail after 50 calls for undeclared schemes. I've also seen that if the protocol is added once you have entered this failing state it will still fail.

Be aware of this, could be useful to someone.

Viviennevivify answered 1/7, 2019 at 14:28 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.