//微信登陆 [WXApi registerApp:WeiXin_AppId withDescription:@"weixin"];
//把代理设置到登陆视图中
- (BOOL)application:(UIApplication *)application
handleOpenURL:(NSURL *)url
{
return [WXApi handleOpenURL:url delegate:[LoginViewController shareLogin]];
}
- (BOOL)application:(UIApplication *)application
openURL:(NSURL *)url
sourceApplication:(NSString *)sourceApplication
annotation:(id)annotation
{
return [WXApi handleOpenURL:url delegate:[LoginViewController shareLogin]];
}
-(void)weiXinLogin
{
SendAuthReq* req =[[SendAuthReq alloc] init];
req.scope = @"snsapi_userinfo,snsapi_base";
req.state = @"0744" ;
[WXApi sendReq:req];
}
-(void)onReq:(BaseReq *)req
{
NSLog(@"呵呵");
[self msgHint:@"登陆失败"];
}
-(void)onResp:(BaseResp *)resp
{
SendAuthResp* sender = (SendAuthResp*)resp;
NSString* code = sender.code;
NSLog(@"啦啦 code = %@",code);
MBProgressHUD * hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
hud.labelText = @"收取用户信息..";
[self getAccess_tokenWithCode:code];
}
-(void)getAccess_tokenWithCode:(NSString*)myCode
{
//https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
NSString *url =[NSString stringWithFormat:@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code",kWXAPP_ID,kWXAPP_SECRET,myCode];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL *zoneUrl = [NSURL URLWithString:url];
NSString *zoneStr = [NSString stringWithContentsOfURL:zoneUrl encoding:NSUTF8StringEncoding error:nil];
NSData *data = [zoneStr dataUsingEncoding:NSUTF8StringEncoding];
dispatch_async(dispatch_get_main_queue(), ^{
if (data) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSString* token = [dic objectForKey:@"access_token"];
NSString* openid = [dic objectForKey:@"openid"];
[self getUserInfoWithToken:token openId:openid];
NSLog(@"token = %@",token);
NSLog(@"openid = %@",openid);
}
});
});
}
-(void)getUserInfoWithToken:(NSString*)myToken openId:(NSString*)myOpenId
{
// https://api.weixin.qq.com/sns/userinfo?access_token=ACCESS_TOKEN&openid=OPENID
NSString *url =[NSString stringWithFormat:@"https://api.weixin.qq.com/sns/userinfo?access_token=%@&openid=%@",myToken,myOpenId];
NSLog(@"infoUrl = %@",url);
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
NSURL *zoneUrl = [NSURL URLWithString:url];
NSString *zoneStr = [NSString stringWithContentsOfURL:zoneUrl encoding:NSUTF8StringEncoding error:nil];
NSData *data = [zoneStr dataUsingEncoding:NSUTF8StringEncoding];
dispatch_async(dispatch_get_main_queue(), ^{
if (data) {
NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
NSString* nickName = [dic objectForKey:@"nickname"];
NSString* wxHeadImgUrl = [dic objectForKey:@"headimgurl"];
NSLog(@"nickName = %@",nickName);
NSLog(@"headImg = %@",wxHeadImgUrl);
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
[userDefaults setObject:ON forKey:LogState];
[userDefaults setObject:ThirdFoudationLogin forKey:LogType];
[userDefaults setObject:nickName forKey:LoginName];
[userDefaults setObject:wxHeadImgUrl forKey:UserHeaderPath];
[userDefaults synchronize];
[MBProgressHUD hideAllHUDsForView:self.view animated:YES];
[self msgHint:@"微信登陆成功"];
[self popView];
}
});
});
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有