Launch an app from within another (iPhone)

后端 未结 14 1637
没有蜡笔的小新
没有蜡笔的小新 2020-11-22 01:14

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 an

相关标签:
14条回答
  • 2020-11-22 01:15

    In Swift

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

    if let url = NSURL(string: "app://") where UIApplication.sharedApplication().canOpenURL(url) {
                UIApplication.sharedApplication().openURL(url)
    } else if let itunesUrl = NSURL(string: "https://itunes.apple.com/itunes-link-to-app") where UIApplication.sharedApplication().canOpenURL(itunesUrl) {
                UIApplication.sharedApplication().openURL(itunesUrl)      
    }
    
    0 讨论(0)
  • 2020-11-22 01:20

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

    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
        }
    }
    
    0 讨论(0)
  • 2020-11-22 01:29

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

    0 讨论(0)
  • 2020-11-22 01:34

    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.

    0 讨论(0)
  • 2020-11-22 01:34

    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];
    
    0 讨论(0)
  • 2020-11-22 01:34

    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.

    0 讨论(0)
提交回复
热议问题