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

Launch an app from within another (iPhone)

提问人: 提问时间:1/7/2009 最后编辑:Naresh 更新时间:11/8/2021 访问量:238397

问:

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

这可能吗?我知道这可以用于使用电话 URL 链接拨打电话,但我想在不拨打任何特定号码的情况下启动电话应用程序。

iOS iPhone Cocoa-Touch 深度链接 OpenURL

评论


答:

4赞 Lily Ballard 1/7/2009 #1

不,不是。除了记录的 URL 处理程序外,无法与其他应用通信/启动其他应用。

84赞 Gordon Wilson 1/7/2009 #2

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

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

为应用定义自定义 URL 方案

至于启动手机,在手机启动之前,您的链接至少需要有三位数字。因此,您不能在不拨打号码的情况下直接进入应用程序。tel:

评论

9赞 Willster 2/1/2011
您可以使用 UIApplication 的 - (BOOL)canOpenURL:(NSURL *)url 检查是否安装了相关应用程序
1赞 eggie5 7/26/2011
如果 2 个应用注册相同的 URL 处理程序,然后调用该 URL,会发生什么情况?我知道在 Android 中,您将看到一个列表,以便您可以选择要运行的两个中的哪一个。ios如何处理这个问题?
4赞 Futur 11/30/2011
注意:如果多个第三方应用注册以处理相同的网址方案,则目前没有确定哪个应用将获得该方案的流程。参考: developer.apple.com/library/ios/#documentation/iPhone/...
3赞 Sindri Jóelsson 6/6/2018
这是更新的链接,其中提到了有关多个第三方应用程序注册以处理相同 URL 方案的说明:Apple Docs
1赞 Dave Nottage 11/11/2019
又过时了。更新链接: developer.apple.com/documentation/uikit/...
7赞 lostInTransit 1/7/2009 #3

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

文档中有一个很好的示例,称为 LaunchMe,它演示了这一点。

截至 2017 年 11 月 6 日的 LaunchMe 示例代码

评论

0赞 lostInTransit 11/6/2017
更新了答案。希望能有所帮助
0赞 Eenvincible 11/6/2017
谢谢;无论如何,您属于 iOS 开发人员家庭聊天室吗?
11赞 Nickolas 12/26/2011 #4

下面是一个很好的教程,用于从另一个应用程序中启动应用程序: iOS SDK:
使用 URL 方案
而且,不可能启动任意应用程序,而是启动注册 URL 方案的本机应用程序。

3赞 foebe 12/27/2011 #5

我不久前也尝试过这个(使用标识符启动iPhone应用程序),但绝对没有记录在案的方法可以做到这一点。:)

评论

2赞 Moszi 5/31/2012
它被记录在案......查看 Apple 文档。developer.apple.com/library/IOs/#documentation/iPhone/......
1赞 foebe 6/1/2012
我知道 URL 方案是可能的,但我在我的问题和 Jeff 提出的是如何打开不支持 url 方案的应用程序......
79赞 lee 6/4/2014 #6

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

假设我们有两个名为 和 的应用程序。当我们打开 FirstApp 时,我们希望能够通过单击按钮打开 SecondApp。执行此操作的解决方案是:FirstAppSecondApp

  1. 在 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。

评论

2赞 youssman 11/8/2014
您可以在此处查看完整的解释 iosdevelopertips.com/cocoa/...
1赞 srinivas n 8/24/2015
@Lee我正在关注您的帖子,Safari可以打开应用程序,但另一个应用程序无法打开(按钮按下方法)否则执行块,这是什么问题,您可以帮帮我吗
0赞 lee 8/24/2015
请给我更多关于你的问题的细节
2赞 11/5/2016
无法实现此功能的人,请在 .然后添加您的应用要打开的方案,在本例中为Info.plistFirstAppItem 0iOSDevTips
1赞 John Erck 1/13/2017
另请参阅下面 KIO 的回答(stackoverflow.com/a/33763449/394969)。FirstApp 需要添加到其 Info.plist 中才能打开 SecondApp。LSApplicationQueriesSchemes
7赞 btmanikandan 9/4/2014 #7

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

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

NSString *mystr=[[NSString alloc] initWithFormat:@"fantacy://location?id=1"];
NSURL *myurl=[[NSURL alloc] initWithString:mystr];
[[UIApplication sharedApplication] openURL:myurl];
31赞 Igor Kovryzhkin 11/18/2015 #8

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

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

enter image description here

评论

2赞 Gustavo Barbosa 12/24/2016
最好的答案应该是 @KIO 和 villy393 之间的合并
23赞 villy393 1/19/2016 #9

在 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)
        }
1赞 cdescours 5/3/2017 #10

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

if UIApplication.shared.canOpenURL(openURL) {
    UIApplication.shared.openURL(openURL)
} else if UIApplication.shared.canOpenURL(installUrl) 
    UIApplication.shared.openURL(installUrl)
}
17赞 choofie 5/25/2017 #11

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

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

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

enter image description here

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

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

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

enter image description here

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 方案注册的新版本。

评论

1赞 Shailesh 7/11/2020
这是正确的答案。我注意到仅添加“LSApplicationQueriesSchemes”不起作用,还需要在info.plist中添加“URL Scheme”。
0赞 S.S.D 10/21/2023
这个答案是100%正确的。在 “if UIApplication.shared.canOpenURL(appURL)” 的 else 部分,您可以添加以下内容: let appStoreURL = URL(string: “apps.apple.com/app/app-name/id123456789”) // 替换为应用的实际 App Store URL if let appStoreURL = appStoreURL { UIApplication.shared.open(appStoreURL, options: [:], completionHandler: nil) }
9赞 Alok 7/19/2017 #12

为了实现这一点,我们需要在两个应用程序中添加几行代码

应用程序 A:您想从其他应用程序打开。

应用程序 B:从应用程序 B 打开应用程序 A(目标)

应用 A 的代码

在 App A 的 Plist 中添加几个标签 Open Plist App 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 代码 -

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

        }
}

如果要将参数从应用程序 B 传递到应用程序 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://?打开应用程序

5赞 Naresh 7/19/2018 #13

在 Swift 4.1 和 Xcode 9.4.1 中

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

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

第 1 步

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

enter image description here

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

评论

1赞 Vigneswaran A 2/5/2020
太好了,这很有帮助。现在如何将参数从源应用程序传递到目标应用程序?
1赞 Duncan Hoggan 7/1/2019 #14

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

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

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

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