【问题标题】:Launch an app from within another (iPhone)从另一个 (iPhone) 中启动应用程序
【发布时间】:2010-09-29 23:11:55
【问题描述】:

是否可以从另一个应用程序中启动任意 iPhone 应用程序?,例如 如果我希望用户按下按钮并直接启动到电话应用程序(关闭当前应用,打开电话应用)

这可能吗?我知道这可以通过 tel URL 链接拨打电话来完成,但我只想启动电话应用程序而不拨打任何特定号码。

【问题讨论】:

    标签: ios iphone cocoa-touch deep-linking openurl


    【解决方案1】:

    正如 Kevin 所指出的,URL 方案是应用程序之间进行通信的唯一方式。所以,不,不可能启动任意应用程序。

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

    Defining a Custom URL Scheme for Your App

    至于启动手机,看起来您的tel: 链接至少需要三位数才能启动手机。所以你不能不拨号码就直接进入应用程序。

    【讨论】:

    • 您可以使用 UIApplication 的 - (BOOL)canOpenURL:(NSURL *)url 来检查是否安装了有问题的应用程序
    • 如果 2 个应用注册相同的 url 处理程序然后调用 url 会发生什么?我知道在 Android 中,您将看到一个列表,以便您可以选择要运行的两个列表中的哪一个。 ios 是如何处理的?
    • 注意:如果有多个第三方应用注册处理相同的 URL 方案,则目前没有确定哪个应用将获得该方案的过程。参考:developer.apple.com/library/ios/#documentation/iPhone/…
    • 这是更新的链接,其中提到了有关多个第三方应用程序注册以处理相同的 URL 方案的注释:Apple Docs
    • 又过时了。更新链接:developer.apple.com/documentation/uikit/…
    【解决方案2】:

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

    假设我们有两个应用程序,分别名为 FirstAppSecondApp。当我们打开 FirstApp 时,我们希望能够通过单击按钮打开 SecondApp。解决方法是:

    1. 在第二个应用程序中

      进入SecondApp的plist文件,你需要添加一个URL Schemes和一个字符串iOSDevTips(当然你可以写另一个字符串,由你决定) .

    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。

    【讨论】:

    • 你可以在这里看到完整的解释iosdevelopertips.com/cocoa/…
    • @Lee 我正在关注您的帖子,safari 可以打开应用程序但另一个应用程序未打开(按钮按下方法)执行 else 块,请问您有什么问题可以帮助我
    • 请详细说明您的问题
    • 不能让它工作的人,在FirstAppInfo.plist 中添加一个名为“LSApplicationQueriesSchemes”的字符串数组。然后添加您的应用要打开的方案,在这种情况下,Item 0 将是iOSDevTips
    • 另见 KIO 在下面的回答 (stackoverflow.com/a/33763449/394969)。 FirstApp 需要在其 Info.plist 中添加LSApplicationQueriesSchemes 才能打开 SecondApp。
    【解决方案3】:

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

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

    【讨论】:

    • 最好的答案应该是@KIO和villy393的合并
    【解决方案4】:

    在 Swift 中

    以防万一有人正在寻找快速的 Swift 复制和粘贴。

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

    【讨论】:

      【解决方案5】:

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

      1.注册您要打开的应用程序

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

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

      2。在您的主应用程序中包含以前的 URL 方案

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

      3.实施打开应用程序的操作

      现在一切都已设置好,因此您可以在打开其他应用的主应用中编写代码。这应该看起来像这样:

      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 安装)打开,这些更改需要新版本。如果您要打开已发布到 Apple AppStore 的应用程序,则需要先上传包含您的 URL 方案注册的新版本。

      【讨论】:

      • 这是正确的答案。我注意到只添加“LSApplicationQueriesSchemes”不起作用,还需要在info.plist中添加“URL Scheme”。
      【解决方案6】:

      这是从另一个应用程序中启动应用程序的好教程:
      iOS SDK: Working with URL Schemes
      而且,不能启动任意应用程序,而是注册URL Schemes 的本机应用程序。

      【讨论】:

        【解决方案7】:

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

        App A:您想从另一个 App 打开。 (来源)

        App B:您要从 App B 打开 App A (目标)

        应用 A

        的代码

        App A的Plist中添加一些标签 在 XML 下打开 App A 和 Past 的 Plist 源

        <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 代码 -

        如果你只是想在没有任何输入参数的情况下打开 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

        【讨论】:

          【解决方案8】:

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

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

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

          【讨论】:

            【解决方案9】:

            您只能启动已注册 URL 方案的应用程序。然后就像您使用 sms: 打开 SMS 应用程序一样,您将能够使用其 URL 方案打开该应用程序。

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

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

            【讨论】:

            • 更新了答案。希望有帮助
            • 谢谢;顺便说一句,你属于 iOS 开发者家庭聊天室吗?
            【解决方案10】:

            不,不是。除了记录在案的 URL 处理程序之外,没有办法与/启动另一个应用程序进行通信。

            【讨论】:

              【解决方案11】:

              在 Swift 4.1 和 Xcode 9.4.1 中

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

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

              第 1 步

              在 DelegateExample 应用程序中打开 .plist 文件并添加 URL 类型和 URL 方案。在这里,我们需要添加我们所需的名称,例如“myapp”。

              第 2 步

              在 PageViewControllerExample 应用中打开 .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
                  }
              }
              

              【讨论】:

              • 太好了,它有帮助。现在如何将参数从源应用程序传递到目标应用程序?
              【解决方案12】:

              我不久前也尝试过 (Launch iPhone Application with Identifier),但绝对没有文档化的方法可以做到这一点。 :)

              【讨论】:

              【解决方案13】:

              关于这个话题的旁注...

              对于未注册的协议有 50 个请求限制。

              In this discussionapple 提到,对于特定版本的应用程序,您只能查询 canOpenUrl 有限的次数,并且在未声明的方案调用 50 次后将失败。我还看到,如果在您进入此失败状态后添加协议,它仍然会失败。

              注意这一点,可能对某人有用。

              【讨论】:

                【解决方案14】:

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

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

                【讨论】:

                  猜你喜欢
                  • 1970-01-01
                  • 1970-01-01
                  • 2010-12-06
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 1970-01-01
                  • 2014-11-05
                  相关资源
                  最近更新 更多