美文网首页苹果开发者
微信Universal Links

微信Universal Links

作者: 喵喵粉 | 来源:发表于2020-02-07 16:45 被阅读0次

    go

    1. 添加文件

    • 创建json文件apple-app-site-association,无后缀名。
    {
        "applinks": {
            "apps": [],
            "details": [
                {
                    "appID": "TeamID.bundleID",
                    "paths": ["*"]
                }
            ]
        }
    }
    
    • json文件上传到服务器域名根目录下

    • json文件的URL:

    https://aaa.com/apple-app-site-association
    

    2. Xcode设置

    • Associated Domains

    填写applinks:+服务器的域名,如applinks:aaa.com 或者 applinks:https://aaa.com

    Associated Domains
    • info.plist添加LSApplicationQueriesSchemes,值为weixin,wechat, weixinULAPI
    LSApplicationQueriesSchemes
    • 添加URL Types
    aa

    3. 开发者应用登记页面

    • 填写Universal Links,以/结尾,如https://aaa.com/
    image.png
    • 快速验证

    在备忘录中输入https://aaa.com/apple-app-site-association,长按这个链接,出现下图提示则配置成功。

    apple-app-site-association.png

    4. 写代码

    • appDelegate处理回调
    
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(nullable NSDictionary<UIApplicationLaunchOptionsKey,id> *)launchOptions {
        [self thirdConfig];
        return YES;
    }
    
    - (void)thirdConfig {
        ///微信
        [WXApi registerApp:wxAppID universalLink:@"https://aa.com/"];
    }
    
    #pragma mark - Wechat Delegate
    - (void)onReq:(BaseReq *)req {
    }
    
    - (void)onResp:(BaseResp *)resp {
        NSLog(@"%s resp:%@", __func__, resp);
    
        // 1.分享后回调类
        if ([resp isKindOfClass:[SendMessageToWXResp class]]) {
            NSLog(@"分享ok");
        }
        // 2.微信登录向微信请求授权回调类
        else if ([resp isKindOfClass:[SendAuthResp class]]) {
            SendAuthResp *rsp = (SendAuthResp *)resp;
            NSLog(@"收到code:%@", rsp.code);
            
            NSString *state = rsp.state;
            if ([state isEqualToString:@""]) {
            }
            
            if (resp.errCode == 0) {
                NSLog(@"登录ok");
            } else {
                NSLog(@"登录失败");
            }
        }
        // 3.支付后回调类
        else if ([resp isKindOfClass:[PayResp class]]) {
            NSLog(@"支付");
        }
    }
    
    - (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options {
        NSLog(@"%s url:%@", __func__, url);
        
        NSString *absoluteString = url.absoluteString;
        if ([absoluteString hasPrefix:wxAppID]) {
            ///微信登录
            return [WXApi handleOpenURL:url delegate:self];
        } else if ([absoluteString hasPrefix:@"xxx"]) {
        }
        
        return [WXApi handleOpenURL:url delegate:self];
    }
    
    - (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray<id<UIUserActivityRestoring>> * _Nullable))restorationHandler {
        NSLog(@"%s", __func__);
        return [WXApi handleOpenUniversalLink:userActivity delegate:self];
    }
    
    • 发起微信登录
    - (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event {
        SendAuthReq* req = [[SendAuthReq alloc] init];
        req.scope = @"snsapi_userinfo";
        req.state = @"bundleID";
        
        //第三方向微信终端发送一个SendAuthReq消息结构
        //发送请求到微信,等待微信返回onResp
        [WXApi sendReq:req completion:nil];
    }
    

    5. 踩坑

    • 掉微信登录api报错
    -canOpenURL: failed for URL: "weixinULAPI://" - error: "This app is not allowed to query for scheme weixinulapi"
    

    解决:填写URL Types

    • ios13授权登录后,跳回APP,不走continueUserActivityonResp回调
      如果包含SceneDelegatecontinueUserActivity方法在appDelegate里边写是不会调用的,要在SceneDelegate文件里写。
      解决:去掉SceneDelegate相关的代码。

    6. 微信的scope说明

    授权作用域(scope) 接口 接口说明
    snsapi_base /sns/oauth2/access_token 通过code换取access_token、refresh_token和已授权scope
    /sns/oauth2/refresh_token 刷新或续期access_token使用
    /sns/auth 检查access_token有效性
    snsapi_userinfo /sns/userinfo 获取用户个人信息

    其中snsapi_base属于基础接口,若应用已拥有其它scope权限,则默认拥有snsapi_base的权限。使用snsapi_base可以让移动端网页授权绕过跳转授权登录页请求用户授权的动作,直接跳转第三方网页带上授权临时票据(code),但会使得用户已授权作用域(scope)仅为snsapi_base,从而导致无法获取到需要用户授权才允许获得的数据和基础功能。

    相关文章

      网友评论

        本文标题:微信Universal Links

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