iOS開發之第三方登入微信-- 史上最全最新第三方登入微信方式實現
阿新 • • 發佈:2019-02-01
大功告成。// // ViewController.m // weixinLoginDemo // // Created by 張國榮 on 16/6/20. // Copyright © 2016年 BateOrganization. All rights reserved. // #import "ViewController.h" #import "WXApi.h" #import "AppDelegate.h" //微信開發者ID #define URL_APPID @"appid" #define URL_SECRET @"app secret" #import "AFNetworking.h" @interface ViewController ()<WXDelegate> { AppDelegate *appdelegate; } @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. } #pragma mark 微信登入 - (IBAction)weixinLoginAction:(id)sender { if ([WXApi isWXAppInstalled]) { SendAuthReq *req = [[SendAuthReq alloc]init]; req.scope = @"snsapi_userinfo"; req.openID = URL_APPID; req.state = @"1245"; appdelegate = [UIApplication sharedApplication].delegate; appdelegate.wxDelegate = self; [WXApi sendReq:req]; }else{ //把微信登入的按鈕隱藏掉。 } } #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]; //通過 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",URL_APPID,URL_SECRET,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); /* 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) { NSLog(@"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]; NSLog(@"dic ==== %@",dic); } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { NSLog(@"error %ld",(long)error.code); }]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end