2019-03-14 iOS 开发之微信授权登录踩坑记录

这里用到了原生网络请求,需要去微信开发者中心获取需要的两个值WeChatAppID WeChatSecret


image.png

AppDelegate.m

-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary *)options
{
    /*! @brief 处理微信通过URL启动App时传递的数据
     *
     * 需要在 application:openURL:sourceApplication:annotation:或者application:handleOpenURL中调用。
     * @param url 微信启动第三方应用时传递过来的URL
     * @param delegate  WXApiDelegate对象,用来接收微信触发的消息。
     * @return 成功返回YES,失败返回NO。
     */
    
    return [WXApi handleOpenURL:url delegate:self];
}

/*! 微信回调,不管是登录还是分享成功与否,都是走这个方法 @brief 发送一个sendReq后,收到微信的回应
 *
 * 收到一个来自微信的处理结果。调用一次sendReq后会收到onResp。
 * 可能收到的处理结果有SendMessageToWXResp、SendAuthResp等。
 * @param resp     具体的回应内容,是自动释放的
 */
-(void) onResp:(BaseResp*)resp
{
    JMLog(@"resp %d",resp.errCode);
    /*
     enum  WXErrCode {
     WXSuccess           = 0,    成功
     WXErrCodeCommon     = -1,  普通错误类型
     WXErrCodeUserCancel = -2,    用户点击取消并返回
     WXErrCodeSentFail   = -3,   发送失败
     WXErrCodeAuthDeny   = -4,    授权失败
     WXErrCodeUnsupport  = -5,   微信不支持
     };
     */
    if ([resp isKindOfClass:[SendAuthResp class]])
    {   //授权登录的类。
        if (resp.errCode == 0)
        {  //成功。
            //这里处理回调的方法 。 通过代理吧对应的登录消息传送过去。
            if ([_sxwWeChatDelegate respondsToSelector:@selector(loginSuccessByCode:)])
            {
                SendAuthResp *resp2 = (SendAuthResp *)resp;
                [_sxwWeChatDelegate loginSuccessByCode:resp2.code];
            }
        }
        else if (resp.errCode == -2)
        { //失败
            [HWAlertShowMessage showAlertViewController:[self getCurrentVC] title:@"登录失败" message:@"用户点击取消并返回" cancelButtonTitle:@"取消" sureButtonTitle:@"确定" sure:^{
                
            } cancle:^{
                
            }];
        }
        else if (resp.errCode == -1)
        {
            [HWAlertShowMessage showAlertViewController:[self getCurrentVC] title:@"登录失败" message:@"普通错误类型" cancelButtonTitle:@"取消" sureButtonTitle:@"确定" sure:^{
                
            } cancle:^{
                
            }];
        }
        else if (resp.errCode == -3)
        {
            [HWAlertShowMessage showAlertViewController:[self getCurrentVC] title:@"登录失败" message:@"发送失败" cancelButtonTitle:@"取消" sureButtonTitle:@"确定" sure:^{
                
            } cancle:^{
                
            }];
        }
        else if (resp.errCode == -4)
        {
            [HWAlertShowMessage showAlertViewController:[self getCurrentVC] title:@"登录失败" message:@"授权失败" cancelButtonTitle:@"取消" sureButtonTitle:@"确定" sure:^{
                
            } cancle:^{
                
            }];
        }
        else if (resp.errCode == -5)
        {
            [HWAlertShowMessage showAlertViewController:[self getCurrentVC] title:@"登录失败" message:@"微信不支持" cancelButtonTitle:@"取消" sureButtonTitle:@"确定" sure:^{
                
            } cancle:^{
                
            }];
        }
    }
    
    if ([resp isKindOfClass:[SendMessageToWXResp class]])
    { //微信分享 微信回应给第三方应用程序的类
        SendMessageToWXResp *response = (SendMessageToWXResp *)resp;
        JMLog(@"error code %d  error msg %@  lang %@   country %@",response.errCode,response.errStr,response.lang,response.country);
        
        if (resp.errCode == 0)
        {  //成功。
            //这里处理回调的方法 。 通过代理吧对应的登录消息传送过去。
            if (_sxwWeChatDelegate)
            {
                if([_sxwWeChatDelegate respondsToSelector:@selector(shareSuccessByCode:)])
                {
                    [_sxwWeChatDelegate shareSuccessByCode:response.errCode];
                }
            }
        }
        else
        { //失败
            JMLog(@"error %@",resp.errStr);
            UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"分享失败" message:[NSString stringWithFormat:@"reason : %@",resp.errStr] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
            [alert show];
        }
    }
    
    /*
     0  展示成功页面
     -1  可能的原因:签名错误、未注册APPID、项目设置APPID不正确、注册的APPID与设置的不匹配、其他异常等。
     -2  用户取消    无需处理。发生场景:用户不支付了,点击取消,返回APP。
     */
    if ([resp isKindOfClass:[PayResp class]])
    { // 微信支付
        
        PayResp*response=(PayResp*)resp;
        switch(response.errCode)
        {
            case 0:
                //服务器端查询支付通知或查询API返回的结果再提示成功
                NSLog(@"支付成功");
                break;
                
            default:
                NSLog(@"支付失败,retcode=%d  errormsg %@",resp.errCode ,resp.errStr);
                break;
        }
    }
}

AppDelegate.h
//设置代理

#import 

@protocol SXWWeChatDelegate 

-(void)loginSuccessByCode:(NSString *)code;
-(void)shareSuccessByCode:(int) code;

@end

@interface AppDelegate : UIResponder 

@property (strong, nonatomic) UIWindow *window;
@property (nonatomic,assign)NSInteger allowRotate;
@property (nonatomic, weak) id sxwWeChatDelegate;

@end

//在需要用到的地方去实现代理

#import "LoginViewController.h"
#import "AppDelegate.h"

@interface LoginViewController ()

//微信登录按钮的点击事件

//微信号快捷登录
-(void)sendAuthRequest
{
    if (![WXApi isWXAppInstalled] || ![WXApi isWXAppSupportApi])
    {
         [self setupAlertController];
    }
    else
    {
        //构造SendAuthReq结构体
        SendAuthReq* req =[[SendAuthReq alloc]init];
        //scope - 必须,应用授权作用域,如获取用户个人信息则填写snsapi_userinfo
        req.scope = @"snsapi_userinfo";
        //state - 非必须,用于保持请求和回调的状态,授权请求后原样带回给第三方。该参数可用于防止csrf攻击(跨站请求伪造攻击),建议第三方带上该参数,可设置为简单的随机数加session进行校验
        req.state = @"authorization_code";
        
        //appid - 必须,应用唯一标识,在微信开放平台提交应用审核通过后获得
        req.openID = WeChatAppID;
        
        self.delegate = [UIApplication sharedApplication].delegate;
        self.delegate.sxwWeChatDelegate = self;
        //第三方向微信终端发送一个SendAuthReq消息结构
        [WXApi sendReq:req];
    }

}
#pragma mark - 设置弹出提示语
- (void)setupAlertController {
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"请先安装微信客户端" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *actionConfirm = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
    [alert addAction:actionConfirm];
    [self presentViewController:alert animated:YES completion:nil];
}

#pragma mark 微信登录回调。
-(void)loginSuccessByCode:(NSString *)code
{
    JMLog(@"code==== %@",code);
    __weak typeof(*&self) weakSelf = self;
    
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];//请求
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];//响应
    manager.responseSerializer.acceptableContentTypes = [NSSet setWithObjects:@"text/html",@"application/json", @"text/json",@"text/plain", nil];
    //通过 appid  secret 认证code . 来发送获取 access_token的请求
    [manager GET:[NSString stringWithFormat:@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code",WeChatAppID,WeChatSecret,code] parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {  //获得access_token,然后根据access_token获取用户信息请求。
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        JMLog(@"dic==== %@",dic);
        
        /*
         access_token    接口调用凭证
         expires_in    access_token接口调用凭证超时时间,单位(秒)
         refresh_token    用户刷新access_token
         openid    授权用户唯一标识
         scope    用户授权的作用域,使用逗号(,)分隔
         unionid     当且仅当该移动应用已获得该用户的userinfo授权时,才会出现该字段
         */
        NSString* accessToken=[dic valueForKey:@"access_token"];
        NSString* openID=[dic valueForKey:@"openid"];
        [weakSelf requestUserInfoByToken:accessToken andOpenid:openID];
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        JMLog(@"error %@",error.localizedFailureReason);
    }];
    
}

-(void)requestUserInfoByToken:(NSString *)token andOpenid:(NSString *)openID
{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.requestSerializer = [AFJSONRequestSerializer serializer];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:[NSString stringWithFormat:@"https://api.weixin.qq.com/sns/userinfo?access_token=%@&openid=%@",token,openID] parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject)
     {
        NSDictionary *dic = (NSDictionary *)[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        //开发人员拿到相关微信用户信息后, 需要与后台对接,进行登录
        JMLog(@"login success dic  ==== %@",dic);
         
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error)
     {
    JMLog(@"error %ld",(long)error.code);
    }];
}

白名单的设置,和ID的设置在 Info.plist文件里面

CFBundleURLTypes
    
        
            CFBundleTypeRole
            Editor
            CFBundleURLName
            weixin
            CFBundleURLSchemes
            
                《这里写注册的微信APPID》
            
        
    
    LSApplicationQueriesSchemes
    
        wechat
        weixin
    

需要去配置的东西

image.png

你可能感兴趣的:(2019-03-14 iOS 开发之微信授权登录踩坑记录)