微信Universal Links

  1. 添加文件

创建json文件apple-app-site-association,无后缀名。

{
    "applinks": {
        "apps": [],
        "details": [
            {
                "appID": "TeamID.bundleID",
                "paths": ["*"]
            }
        ]
    }
}

将json文件上传到服务器域名根目录下

json文件的URL:

https://aaa.com/apple-app-site-association
  1. Xcode设置
Associated Domains
填写applinks:+服务器的域名,如applinks:aaa.com applinks:https://aaa.com

微信Universal Links_第1张图片
info.plist添加LSApplicationQueriesSchemes,值为weixin,wechat, weixinULAPI

LSApplicationQueriesSchemes

添加URL Types
微信Universal Links_第2张图片
3. 开发者应用登记页面

填写Universal Links,以/结尾,如https://aaa.com/
微信Universal Links_第3张图片
快速验证
在备忘录中输入https://aaa.com/apple-app-site-association,长按这个链接,出现下图提示则配置成功。
微信Universal Links_第4张图片
4. 写代码
appDelegate处理回调

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(nullable NSDictionary *)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 *)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> * _Nullable))restorationHandler {
    NSLog(@"%s", __func__);
    return [WXApi handleOpenUniversalLink:userActivity delegate:self];
}
发起微信登录
- (void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    SendAuthReq* req = [[SendAuthReq alloc] init];
    req.scope = @"snsapi_userinfo";
    req.state = @"bundleID";
    
    //第三方向微信终端发送一个SendAuthReq消息结构
    //发送请求到微信,等待微信返回onResp
    [WXApi sendReq:req completion:nil];
}
  1. 踩坑

掉微信登录api报错

-canOpenURL: failed for URL: "weixinULAPI://" - error: "This app is not allowed to query for scheme weixinulapi"

解决:填写URL Types

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

  1. 微信的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,从而导致无法获取到需要用户授权才允许获得的数据和基础功能。

你可能感兴趣的:(ios)