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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/419119/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 15:30:33  来源:igfitidea点击:

Launch an app from within another (iPhone)

iosiphonedeep-linkingopenurl

提问by

Is it possible to launch any arbitrary iPhone application from within another app?, For examplein my application if I want the user to push a button and launch right into the Phone app (close the current app, open the Phone app).

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

would this be possible? I know this can be done for making phone calls with the tel URL link, but I want to instead just have the Phone app launch without dialing any specific number.

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

回答by Gordon Wilson

As Kevin points out, URL Schemes are the only way to communicate between apps. So, no, it's not possible to launch arbitrary apps.

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

But it is possible to launch any app that registers a URL Scheme, whether it's Apple's, yours, or another developer's. The docs are here:

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

Communicating with Other Applications

与其他应用程序通信

As for launching the phone, looks like your tel:link needs to have least three digits before the phone will launch. So you can't just drop into the app without dialing a number.

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

回答by lee

I found that it's easy to write an app that can open another app.

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

Let's assume that we have two apps called FirstAppand 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:

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

  1. In SecondApp

    Go to the plist file of SecondApp and you need to add a URL Schemeswith a string iOSDevTips(of course you can write another string.it's up to you).

  1. 在第二个应用程序中

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

enter image description here

在此处输入图片说明

2 . In FirstApp

2 . 在第一应用

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.

就是这样。现在,当您可以单击 FirstApp 中的按钮时,它应该会打开 SecondApp。

回答by KIO

The lee answer is absolutely correct for iOS prior to 8.

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

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

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

enter image description here

在此处输入图片说明

回答by villy393

In Swift

在斯威夫特

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

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

回答by choofie

In order to let you open your application from another, you'll need to make changes in both applications. Here are the steps using Swift 3with iOS 10update:

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

1. Register your application that you want to open

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

Update the Info.plistby defining your application's custom and unique URL Scheme.

更新Info.plist定义应用程序的定制和独特的URL方案。

enter image description here

在此处输入图片说明

Note that your scheme name should be unique, otherwise if you have another application with the same URL scheme name installed on your device, then this will be determined runtime which one gets opened.

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

2. Include the previous URL scheme in your main application

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

You'll need to specify the URL scheme you want the app to be able to use with the canOpenURL:method of the UIApplicationclass. So open the main application's Info.plistand add the other application's URL scheme to LSApplicationQueriesSchemes. (Introduced in iOS 9.0)

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

enter image description here

在此处输入图片说明

3. Implement the action that opens your application

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

Now everything is set up, so you're good to write your code in your main application that opens your other app. This should looks something like this:

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

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

Note that these changes requires a new release if you want your existing app (installed from AppStore) to open. If you want to open an application that you've already released to Apple AppStore, then you'll need to upload a new version first that includes your URL scheme registration.

请注意,如果您希望打开现有应用程序(从 AppStore 安装),则这些更改需要新版本。如果要打开已发布到 Apple AppStore 的应用程序,则需要先上传包含 URL 方案注册的新版本。

回答by Nickolas

Here is a good tutorial for launching application from within another app:
iOS SDK: Working with URL Schemes
And, it is not possible to launch arbitrary application, but the native applications which registered the URL Schemes.

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

回答by Alok

To achieve this we need to add few line of Code in both App

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

App A:Which you want to open from another App. (Source)

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

App B: From App B you want to open App A(Destination)

App B: 从 App B 你想打开App A (Destination)

Code for App A

应用程序 A 的代码

Add few tags into the Plist of App AOpen Plist Source of App A and Past below XML

App A的 Plist 中添加几个标签 ,在 XML 下方打开 App A 和 Past 源

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

In App delegate of App A- Get Callback here

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

Now coming to App B code-

现在来到App B 代码-

If you just want to open App A without any input parameter

如果你只是想打开 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];

        }
}

If you want to pass parameter from App Bto App Athen use below Code

如果要将参数从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];

        }
}

Note: You can also open App with just type testApp.linking://?on safari browser

注意:您也可以只输入testApp.linking://? 在 safari 浏览器上

回答by btmanikandan

Try the following code will help you to Launch an application from your application

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

Note: Replace the name fantacy with actual application name

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

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

回答by lostInTransit

You can only launch apps that have registered a URL scheme. Then just like you open the SMS app by using sms:, you'll be able to open the app using their URL scheme.

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

There is a very good example available in the docs called LaunchMe which demonstrates this.

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

LaunchMe sample codeas of 6th Nov 2017.

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

回答by Lily Ballard

No it's not. Besides the documented URL handlers, there's no way to communicate with/launch another app.

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