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

165 iphone deep-linking openurl ios

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

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

Gor*_*son 80

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

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

与其他应用程序通信

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

  • 您可以使用UIApplication的 - (BOOL)canOpenURL:(NSURL*)url检查相关应用程序是否已安装 (7认同)
  • 注意:如果多个第三方应用程序注册以处理相同的URL方案,则目前没有确定将为该方案提供哪个应用程序的过程.参考:http://developer.apple.com/library/ios/#documentation/iPhone/Conceptual/iPhoneOSProgrammingGuide/AdvancedAppTricks/AdvancedAppTricks.html (3认同)
  • 这是更新的链接,其中提到有关多于一个第三方应用程序注册以处理相同URL方案的说明:[Apple Docs](https://developer.apple.com/library/archive/documentation/iPhone/Conceptual /iPhoneOSProgrammingGuide/Inter-AppCommunication/Inter-AppCommunication.html#//apple_ref/doc/uid/TP40007072-CH6-SW7) (2认同)

lee*_*lee 75

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

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

  1. 在SecondApp中

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

在此输入图像描述

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

}
Run Code Online (Sandbox Code Playgroud)

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

  • 你可以在这里看到完整的解释http://iosdevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html (2认同)
  • 如果无法实现此功能,请在“FirstApp”的“Info.plist”中添加名为“LSApplicationQueriesSchemes”的字符串数组。然后添加您的应用程序想要打开的方案,在本例中“Item 0”将是“iOSDevTips” (2认同)

KIO*_*KIO 27

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

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

在此输入图像描述

  • 最好的答案应该是@KIO 和villy393 之间的合并 (2认同)

vil*_*393 18

在斯威夫特

只是因为某人正在寻找快速的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)      
}
Run Code Online (Sandbox Code Playgroud)


cho*_*fie 13

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

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

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

在此输入图像描述

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

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

您需要指定希望应用程序能够与类的canOpenURL:方法一起使用的URL方案UIApplication.因此,打开主应用程序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.
}
Run Code Online (Sandbox Code Playgroud)

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


Nic*_*las 11

以下是从另一个应用程序启动应用程序的好教程:
iOS SDK:使用URL方案
并且,无法启动任意应用程序,但是注册URL方案的本机应用程序.


Alo*_*lok 9

为实现这一目标,我们需要在两个App中添加几行代码

应用程序A:您要从其他应用程序打开哪个.(资源)

应用B:从应用B你想要打开应用A (目的地)

App A的代码

将少量标签添加到应用程序的Plist中,将App App A的 Open Plist源和过去的XML下载

<key>CFBundleURLTypes</key>
<array>
    <dict>
        <key>CFBundleURLName</key>
        <string>com.TestApp</string>
        <key>CFBundleURLSchemes</key>
        <array>
            <string>testApp.linking</string>
        </array>
    </dict>
</array>
Run Code Online (Sandbox Code Playgroud)

在应用程序代理应用程序一个 -这里获取回调

- (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 
}
Run Code Online (Sandbox Code Playgroud)

现在来到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];

        }
}
Run Code Online (Sandbox Code Playgroud)

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

        }
}
Run Code Online (Sandbox Code Playgroud)

注意:您也可以使用testApp.linking://类型打开App 在safari浏览器上


los*_*sit 7

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

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

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


btm*_*dan 7

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

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

NSString *mystr=[[NSString alloc] initWithFormat:@"fantacy://location?id=1"];
NSURL *myurl=[[NSURL alloc] initWithString:mystr];
[[UIApplication sharedApplication] openURL:myurl];
Run Code Online (Sandbox Code Playgroud)


iOS*_*iOS 5

在 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>
Run Code Online (Sandbox Code Playgroud)

现在,当我们单击 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
    }
}
Run Code Online (Sandbox Code Playgroud)


Lil*_*ard 4

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