- (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
}
if UIApplication.shared.canOpenURL(openURL) {
UIApplication.shared.openURL(openURL)
} else if UIApplication.shared.canOpenURL(installUrl)
UIApplication.shared.openURL(installUrl)
}
3. Implement the action that opens your application
现在一切都已设置完毕,因此您可以在主应用程序中编写代码来打开其他应用程序 . 这应该是这样的:
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.
}
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)
}
13 回答
这是从另一个应用程序中启动应用程序的好教程:
iOS SDK: Working with URL Schemes
并且,无法启动任意应用程序,但注册URL Schemes的本机应用程序 .
尝试以下代码将帮助您从应用程序启动应用程序
注意:将名称fantacy替换为实际应用程序名称
对于8之前的iOS,lee答案绝对正确 .
在iOS 9中,您必须将您的应用程序想要在LSApplicationQueriesSchemes键(字符串数组)下的Info.plist中查询的任何URL方案列入白名单:
在Swift 4.1和Xcode 9.4.1中
我有两个应用程序1)PageViewControllerExample和2)DelegateExample . 现在我想用PageViewControllerExample app打开DelegateExample应用程序 . 当我单击PageViewControllerExample中的打开按钮时,将打开DelegateExample应用程序 .
为此,我们需要对这两个应用程序的.plist文件进行一些更改 .
Step 1
在DelegateExample app中打开.plist文件并添加URL类型和URL方案 . 在这里,我们需要添加我们的必需名称,如“myapp” .
Step 2
在PageViewControllerExample app中打开.plist文件并添加此代码
现在,当我们点击PageViewControllerExample中的按钮时,我们可以打开DelegateExample应用程序 .
我发现编写一个可以打开另一个应用程序的应用程序很容易 .
假设我们有两个名为
FirstApp
和SecondApp
的应用程序 . 当我们打开FirstApp时,我们希望能够通过单击按钮打开SecondApp . 这样做的解决方案是:转到SecondApp的plist文件,你需要添加一个带有字符串 iOSDevTips 的 URL Schemes (当然你可以写另一个字符串 . 这取决于你) .
2 . 在FirstApp中
使用以下操作创建一个按钮:
而已 . 现在,当您可以单击FirstApp中的按钮时,它应该打开SecondApp .
正如Kevin指出的那样,URL Schemes是应用程序之间通信的唯一方式 . 所以,不,不可能推出任意应用程序 .
但是,可以启动任何注册URL方案的应用程序,无论是Apple,您的还是其他开发人员 . 文档在这里:
Communicating with Other Applications
至于启动手机,看起来你的
tel:
链接需要至少三位数才能启动手机 . 所以你不能只是拨打一个号码进入应用程序 .为此,我们需要在两个App中添加几行代码
App A: 您要从其他应用中打开哪个 .
App B :从App B你要打开 App A
代码 App A
将少量标签添加到 App A 打开Plist源App of A和过去的XML下面
在 App A 的App委托中 - 在此处获取回调
现在来 App B code -
如果您只想在没有任何输入参数的情况下打开App A.
如果要将参数从 App B 传递到 App A ,请使用以下代码
注意:您也可以在Safari浏览器中输入 testApp.linking://? 打开App
不,这不对 . 除了记录的URL处理程序之外,没有办法与/启动另一个应用程序 .
您只能启动已注册URL方案的应用程序 . 然后,就像您使用短信:打开短信应用程序,您将能够使用他们的URL方案打开应用程序 .
在一个名为LaunchMe的文档中有一个很好的例子可以证明这一点 .
LaunchMe sample code截至2017年11月6日 .
Swift 3快速粘贴@ villy393的答案:
我之前也试过这个(Launch iPhone Application with Identifier),但肯定没有文档化的方法来做到这一点 . :)
为了让您从另一个应用程序打开您的应用程序,您需要在两个应用程序中进行更改 . 以下是使用 Swift 3 和 iOS 10 更新的步骤:
1. Register your application that you want to open
通过定义应用程序的自定义和唯一URL方案来更新
Info.plist
.请注意,您的方案名称应该是唯一的,否则如果您的设备上安装了另一个具有相同URL方案名称的应用程序,那么将确定运行时哪个应用程序打开 .
2. Include the previous URL scheme in your main application
您需要指定希望应用程序能够与
UIApplication
类的canOpenURL:
方法一起使用的URL方案 . 因此,打开主应用程序的Info.plist
并将其他应用程序的URL方案添加到LSApplicationQueriesSchemes
. (在iOS 9.0中引入)3. Implement the action that opens your application
现在一切都已设置完毕,因此您可以在主应用程序中编写代码来打开其他应用程序 . 这应该是这样的:
请注意,如果您希望打开现有应用程序(从AppStore安装),则这些更改需要新版本 . 如果要打开一个应用程序,您需要首先上传新版本包括您的URL方案注册 .
In Swift
只是因为某人正在寻找快速的Swift复制和粘贴