在另一个应用程序内启动任意iPhone应用程序,这是可能的吗? 例如 如果我想让用户按一个按钮并直接进入电话应用程序(关闭当前应用程序,打开电话应用程序)。
这种操作是否可行?我知道可以通过tel URL链接来进行电话呼叫,但我想只是启动电话应用程序而不拨打任何特定号码。
在另一个应用程序内启动任意iPhone应用程序,这是可能的吗? 例如 如果我想让用户按一个按钮并直接进入电话应用程序(关闭当前应用程序,打开电话应用程序)。
这种操作是否可行?我知道可以通过tel URL链接来进行电话呼叫,但我想只是启动电话应用程序而不拨打任何特定号码。
正如Kevin所指出的,URL Scheme是应用程序之间通信的唯一方式。 因此,不,无法启动任意应用程序。
但是,只要它是苹果公司、您或其他开发人员注册了URL Scheme,就可以启动任何应用程序。 文档在这里:
至于启动电话,看起来您的tel:
链接需要在启动电话之前至少拨打三个数字。 因此,您不能只是进入应用程序而不拨打号码。
我发现编写一个可以打开另一个应用程序的应用程序很容易。
假设我们有两个应用程序,分别称为 FirstApp
和 SecondApp
。当我们打开FirstApp时,我们希望能够通过点击按钮打开SecondApp。实现此目标的解决方案是:
在SecondApp中
转到SecondApp的plist文件,并添加一个名为 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];
}
}
就这样了。现在,当您点击第一个应用程序中的按钮时,它应该打开第二个应用程序。
在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)
}
Info.plist
。
请注意,您的 scheme 名称应该是唯一的,否则如果您的设备上安装了另一个具有相同 URL scheme 名称的应用程序,则在运行时将确定打开哪个应用程序。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.
}
这里有一个关于从另一个应用程序发起应用程序的好教程:
iOS SDK: 使用URL Scheme
而且,不可能启动任意应用程序,只能启动已注册了URL Scheme的本机应用程序。
<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleURLName</key>
<string>com.TestApp</string>
<key>CFBundleURLSchemes</key>
<array>
<string>testApp.linking</string>
</array>
</dict>
</array>
- (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
}
-(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];
}
}
NSString *mystr=[[NSString alloc] initWithFormat:@"fantacy://location?id=1"];
NSURL *myurl=[[NSURL alloc] initWithString:mystr];
[[UIApplication sharedApplication] openURL:myurl];
步骤2
在PageViewControllerExample应用程序中打开.plist文件并添加以下代码
<key>LSApplicationQueriesSchemes</key>
<array>
<string>myapp</string>
</array>
//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
}
}