iOS

iOS 微信第三方登录的实现

原生微信SDK开发

Posted by Japho on April 18, 2016

下载微信SDK

微信开放平台 https://open.weixin.qq.com

微信开放平台

导入SDK

导入SDK,并添加依赖库

配置URL scheme

在Xcode中,选择你的工程设置项,选中TARGETS一栏,在info标签栏的URL type添加URL scheme为你所注册的应用程序id(如下图所示),此步为配置应用间的跳转。

配置Url Scheme

开始编写代码

  • Appdelegate.h中引用微信文件,声明遵循代理。

#import <UIKit/UIKit.h>  
#import "WXApi.h"  
  
@interface AppDelegate : UIResponder <UIApplicationDelegate,WXApiDelegate>  
  
@property (strong, nonatomic) UIWindow *window;  
  
@end


  • 注册SDK要使你的程序启动后微信终端能响应你的程序,必须在代码中向微信终端注册你的id。(在 AppDelegatedidFinishLaunchingWithOptions 函数中向微信注册id)。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
    // Override point for customization after application launch.  
      
    [WXApi registerApp:@"这里填写你的AppID"];  
      
    return YES;  
}  


  • 重写AppDelegatehandleOpenURLopenURL方法

//和QQ,新浪并列回调句柄  
- (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation  
{  
    return [WXApi handleOpenURL:url delegate:self];  
}  
  
- (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url  
{  
    return [WXApi handleOpenURL:url delegate:self];  
}  


  • 调用微信API,调用微信登录

scope : 应用授权作用域,如获取用户个人信息则填写snsapi_userinfo

state : 用于保持请求和回调的状态,授权请求后原样带回给第三方。该参数可用于防止csrf攻击(跨站请求伪造攻击),建议第三方带上该参数,可设置为简单的随机数加session进行校验(非必填)。


- (void)wechatLoginButtonPressed  
{  
    NSLog(@"%s",__func__);  
      
    //构造SendAuthReq结构体  
    SendAuthReq* req =[[SendAuthReq alloc] init];  
    req.scope = @"snsapi_userinfo" ;  
    req.state = @"123" ;  
    //第三方向微信终端发送一个SendAuthReq消息结构  
    [WXApi sendReq:req];  
}  

  • Appdelegate中实现微信的代理,获取微信返回的code,这里我使用了通知的方法来调用登录controller中的相应才做,也可以使用代理、KVO等方式来实现。

//授权后回调 WXApiDelegate  
-(void)onResp:(BaseReq *)resp  
{  
    /* 
     ErrCode ERR_OK = 0(用户同意) 
     ERR_AUTH_DENIED = -4(用户拒绝授权) 
     ERR_USER_CANCEL = -2(用户取消) 
     code    用户换取access_token的code,仅在ErrCode为0时有效 
     state   第三方程序发送时用来标识其请求的唯一性的标志,由第三方程序调用sendReq时传入,由微信终端回传,state字符串长度不能超过1K 
     lang    微信客户端当前语言 
     country 微信用户当前国家信息 
     */  
  
    if ([resp isKindOfClass:[SendAuthResp class]]) //判断是否为授权请求,否则与微信支付等功能发生冲突  
    {  
        SendAuthResp *aresp = (SendAuthResp *)resp;  
        if (aresp.errCode== 0)  
        {  
            NSLog(@"code %@",aresp.code);  
            [[NSNotificationCenter defaultCenter] postNotificationName:@"wechatDidLoginNotification" object:self userInfo:@{@"code":aresp.code}];  
        }  
    }  
}  


相对应注册通知方法:


 //跳转到主界面
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(wechatDidLoginNotification:) name:@"wechatDidLoginNotification" object:nil];


记得在dealloc方法中移除通知,避免发生冲突:


- (void)dealloc  
{  
    [[NSNotificationCenter defaultCenter] removeObserver:self name:@"wechatDidLoginNotification" object:nil];  
}  


微信登录认证后获取用户的个人信息比较麻烦,需要三个步骤:

  • 获取微信登录code
  • 根据code获取accessToken和openId
  • 根据accessToken和openId获取用户信息

具体步骤:

刚刚我们已经在appdelegate中微信的代理中获取到了code,下面直接来进行第二步,根据code获取accessTokenopenId: 参照微信开放平台官方文档:


- (void)getWechatAccessTokenWithCode:(NSString *)code  
{  
    NSString *url =[NSString stringWithFormat:  
      @"https://api.weixin.qq.com/sns/oauth2/access_token?appid=%@&secret=%@&code=%@&grant_type=authorization_code",  
        WechatAppKey,WechatSecrectKey,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];  
                  
                NSLog(@"%@",dic);  
                NSString *accessToken = dic[@"access_token"];  
                NSString *openId = dic[@"openid"];  
                  
                [self getWechatUserInfoWithAccessToken:accessToken openId:openId];  
            }  
        });  
    });  
}  


现在已经获取了微信的accessTokenopenId,就可以请求相应的微信接口了。 参照文档,我们使用以下接口:

使用这个接口就可以获取用户信息了,然后调用自己的方法进行登录,这里可以使用openId当做账号,他是每个微信用户唯一对应的id


- (void)getWechatUserInfoWithAccessToken:(NSString *)accessToken openId:(NSString *)openId  
{  
    NSString *url =[NSString stringWithFormat:  
                    @"https://api.weixin.qq.com/sns/userinfo?access_token=%@&openid=%@",accessToken,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];  
                  
                NSLog(@"%@",dic);  
                  
                NSString *openId = [dic objectForKey:@"openid"];  
                NSString *memNickName = [dic objectForKey:@"nickname"];  
                NSString *memSex = [dic objectForKey:@"sex"];  
                  
                [self loginWithOpenId:openId memNickName:memNickName memSex:memSex];  
            }  
        });  
          
    });  
} 


至此,就实现了简单的微信登录

效果图: