美文网首页
iOS开发 - APP间通讯

iOS开发 - APP间通讯

作者: lotus_yoma | 来源:发表于2020-03-06 16:03 被阅读0次

    常用的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吧。

    相关文章

      网友评论

          本文标题:iOS开发 - APP间通讯

          本文链接:https://www.haomeiwen.com/subject/jvoirhtx.html