iOS登录微信登录

1.下载sdk并作对应的配置;

2.在AppDelegate文件里,- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions方法下添加以下代码:

[WXApi registerApp:WXAppId withDescription:@"wechat"];

3.- (BOOL)application:(UIApplication *)application      handleOpenURL:(NSURL *)url{    return  [WXApi handleOpenURL:url delegate:self] || [UMSocialSnsService handleOpenURL:url];    }-(BOOL)application:(UIApplication*)app openURL:(NSURL*)url options:(NSDictionary*)options

{

return  [WXApi handleOpenURL:url delegate:self] ;

}

- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation

{

 [WXApi handleOpenURL:url delegate:self];

}

- (void)onResp:(BaseResp *)resp

{


if ([resp isKindOfClass:[SendAuthResp class]])

{

if (resp.errCode == 0)

{  //成功。

NSLog(@"error %d",resp.errCode);

//            这里处理回调的方法 。 通过代理吧对应的登录消息传送过去。

if ([_wxDelegate respondsToSelector:@selector(loginSuccessByCode:)])

{

SendAuthResp *resp2 = (SendAuthResp *)resp;

[_wxDelegate loginSuccessByCode:resp2.code];

}

}else{ //失败

NSLog(@"error %@",resp.errStr);

UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"登录失败" message:[NSString stringWithFormat:@"reason : %@",resp.errStr] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];

[alert show];

}

}

}

4.在登录界面点击微信登录的地方(要设置代理)

#import

@protocol wxPassDelegate

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

@end


if ([WXApi isWXAppInstalled]) {

SendAuthReq *req = [[SendAuthReq alloc]init];

req.scope = @"snsapi_userinfo";

req.openID = @"wx5f427e47a3d32261";

req.state = @"1245";

appdelegate = (AppDelegate *)[[UIApplication  sharedApplication] delegate];

appdelegate.wxDelegate = self;

[WXApi sendReq:req];

}


#pragma mark 微信登录回调。

-(void)loginSuccessByCode:(NSString *)code{

NSLog(@"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, 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",@"wx5f427e47a3d32261",@"5accc360949a8fb810b29f00c20741d0",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];

NSLog(@"dic %@",dic);

[self getWxPersonalInfoWithAccess_token:dic[@"access_token"] withOpenId:dic[@"openid"]];

//                    [dic setObject:@"" forKey:@"phoneNum"];

//                    [dic setObject:@"" forKey:@"password"];

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

NSLog(@"error %@",error.localizedFailureReason);

}];

}


你可能感兴趣的:(iOS登录微信登录)