关于微信登录在登录界面怎么传值

参考文章

问题:在Appdelegate中调用微信的协议方法,但是如何在登录控制器中获取到code值

解决:用通知传值

-(void) onResp:(BaseResp*)resp{
    /*
     enum  WXErrCode {
     WXSuccess           = 0,    成功
     WXErrCodeCommon     = -1,  普通错误类型
     WXErrCodeUserCancel = -2,    用户点击取消并返回
     WXErrCodeSentFail   = -3,   发送失败
     WXErrCodeAuthDeny   = -4,    授权失败
     WXErrCodeUnsupport  = -5,   微信不支持
     };
     */
    if ([resp isKindOfClass:[SendAuthResp class]]) {   //授权登录的类。
        if (resp.errCode == 0) {  //成功。
            SendAuthResp *resp2 = (SendAuthResp *)resp;
            //发通知
            [[NSNotificationCenter defaultCenter] postNotificationName:@"wxLogin" object:resp2.code];
        }else{ //失败
            NSLog(@"授权失败");
        }
    }
}

在登录的控制器里面传递参数即可

- (void)getWX:(NSNotification *)sender {
    
    NSString *code = [sender object];
    //用户同意授权
    NSString *URL = @"https://api.weixin.qq.com/sns/oauth2/access_token?";
    NSDictionary *dict = @{
                           @"appid":WX_APPID,
                           @"secret":WXAPPKEYSECRET,
                           @"code":code,
                           @"grant_type":@"authorization_code"
                           };
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
    
    NSURLSessionDataTask *dataTask = [manager GET:URL parameters:dict progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        
        [TTUserManager sharedInstance].currentUser.accessToken = responseObject[@"access_token"];
        [TTUserManager sharedInstance].currentUser.refreshToken = responseObject[@"refresh_token"];
        [TTUserManager sharedInstance].currentUser.expires_in = responseObject[@"expires_in"];
        [TTUserManager sharedInstance].currentUser.scope = responseObject[@"scope"];
        [TTUserManager sharedInstance].currentUser.unionid = responseObject[@"unionid"];
        [TTUserManager sharedInstance].currentUser.openid = responseObject[@"openid"];
        [TTUserManager sharedInstance].currentUser.openId = responseObject[@"openid"];
        
        [self getUserInfoFromWechat];
    } failure:nil];
    [dataTask resume];

}

你可能感兴趣的:(关于微信登录在登录界面怎么传值)