提问人: 提问时间:1/7/2009 最后编辑:Naresh 更新时间:11/8/2021 访问量:238397
从另一个 (iPhone) 中启动应用程序
Launch an app from within another (iPhone)
问:
是否可以从另一个应用程序中启动任何任意iPhone应用程序?例如,在我的应用程序中,如果我希望用户按下按钮并直接启动到电话应用程序(关闭当前应用程序,打开电话应用程序)。
这可能吗?我知道这可以用于使用电话 URL 链接拨打电话,但我想在不拨打任何特定号码的情况下启动电话应用程序。
答:
不,不是。除了记录的 URL 处理程序外,无法与其他应用通信/启动其他应用。
正如 Kevin 所指出的,URL 方案是应用程序之间通信的唯一方式。所以,不,不可能启动任意应用程序。
但是,可以启动任何注册 URL 方案的应用程序,无论是 Apple、您的还是其他开发人员的应用程序。文档在这里:
至于启动手机,在手机启动之前,您的链接至少需要有三位数字。因此,您不能在不拨打号码的情况下直接进入应用程序。tel:
评论
您只能启动已注册 URL 方案的应用。然后,就像使用 sms: 打开 SMS 应用程序一样,您将能够使用其 URL 方案打开应用程序。
文档中有一个很好的示例,称为 LaunchMe,它演示了这一点。
截至 2017 年 11 月 6 日的 LaunchMe 示例代码。
评论
下面是一个很好的教程,用于从另一个应用程序中启动应用程序: iOS SDK:
使用 URL 方案
而且,不可能启动任意应用程序,而是启动注册 URL 方案的本机应用程序。
我不久前也尝试过这个(使用标识符启动iPhone应用程序),但绝对没有记录在案的方法可以做到这一点。:)
评论
我发现编写一个可以打开另一个应用程序的应用程序很容易。
假设我们有两个名为 和 的应用程序。当我们打开 FirstApp 时,我们希望能够通过单击按钮打开 SecondApp。执行此操作的解决方案是:FirstApp
SecondApp
在 SecondApp 中
进入SecondApp的plist文件,需要添加一个带有字符串iOSDevTips的URL 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];
}
}
就是这样。现在,当您可以单击 FirstApp 中的按钮时,它应该打开 SecondApp。
评论
Info.plist
FirstApp
Item 0
iOSDevTips
LSApplicationQueriesSchemes
尝试以下代码将帮助您从应用程序启动应用程序
注意:将名称 fantacy 替换为实际应用程序名称
NSString *mystr=[[NSString alloc] initWithFormat:@"fantacy://location?id=1"];
NSURL *myurl=[[NSURL alloc] initWithString:mystr];
[[UIApplication sharedApplication] openURL:myurl];
lee 答案对于 8 之前的 iOS 来说是绝对正确的。
在 iOS 9+ 中,必须在 LSApplicationQueriesSchemes 键(字符串数组)下的 Info.plist 中将应用要查询的任何 URL 方案列入白名单:
评论
在 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)
}
Swift 3 快速粘贴版@villy393答案:
if UIApplication.shared.canOpenURL(openURL) {
UIApplication.shared.openURL(openURL)
} else if UIApplication.shared.canOpenURL(installUrl)
UIApplication.shared.openURL(installUrl)
}
为了让您从另一个应用程序打开您的应用程序,您需要在两个应用程序中进行更改。以下是将 Swift 3 与 iOS 10 更新结合使用的步骤:
1. 注册要打开的应用程序
通过定义应用程序的自定义和唯一 URL 方案来更新Info.plist
请注意,您的方案名称应该是唯一的,否则,如果您的设备上安装了另一个具有相同 URL 方案名称的应用程序,那么这将决定在运行时打开哪个应用程序。
2. 在主应用程序中包含以前的 URL 方案
需要指定希望应用能够与类的方法一起使用的 URL 方案。因此,打开主应用程序的 并将其他应用程序的 URL 方案添加到 .(在 iOS 9.0 中引入)canOpenURL:
UIApplication
Info.plist
LSApplicationQueriesSchemes
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 方案注册的新版本。
评论
为了实现这一点,我们需要在两个应用程序中添加几行代码
应用程序 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®istered=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://?打开应用程序
在 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>
现在,当我们单击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
}
}
评论
关于这个话题的旁注......
未注册的协议有 50 个请求限制。
在这次讨论中,苹果提到,对于应用程序的特定版本,您只能查询有限的次数,并且在调用未声明的方案 50 次后将失败。我还看到,如果在进入此失败状态后添加协议,它仍然会失败。canOpenUrl
请注意这一点,可能对某人有用。
评论