问题的背景:
a. 企业账号发布和微信相关的有两个点:微信分享和未签名应用提示
b. 微信授权登录:更新版本后发现注册时多了个universal link,跳转有问题了
在这里我记录一下我遇到的问题和解决方法
经过这两天尝试了接入微信登陆接口,中间也遇到了不少问题,记录一下,这里是微信文档可以拿来参考微信文档
1. 准备工作
先要在微信开放平台创建应用,这个过程就不详细叙述了,注册好后可以拿到appID、appSecret和UniversalLink。
iOS项目相关配置,
应用下载地址可以是App Store发布的地址也可以是企业账号发布的服务器地址。
App Store地址获取,手机App Store搜到你们APP,点击分享下面有个拷贝地址,这个就是应用下载地址而且是不会变的。
Bundle ID: 项目的Bundle Identifier
UniversalLink:必须是https的URL连接 ,连接可以是官网地址也可以直接指向你们产品首页
现在没有Universal Links是不能跳转的2. 导入微信SDK:我是通过cocoapods导入sdk pod 'WechatOpenSDK'
我的WechatOpenSDK 1.8.7.1 这就意味着需要配置universal link
3.注册白名单以及设置App Transport Security Settings和URL types
微信权限相关配置比较重要当然也可以在RARGETS -> info - >URL Tpes设置
这个比较直观一点4. 代码区域:AppDelegate
- (BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions {
//微信注册
[WXApi registerApp:@"你的appid(微信申请应用)" universalLink:@"微信应用平台的ul"];
}
#pragma mark - 唤起微信
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{
return [WXApi handleOpenURL:url delegate:self];
}
- (void)onResp:(BaseResp *)resp{
if([resp isKindOfClass:[SendAuthResp class]]){
SendAuthResp *resp2 = (SendAuthResp *)resp;
[[NSNotificationCenter defaultCenter] postNotificationName:WX_Notification_name object:resp2];
}else{
NSLog(@"授权失败");
}
}
登录:
#pragma mark - 微信登录
- (void)wechatLogin:(id)sender {
//判断微信是否安装
if([WXApi isWXAppInstalled]){
SendAuthReq *req = [[SendAuthReq alloc] init];
req.scope = @"snsapi_userinfo";
req.state = @"App";
[WXApi sendAuthReq:req viewController:self delegate:self completion:^(BOOL success) {
NSLog(@"successsuccess");
}];
}else{
[self setupAlertController];
}
}
#pragma mark - 获取微信code
- (void)wechatLoginNotification:(NSNotification*)notification
{
//获取到code
SendAuthResp *resp = notification.object;
NSLog(@"%@",resp.code);
NSString * _code = resp.code;
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSString *url = [NSString stringWithFormat:@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=%@",appId,appSecret,_code,@"authorization_code"];
manager.requestSerializer = [AFJSONRequestSerializer serializer];
[manager.requestSerializer setValue:@"text/html; charset=utf-8" forHTTPHeaderField:@"Content-Type"];
NSMutableSet *mgrSet = [NSMutableSet set];
mgrSet.set = manager.responseSerializer.acceptableContentTypes;
[mgrSet addObject:@"text/html"];
//因为微信返回的参数是text/plain 必须加上 会进入fail方法
[mgrSet addObject:@"text/plain"];
[mgrSet addObject:@"application/json"];
manager.responseSerializer.acceptableContentTypes = mgrSet;
[manager GET:url parameters:nil headers:nil progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"success");
NSDictionary *resp = (NSDictionary*)responseObject;
NSString *openid = resp[@"openid"];
NSString *unionid = resp[@"unionid"];
NSString *accessToken = resp[@"access_token"];
NSString *refreshToken = resp[@"refresh_token"];
if(accessToken && ![accessToken isEqualToString:@""] && openid && ![openid isEqualToString:@""]){
[[NSUserDefaults standardUserDefaults] setObject:openid forKey:WX_OPEN_ID];
[[NSUserDefaults standardUserDefaults] setObject:accessToken forKey:WX_ACCESS_TOKEN];
[[NSUserDefaults standardUserDefaults] setObject:refreshToken forKey:WX_REFRESH_TOKEN];
[[NSUserDefaults standardUserDefaults] synchronize];
}
[self getWechatUserInfo];
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
}
#pragma mark - 获取微信个人信息
- (void)getWechatUserInfo
{
//获取个人信息
AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
NSString *url = [NSString stringWithFormat:@"https://api.weixin.qq.com/sns/userinfo?access_token=%@&openid=%@",[[NSUserDefaults standardUserDefaults] objectForKey:WX_ACCESS_TOKEN],[[NSUserDefaults standardUserDefaults] objectForKey:WX_OPEN_ID]];
NSMutableSet *mgrSet = [NSMutableSet set];
mgrSet.set = manager.responseSerializer.acceptableContentTypes;
[mgrSet addObject:@"text/html"];
//因为微信返回的参数是text/plain 必须加上 会进入fail方法
[mgrSet addObject:@"text/plain"];
[mgrSet addObject:@"application/json"];
manager.responseSerializer.acceptableContentTypes = mgrSet;
[manager GET:url parameters:nil headers:nil progress:^(NSProgress * _Nonnull downloadProgress) {
} success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) {
NSLog(@"success");
NSLog(@"success");
NSLog(@"%@",responseObject);
// NSDictionary *resp = (NSDictionary*)responseObject;
// self->_nicknameLabel.text = resp[@"nickname"];
// self->_sexLabel.text = [resp[@"sex"] intValue] == 1 ? @"男" : @"女";
// self->_addressLabel.text = [NSString stringWithFormat:@"%@%@%@",resp[@"country"],resp[@"province"],resp[@"city"]];
// }
} failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
}];
}
接下来就是没你们怎么关联账号问题了
网友评论