美文网首页
iOS微信登陆根据token获取用户信息

iOS微信登陆根据token获取用户信息

作者: 携一两本单色书来 | 来源:发表于2019-01-30 18:14 被阅读16次
    + (void)getWechatAccessTokenWithCode:(NSString *)code Hnadle:(void (^)(NSString *))handle
        {
            
            NSMutableDictionary *loginDatas = [NSMutableDictionary new];
            NSString *url =[NSString stringWithFormat:@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code",wx_id,wx_secret,code];
            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];
                        
                        [loginDatas setObject:dic[@"access_token"] forKey:@"access_token"];
                        [loginDatas setObject:dic[@"openid"] forKey:@"openid"];
                        [loginDatas setObject:dic[@"refresh_token"] forKey:@"refresh_token"];
                        
                        //根据accesstoken和openid获取用户信息
                        NSString *url =[NSString stringWithFormat:@"https://api.weixin.qq.com/sns/userinfo?access_token=%@&openid=%@",dic[@"access_token"], dic[@"openid"]];
                        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];
                                    
                                    [loginDatas setObject:dic[@"nickname"] forKey:@"nickname"];
                                    [loginDatas setObject:dic[@"unionid"] forKey:@"unionid"];
                                    
                                    handle([WXHelper convertToJsonData:loginDatas]);
                                    
                                }
                            });
                        });
                    }
                });
            });
        }
        
    #pragma mark - Tools 字典转json
    +(NSString *)convertToJsonData:(NSDictionary *)dict{
        
        NSError *error;
        
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dict options:NSJSONWritingPrettyPrinted error:&error];
        
        NSString *jsonString;
        
        if (!jsonData) {
            
            NSLog(@"%@",error);
            
        }else{
            
            jsonString = [[NSString alloc]initWithData:jsonData encoding:NSUTF8StringEncoding];
            
        }
        
        NSMutableString *mutStr = [NSMutableString stringWithString:jsonString];
        
        NSRange range = {0,jsonString.length};
        
        //去掉字符串中的空格
        
        [mutStr replaceOccurrencesOfString:@" " withString:@"" options:NSLiteralSearch range:range];
        
        NSRange range2 = {0,mutStr.length};
        
        //去掉字符串中的换行符
        
        [mutStr replaceOccurrencesOfString:@"\n" withString:@"" options:NSLiteralSearch range:range2];
        
        return mutStr;
        
    }
    

    相关文章

      网友评论

          本文标题:iOS微信登陆根据token获取用户信息

          本文链接:https://www.haomeiwen.com/subject/cvbcsqtx.html