首页 文章

从另一个(iPhone)启动应用程序

提问于
浏览
151

如果我希望用户按下按钮并启动到手机应用程序(关闭当前应用程序,打开手机应用程序),是否可以在我的应用程序中从另一个应用程序中启动任意iPhone应用程序?, For example .

这有可能吗?我知道这可以通过tel URL链接拨打电话,但我想改为启动Phone应用而不拨打任何特定号码 .

13 回答

  • 2

    这是从另一个应用程序中启动应用程序的好教程:
    iOS SDK: Working with URL Schemes
    并且,无法启动任意应用程序,但注册URL Schemes的本机应用程序 .

  • 6

    尝试以下代码将帮助您从应用程序启动应用程序

    注意:将名称fantacy替换为实际应用程序名称

    NSString *mystr=[[NSString alloc] initWithFormat:@"fantacy://location?id=1"];
    NSURL *myurl=[[NSURL alloc] initWithString:mystr];
    [[UIApplication sharedApplication] openURL:myurl];
    
  • 1

    对于8之前的iOS,lee答案绝对正确 .

    在iOS 9中,您必须将您的应用程序想要在LSApplicationQueriesSchemes键(字符串数组)下的Info.plist中查询的任何URL方案列入白名单:

    enter image description here

  • 0

    在Swift 4.1和Xcode 9.4.1中

    我有两个应用程序1)PageViewControllerExample和2)DelegateExample . 现在我想用PageViewControllerExample app打开DelegateExample应用程序 . 当我单击PageViewControllerExample中的打开按钮时,将打开DelegateExample应用程序 .

    为此,我们需要对这两个应用程序的.plist文件进行一些更改 .

    Step 1

    在DelegateExample app中打开.plist文件并添加URL类型和URL方案 . 在这里,我们需要添加我们的必需名称,如“myapp” .

    enter image description here

    Step 2

    在PageViewControllerExample app中打开.plist文件并添加此代码

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

    现在,当我们点击PageViewControllerExample中的按钮时,我们可以打开DelegateExample应用程序 .

    //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
        }
    }
    
  • 22

    我发现编写一个可以打开另一个应用程序的应用程序很容易 .

    假设我们有两个名为 FirstAppSecondApp 的应用程序 . 当我们打开FirstApp时,我们希望能够通过单击按钮打开SecondApp . 这样做的解决方案是:

    • 在SecondApp中

    转到SecondApp的plist文件,你需要添加一个带有字符串 iOSDevTipsURL Schemes (当然你可以写另一个字符串 . 这取决于你) .

    enter image description here

    2 . 在FirstApp中

    使用以下操作创建一个按钮:

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

    而已 . 现在,当您可以单击FirstApp中的按钮时,它应该打开SecondApp .

  • 6

    正如Kevin指出的那样,URL Schemes是应用程序之间通信的唯一方式 . 所以,不,不可能推出任意应用程序 .

    但是,可以启动任何注册URL方案的应用程序,无论是Apple,您的还是其他开发人员 . 文档在这里:

    Communicating with Other Applications

    至于启动手机,看起来你的 tel: 链接需要至少三位数才能启动手机 . 所以你不能只是拨打一个号码进入应用程序 .

  • 7

    为此,我们需要在两个App中添加几行代码

    App A: 您要从其他应用中打开哪个 .

    App B :从App B你要打开 App A

    代码 App A

    将少量标签添加到 App A 打开Plist源App of A和过去的XML下面

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

    App A 的App委托中 - 在此处获取回调

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

    现在来 App B code -

    如果您只想在没有任何输入参数的情况下打开App A.

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

    如果要将参数从 App B 传递到 App A ,请使用以下代码

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

    注意:您也可以在Safari浏览器中输入 testApp.linking://? 打开App

  • 70

    不,这不对 . 除了记录的URL处理程序之外,没有办法与/启动另一个应用程序 .

  • 14

    您只能启动已注册URL方案的应用程序 . 然后,就像您使用短信:打开短信应用程序,您将能够使用他们的URL方案打开应用程序 .

    在一个名为LaunchMe的文档中有一个很好的例子可以证明这一点 .

    LaunchMe sample code截至2017年11月6日 .

  • 10

    Swift 3快速粘贴@ villy393的答案:

    if UIApplication.shared.canOpenURL(openURL) {
        UIApplication.shared.openURL(openURL)
    } else if UIApplication.shared.canOpenURL(installUrl) 
        UIApplication.shared.openURL(installUrl)
    }
    
  • 9

    我之前也试过这个(Launch iPhone Application with Identifier),但肯定没有文档化的方法来做到这一点 . :)

  • 3

    为了让您从另一个应用程序打开您的应用程序,您需要在两个应用程序中进行更改 . 以下是使用 Swift 3iOS 10 更新的步骤:

    1. Register your application that you want to open

    通过定义应用程序的自定义和唯一URL方案来更新 Info.plist .

    enter image description here

    请注意,您的方案名称应该是唯一的,否则如果您的设备上安装了另一个具有相同URL方案名称的应用程序,那么将确定运行时哪个应用程序打开 .

    2. Include the previous URL scheme in your main application

    您需要指定希望应用程序能够与 UIApplication 类的 canOpenURL: 方法一起使用的URL方案 . 因此,打开主应用程序的 Info.plist 并将其他应用程序的URL方案添加到 LSApplicationQueriesSchemes . (在iOS 9.0中引入)

    enter image description here

    3. Implement the action that opens your application

    现在一切都已设置完毕,因此您可以在主应用程序中编写代码来打开其他应用程序 . 这应该是这样的:

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

    请注意,如果您希望打开现有应用程序(从AppStore安装),则这些更改需要新版本 . 如果要打开一个应用程序,您需要首先上传新版本包括您的URL方案注册 .

  • 76

    In Swift

    只是因为某人正在寻找快速的Swift复制和粘贴

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

相关问题