ios 微信登录

1.在这里说一下微信登录,其实很简单,配置就根据官方文档配置,就不在这里强调了。
第一步:在appDelegate.m中注册微信
    BOOL sccurr = [WXApi registerApp:WX_APP];
    if (sccurr == YES) {
        NSLog(@"微信注册成功");
    }else{
        NSLog(@"注册失败");
    }
 第二步:设置微信回调
 - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url {
        return [WXApi handleOpenURL:url delegate:self];
}
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
        return [WXApi handleOpenURL:url delegate:self];
}
//微信登录中这个方法中必须有,不然后走回调失败
- (BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString*, id> *)options NS_AVAILABLE_IOS(9_0){
        return [WXApi handleOpenURL:url delegate:self];
    }

}
第三步:微信授权
-(void)sendAuthRequest
{
//点击按钮时只需要这几句代码基本就可以调起微信授权登录界面了
    //构造SendAuthReq结构体
    SendAuthReq* req =[[SendAuthReq alloc ] init ];
    req.scope = @"snsapi_userinfo" ;
    req.state = @"wxb839cbeb1ab95794" ;
    //第三方向微信终端发送一个SendAuthReq消息结构
    [WXApi sendReq:req];
}
第四步:微信回调方法
//微信支付回调处理
-(void) onResp:(BaseResp*)resp
{
    if([resp isKindOfClass:[SendAuthResp class]]){
        switch (resp.errCode) {
                //retcode: 0:用户同意 -4:用户拒绝授权 -2:用户取消
            case 0:{
   //如果成功了,一般发通知到相应的页面,不要给appDelegate太大的压力
                NSMutableDictionary *notDic = [NSMutableDictionary dictionary];
                [notDic setValue:resp forKey:@"wxResp"];
                [[NSNotificationCenter defaultCenter] postNotificationName:@"wxLogin" object:notDic];

            }
                break;
            case -4:
                strMsg = @"用户拒绝授权!";
                ZNLog(@"支付成功-PaySuccess,retcode = %d", resp.errCode);
                //                [FanweMessage alert:strMsg];
                break;
            case -2:
                strMsg = @"用户取消!";
                ZNLog(@"支付成功-PaySuccess,retcode = %d", resp.errCode);
                //                [FanweMessage alert:strMsg];
                break;

            default:

                break;
        }
    }
}
第五步:微信回调
//1.在相应的点击界面接收通知
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(wxLogin:) name:@"wxLogin" object:nil];

//2.微信回调,在这个方法中就可以拿到微信返回的一个code,看后台要求,如果后台就只需要这个code那么微信登录到这个方法就结束了
-(void)wxLogin:(NSNotification *)notification{
    BaseResp *resp = notification.object[@"wxResp"];
    SendAuthResp *aresp = (SendAuthResp *)resp;
    [self weChatCallBackWithCode:aresp.code];
}
//如果还需要token和openid就调用weChatCallBackWithCode:(NSString *)code方法即可
//下面这两个方法中的URL是微信给我们提供的,我们就只需要把参数传给微信就好了,不需要做更改
- (void)weChatCallBackWithCode:(NSString *)code{
   //得到code进行相应的操作
}
//这里得到token 和openid
- (void)weChatCallBackWithCode:(NSString *)code{
    NSString *urlString =[NSString stringWithFormat:@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code",APPKEY,APP_SECRET,code];
//    __block LoginPremiseViewController *loginVC = self;
    [[NetWorkManager sharedManager] requestSerializer].timeoutInterval = TimeOutInterval;
    [[NetWorkManager sharedManager] GET:urlString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
    //调用这个方法得到用户信息
        [self getUserInfoWithAccessToken:dic[@"access_token"] andOpenId:dic[@"openid"]];

    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

    }];

}
//在这里得到用户信息
- (void)getUserInfoWithAccessToken:(NSString *)accessToken andOpenId:(NSString *)openId{
        NSString *urlString =[NSString stringWithFormat:@"https://api.weixin.qq.com/sns/userinfo?access_token=%@&openid=%@",accessToken,openId];
    [[NetWorkManager sharedManager] requestSerializer].timeoutInterval = TimeOutInterval;
    [[NetWorkManager sharedManager] GET:urlString parameters:nil progress:nil success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSLog(@"地产  == %@",dic);
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {

    }];
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值