iOS开发 - APP间通讯

常用的APP间通讯场景是支付和分享。接入支付宝等支付场景时需要跳转到支付宝APP,完成支付后再将支付结果返回原来的APP,这样就涉及APP间传值。

新建App1和App2两个应用,如果我想从App1调起App2,那么我需要添加App2的URL Scheme,不同于其他手机APPs的值,用来在App1发起openUrl方法时找到手机里的App2应用。

设置App2的URL Scheme为AppTwoScheme


App1想要调起App2时只需要通过如下方法即可

NSURL *url = [NSURL URLWithString: [NSString stringWithFormat: @"AppTwoScheme://com.zy.app1/subpath?para=%i&from=%@", 1, @"app1"]];
if ([[UIApplication sharedApplication] canOpenURL: url]) {
    NSLog(@"canOpenUrl");
    if (@available(iOS 10, *)) {
        NSDictionary *options = @{UIApplicationOpenURLOptionsSourceApplicationKey : @YES};
        [[UIApplication sharedApplication] openURL: url options: options completionHandler: nil];
    }else{
        [[UIApplication sharedApplication] openURL: url];
    }
}

从iOS 10开始,还需要在App1中添加白名单LSApplicationQueriesSchemes数组


如果只需要从App1到App2,不需要返回,这要就可以了。

在App2的AppDelegate代理方法中接收传入的值,如果使用SceneDelegate类,则在-(void)scene:(UIScene *)scene openURLContexts:(NSSet<UIOpenURLContext *> *)URLContexts代理方法中接收。

-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
    UIAlertController *alertVC = [UIAlertController alertControllerWithTitle: @"提示" message: @"收到一条从APP1传来的简讯" preferredStyle: UIAlertControllerStyleAlert];
    [self.window.rootViewController presentViewController: alertVC animated: YES completion: nil];
    NSLog(@"absoulteString: %@", url.absoluteString);
    NSLog(@"scheme: %@", url.scheme);
    NSLog(@"host: %@", url.host);
    NSLog(@"path: %@", url.path);
    NSLog(@"query: %@", url.query);
    NSLog(@"Url参数: %@", [self getURLParameters: url.absoluteString]);
    if ([url.host isEqualToString: @"com.zy.app1"]) {
        //从app1跳转过来
    }
    return YES;
}

scheme、host和query等结果如下:



若需要将App2中的处理结果返回给App1,同样的操作:设置App1的URL Scheme为AppOneScheme,添加App2的白名单为AppOneScheme,给App2一个触发事件调用

UIAlertController *alertVC = [UIAlertController alertControllerWithTitle: @"提示" message: @"收到一条从APP1传来的简讯" preferredStyle: UIAlertControllerStyleAlert];
[alertVC addAction: [UIAlertAction actionWithTitle: @"确定" style: UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSURL *oneUrl = [NSURL URLWithString: [NSString stringWithFormat: @"AppOneScheme://com.zy.app2?result=1"]];
        if (@available(iOS 10, *)) {
            [[UIApplication sharedApplication] openURL: oneUrl options: @{} completionHandler: nil];
        }else{
            [[UIApplication sharedApplication] openURL: oneUrl];
        }
 }]];
 [self.window.rootViewController presentViewController: alertVC animated: YES completion: nil];

App1接收到的返回值为

-(void)scene:(UIScene *)scene openURLContexts:(NSSet<UIOpenURLContext *> *)URLContexts
{
    if (URLContexts.count > 0) {
        NSEnumerator *enumerator = [URLContexts objectEnumerator];
        UIOpenURLContext *urlContext = [enumerator nextObject];
        NSURL *url = urlContext.URL;
        NSLog(@"absoulteString: %@", url.absoluteString);
        NSLog(@"scheme: %@", url.scheme);
        NSLog(@"host: %@", url.host);
        NSLog(@"path: %@", url.path);
        NSLog(@"query: %@", url.query);
        NSLog(@"Url参数: %@", [self getURLParameters: url.absoluteString]);
        if ([url.host isEqualToString: @"com.zy.app2"]) {
            //从app2返回的数据
        }
    }
}

如此,即完成了一组App间的传值。

但要注意,如果从左上角的返回按钮回到App1则无法将结果返回,因为没有调用openURL吧。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 背景:目前公司维护2个App,一个电商类项目,一个金融类项目;类似淘宝和支付宝,很多时候需要在App之间进行通信,...
    minyue阅读 3,658评论 1 1
  • iOS系统是相对封闭的系统,App各自在各自的沙盒(sandbox)中运行。每个App都只能读取iPhone上iO...
    SkyMing一C阅读 5,395评论 0 4
  • 什么是URL Scheme 简单的说,由于苹果选择使用沙盒机制来保障用户的隐私和安全,APP只能访问自己沙盒数据,...
    哦尼酱呢阅读 7,100评论 0 0
  • 1 OpenUrl原理 如果一个应用程序支持一些已知类型的URL,您就可以通过对应的URL模式和该程序进行通讯。然...
    Kevin_Junbaozi阅读 5,548评论 0 7
  • 什么是URL Scheme 简单的说,由于苹果选择使用沙盒机制来保障用户的隐私和安全,APP只能访问自己沙盒数据,...
    CodeRookie阅读 14,179评论 1 7