Ios@IONICiOS开发技巧

iOS 微信第三方登录的简单实现

2016-11-08  本文已影响2623人  Japho

下载微信SDK

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

微信开放平台

导入SDK

导入SDK,并添加依赖库

配置URL scheme

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

配置Url Scheme

开始编写代码

#import <UIKit/UIKit.h>  
#import "WXApi.h"  
  
@interface AppDelegate : UIResponder <UIApplicationDelegate,WXApiDelegate>  
  
@property (strong, nonatomic) UIWindow *window;  
  
@end  
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {  
    // Override point for customization after application launch.  
      
    [WXApi registerApp:@"这里填写你的AppID"];  
      
    return YES;  
}  
//和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];  
}  
- (void)wechatLoginButtonPressed  
{  
    NSLog(@"%s",__func__);  
      
    //构造SendAuthReq结构体  
    SendAuthReq* req =[[SendAuthReq alloc] init];  
    req.scope = @"snsapi_userinfo" ;  
    req.state = @"123" ;  
    //第三方向微信终端发送一个SendAuthReq消息结构  
    [WXApi sendReq:req];  
}  
//授权后回调 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

具体步骤:
刚刚我们已经在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];  
            }  
        });  
          
    });  
}  

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

效果图:

上一篇 下一篇

猜你喜欢

热点阅读