微信Universal Links
2020-02-07 本文已影响0人
喵喵粉
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
-
info.plist
添加LSApplicationQueriesSchemes
,值为weixin,wechat, weixinULAPI
- 添加
URL Types
3. 开发者应用登记页面
- 填写
Universal Links
,以/
结尾,如https://aaa.com/
。
- 快速验证
在备忘录中输入https://aaa.com/apple-app-site-association
,长按这个链接,出现下图提示则配置成功。
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,不走continueUserActivity
和onResp
回调
如果包含SceneDelegate
,continueUserActivity
方法在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
,从而导致无法获取到需要用户授权才允许获得的数据和基础功能。