iOS APP打开其他应用

时间:2023-03-09 03:30:13
iOS APP打开其他应用

1、限于iOS的沙盒机制,一般的app都只在沙盒内操作运行,针对app之间的通讯苹果还是给出了一些解决方案的。

最常见的场景就是在一个APP中打开另一个APP。

核心就是一个API,通过制定一个一个URL,打开一个app

            [[UIApplication sharedApplication] openURL:url];

2、不过在这之前,我们还需要做一些配置。我们需要在info.plist里配置需要打开的app的URL scheme,一些常用的app scheme自行百度。

    <key>LSApplicationQueriesSchemes</key>
<array>
<string>wechat</string>
<string>weixin</string>
<string>twitter</string>
</array>

在打开URL的时候要先判断URL是否有效。然后才能做跳转操作

写一个跳转的判断方法。

- (void)checkWhetherHasInstalledAppWithUrlSchemes:(NSString *)urlSchemes {
NSURL *url = [NSURL URLWithString:urlSchemes];
if ([[UIApplication sharedApplication] canOpenURL:url]) {
NSLog(@"%@ 有效" ,urlSchemes);
[[UIApplication sharedApplication] openURL:url];
}else {
NSLog(@"%@ 无效" ,urlSchemes);
}
}

3、如果想要让其他APP能跳转到自己的应用可以在info.plist 里设置一个或多个 urlTypes

	<key>CFBundleURLTypes</key>
<array>
<dict>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>CFBundleURLSchemes</key>
<array>
<string>demos</string>
</array>
</dict>
</array>

然后在AppDelegate里做个可以跳转的权限配置(iOS 9 以后)

- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options{
NSLog(@"func: %s url:%@ ",__func__,[url absoluteString]);
return YES;
}

验证的话,跑真机,只需要在 safari里输入 demos://。按照提示就可以跳转了。