ChatGPT解决这个技术问题 Extra ChatGPT

Launch an app from within another (iPhone)

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.


A
Adil Hussain

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.


You can check whether the application in question is installed by using UIApplication's - (BOOL)canOpenURL:(NSURL *)url
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?
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/…
This is the updated link which mentions the note about more-than one third party app registering to handle the same URL-scheme: Apple Docs
Out of date again. Updated link: developer.apple.com/documentation/uikit/…
p
patrick

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:

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).

https://i.stack.imgur.com/DWTsi.png

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.


You can see the full explanation here iosdevelopertips.com/cocoa/…
@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 me
Please give me more some detail of your problem
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 iOSDevTips
See also KIO's answer below (stackoverflow.com/a/33763449/394969). FirstApp needs to add LSApplicationQueriesSchemes to its Info.plist to be able to open SecondApp.
K
KIO

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):

https://i.stack.imgur.com/txMUq.png


The best answer should be a merge between @KIO and villy393
S
Sunil Targe

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)
        }

c
choofie

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.

https://i.stack.imgur.com/OJM5v.png

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)

https://i.stack.imgur.com/ducnT.png

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.


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.
N
Nickolas

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.


A
Alok

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


b
btmanikandan

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];

l
lostInTransit

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.


Updated answer. Hope that helps
Thanks; by any chance, do you belong in the iOS Developer Family chatrooms?
N
Naresh

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".

https://i.stack.imgur.com/gSwM7.png

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
    }
}

Great, it helps. Now how to pass the argument from source app to destination app?
L
Lily Ballard

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


C
Community

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


It is documented... Check the Apple documentation. developer.apple.com/library/IOs/#documentation/iPhone/…
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...
D
Duncan Hoggan

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.


c
cdescours

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)
}