项目地址 : https://github.com/zhonggaorong/weixinLoginDemo
最新版本的微信登录实现步骤实现:
1.在进行微信OAuth2.0授权登录接入之前,在微信开放平台注册开发者帐号,并拥有一个已审核通过的移动应用,并获得相应的AppID和AppSecret,申请微信登录且通过审核后,可开始接入流程。 地址: 点击打开链接
2. 下载最新的SDK 地址: 点击打开链接
SDK内容如下:
结构解析:
从上到下依次说明:
1. 静态库,直接拖入工程。
2. ready.text自己看
3. 授权SDK。
4. 登录方法所在类。
5. 一些常用的对象类。
iOS微信登录注意事项:
- 1、目前移动应用上微信登录只提供原生的登录方式,需要用户安装微信客户端才能配合使用。
- 2、对于Android应用,建议总是显示微信登录按钮,当用户手机没有安装微信客户端时,请引导用户下载安装微信客户端。
- 3、对于iOS应用,考虑到iOS应用商店审核指南中的相关规定,建议开发者接入微信登录时,先检测用户手机是否已安装微信客户端(使用sdk中isWXAppInstalled函数 ),对未安装的用户隐藏微信登录按钮,只提供其他登录方式(比如手机号注册登录、游客登录等)。
iOS微信登录大致流程:
- 1. 第三方发起微信授权登录请求,微信用户允许授权第三方应用后,微信会拉起应用或重定向到第三方网站,并且带上授权临时票据code参数;
- 2. 通过code参数加上AppID和AppSecret等,通过API换取access_token;
- 3. 通过access_token进行接口调用,获取用户基本数据资源或帮助用户实现基本操作。
示意图:

接下来就进入正题:
1.配置工程
1. 新建一个工程。
2. 把下载下来的sdk中的.h文件与静态库全部拖入工程。
3. 加入依赖库
4. URL - Types (加入 appid)
target - Info - URL Types
5. 白名单
当程序出现此错误
- -canOpenURL: failed for URL: "weixin://app/wx5efead4057f98bc0/" - error: "This app is not allowed to query for scheme weixin"
就说明没有针对iOS9 增加白名单。在info.plist文件中加入 LSApplicationQueriesSchemes
App Transport Security 这个是让程序还是用http进行请求。
LSApplicationQueriesSchemes 这个是增加微信的白名单。
6. 现在编译应该是没有问题了。
2. 终于到令人兴奋的代码部分了。 直接上代码。
-
-
-
-
-
-
-
-
- #import "AppDelegate.h"
- #import "WXApi.h"
-
-
- #define URL_APPID @"app id"
-
- @end
-
-
-
- @implementation AppDelegate
-
-
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
-
-
- [WXApi registerApp:URL_APPID withDescription:@"wechat"];
- return YES;
- }
-
- -(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<NSString *,id> *)options{
-
-
-
-
-
-
-
-
-
- return [WXApi handleOpenURL:url delegate:self];
- }
-
-
-
-
-
-
-
-
- -(void) onResp:(BaseResp*)resp{
- NSLog(@"resp %d",resp.errCode);
-
-
-
-
-
-
-
-
-
-
-
- if ([resp isKindOfClass:[SendAuthResp class]]) {
- if (resp.errCode == 0) {
-
- if ([_wxDelegate respondsToSelector:@selector(loginSuccessByCode:)]) {
- SendAuthResp *resp2 = (SendAuthResp *)resp;
- [_wxDelegate loginSuccessByCode:resp2.code];
- }
- }else{
- NSLog(@"error %@",resp.errStr);
- UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"登录失败" message:[NSString stringWithFormat:@"reason : %@",resp.errStr] delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil nil];
- [alert show];
- }
- }
- }
-
- @end
下面是登录的类。
-
-
-
-
-
-
-
-
- #import "ViewController.h"
- #import "WXApi.h"
- #import "AppDelegate.h"
-
- #define URL_APPID @"appid"
- #define URL_SECRET @"app secret"
- #import "AFNetworking.h"
- @interface ViewController ()<WXDelegate>
- {
- AppDelegate *appdelegate;
- }
- @end
-
- @implementation ViewController
-
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- }
- #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 nil];
-
- [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) {
-
- NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
- NSLog(@"dic %@",dic);
-
-
-
-
-
-
-
-
-
- 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];
-
- }
-
- @end
大功告成。