一:下载SDK
https://open.weixin.qq.com/cgi-bin/frame?t=resource/res_main_tmpl&verify=1&lang=zh_CN&target=res/app_download_ios
二:配置环境
将文件拖入工程
配置URL scheme
三:DEFINE
1 #ifndef LoginDemo_Define_h 2 #define LoginDemo_Define_h 3 4 #define kAppDescription @“*******" 5 6 #define kWeiXinAppId @“wx**************" 7 #define kWeiXinAppSecret @“4f85d************011afa8a23d5a" 8 9 #define kWeiXinAccessToken @"WeiXinAccessToken"10 #define kWeiXinOpenId @"WeiXinOpenId"11 #define kWeiXinRefreshToken @"WeiXinRefreshToken"12 13 14 #endif
四:AppDelegate.h
1 #import 2 3 #import "WeChatSDK_64/WXApi.h"4 5 @interface AppDelegate : UIResponder 6 7 @property (strong, nonatomic) UIWindow *window;8 9 @end
五:AppDelegate.m
1 #import "AppDelegate.h" 2 #import "RootViewController.h" 3 #import "Define.h" 4 5 @interface AppDelegate () 6 { 7 RootViewController *root; 8 } 9 @end10 11 @implementation AppDelegate12 13 14 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions15 {16 //….17 18 root = [[RootViewController alloc]init];19 self.window.rootViewController = root;20 21 [WXApi registerApp:kWeiXinAppId withDescription:kAppDescription];22 23 return YES;24 }25 26 - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url27 {28 return [WXApi handleOpenURL:url delegate:self];29 }30 31 - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation32 {33 return [WXApi handleOpenURL:url delegate:self];34 }35 36 - (void)onReq:(BaseReq*)req37 {38 41 }42 43 - (void)onResp:(BaseResp*)resp44 {45 48 [root.weixinViewController getWeiXinCodeFinishedWithResp:resp];49 }
六:发起授权请求
1 - (void)loginButtonClicked2 {3 SendAuthReq* req =[[SendAuthReq alloc ] init];4 req.scope = @"snsapi_userinfo";5 req.state = kAppDescription;6 [WXApi sendReq:req];7 }
调用方法后会弹出授权页面,完成授权后调用AppDelegate中的- (void)onResp:(BaseResp*)resp
七:处理返回数据,获取code
1 - (void)getWeiXinCodeFinishedWithResp:(BaseResp *)resp 2 { 3 if (resp.errCode == 0) 4 { 5 statusCodeLabel.text = @"用户同意"; 6 SendAuthResp *aresp = (SendAuthResp *)resp; 7 [self getAccessTokenWithCode:aresp.code]; 8 9 }else if (resp.errCode == -4){10 statusCodeLabel.text = @"用户拒绝";11 }else if (resp.errCode == -2){12 statusCodeLabel.text = @"用户取消";13 }14 }
八:使用code获取access token
1 - (void)getAccessTokenWithCode:(NSString *)code 2 { 3 NSString *urlString =[NSString stringWithFormat:@"https://api.weixin.qq.com/sns/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code",kWeiXinAppId,kWeiXinAppSecret,code]; 4 NSURL *url = [NSURL URLWithString:urlString]; 5 6 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 7 8 NSString *dataStr = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil]; 9 NSData *data = [dataStr dataUsingEncoding:NSUTF8StringEncoding];10 11 dispatch_async(dispatch_get_main_queue(), ^{12 13 if (data)14 {15 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];16 17 if ([dict objectForKey:@"errcode"])18 {19 //获取token错误 20 }else{ 21 //存储AccessToken OpenId RefreshToken以便下次直接登陆22 //AccessToken有效期两小时,RefreshToken有效期三十天 23 [self getUserInfoWithAccessToken:[dict objectForKey:@"access_token"] andOpenId:[dict objectForKey:@"openid"]];24 }25 }26 });27 });28 29 37 38 43 }
九:使用AccessToken获取用户信息
1 - (void)getUserInfoWithAccessToken:(NSString *)accessToken andOpenId:(NSString *)openId 2 { 3 NSString *urlString =[NSString stringWithFormat:@"https://api.weixin.qq.com/sns/userinfo?access_token=%@&openid=%@",accessToken,openId]; 4 NSURL *url = [NSURL URLWithString:urlString]; 5 6 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 7 8 NSString *dataStr = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil]; 9 NSData *data = [dataStr dataUsingEncoding:NSUTF8StringEncoding];10 11 dispatch_async(dispatch_get_main_queue(), ^{12 13 if (data)14 {15 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];16 17 if ([dict objectForKey:@"errcode"])18 {19 //AccessToken失效20 [self getAccessTokenWithRefreshToken:[[NSUserDefaults standardUserDefaults]objectForKey:kWeiXinRefreshToken]];21 }else{22 //获取需要的数据 23 }24 }25 });26 });27 28 41 42 47 }
十:使用RefreshToken刷新AccessToken
该接口调用后,如果AccessToken未过期,则刷新有效期,如果已过期,更换AccessToken。
1 - (void)getAccessTokenWithRefreshToken:(NSString *)refreshToken 2 { 3 NSString *urlString =[NSString stringWithFormat:@"https://api.weixin.qq.com/sns/oauth2/refresh_token?appid=%@&grant_type=refresh_token&refresh_token=%@",kWeiXinAppId,refreshToken]; 4 NSURL *url = [NSURL URLWithString:urlString]; 5 6 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ 7 8 9 NSString *dataStr = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:nil];10 NSData *data = [dataStr dataUsingEncoding:NSUTF8StringEncoding];11 12 dispatch_async(dispatch_get_main_queue(), ^{13 14 if (data)15 {16 NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];17 18 if ([dict objectForKey:@"errcode"])19 {20 //授权过期 21 }else{22 //重新使用AccessToken获取信息23 }24 }25 });26 });27 28 29 36 37 42 }
微信第三方登陆