ChatGPT解决这个技术问题 Extra ChatGPT

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

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

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


A
Adil Hussain

正如 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/…
p
patrick

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

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

在SecondApp 中进入SecondApp 的plist 文件,你需要添加一个带有字符串iOSDevTips 的URL Schemes(当然你也可以写另一个字符串。这取决于你)。

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

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 需要将 LSApplicationQueriesSchemes 添加到其 Info.plist 才能打开 SecondApp。
K
KIO

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

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

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


最好的答案应该是 @KIO 和 villy393 之间的合并
S
Sunil Targe

在斯威夫特

以防万一有人正在寻找快速的 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)
        }

c
choofie

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

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

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

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

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

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

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

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

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”。
N
Nickolas

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


A
Alok

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

应用 A:您想从另一个应用打开的应用。 (资源)

应用 B:从应用 B 你要打开应用 A(目标)

应用程序 A 的代码

在 App A 的 Plist 中添加几个标签 打开 App A 的 Plist Source 和 XML 下面的 Past

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

        }
}

注意:您也可以通过键入 testApp.linking://?在 Safari 浏览器上


b
btmanikandan

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

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

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

l
lostInTransit

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

在名为 LaunchMe 的文档中有一个非常好的示例,它演示了这一点。

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


更新了答案。希望有帮助
谢谢;无论如何,你属于 iOS 开发者家庭聊天室吗?
N
Naresh

在 Swift 4.1 和 Xcode 9.4.1

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

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

步骤1

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

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

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

太好了,它有帮助。现在如何将参数从源应用程序传递到目标应用程序?
L
Lily Ballard

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


C
Community

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


它被记录在案......检查Apple文档。 developer.apple.com/library/IOs/#documentation/iPhone/…
我知道 URL 方案是可能的,但我在我的问题和 Jeff 中提出的问题是如何打开不支持 url 方案的应用程序......
D
Duncan Hoggan

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

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

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

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


c
cdescours

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

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