iOS开发之第三方登录微信-- 史上最全最新第三方登录微信方式实现
项目地址 : https://github.com/zhonggaorong/weixinLoginDemo
最新版本的微信登录实现步骤实现:
1.在进行微信OAuth2.0授权登录接入之前,在微信开放平台注册开发者帐号,并拥有一个已审核通过的移动应用,并获得相应的AppID和AppSecret,申请微信登录且通过审核后,可开始接入流程。 地址: 点击打开链接
2. 下载最新的SDK 地址: 点击打开链接
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文件与静态库全部拖入工程。
-canOpenURL: failed for URL: "weixin://app/wx5efead4057f98bc0/" - error: "This app is not allowed to query for scheme weixin"
就说明没有针对iOS9 增加白名单。在info.plist文件中加入 LSApplicationQueriesSchemes
//
// AppDelegate.m
// weixinLoginDemo
//
// Created by 张国荣 on 16/6/20.
// Copyright © 2016年 BateOrganization. All rights reserved.
//#import "AppDelegate.h"
#import "WXApi.h"//微信开发者ID
#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{/*! @brief 处理微信通过URL启动App时传递的数据** 需要在 application:openURL:sourceApplication:annotation:或者application:handleOpenURL中调用。* @param url 微信启动第三方应用时传递过来的URL* @param delegate WXApiDelegate对象,用来接收微信触发的消息。* @return 成功返回YES,失败返回NO。*/return [WXApi handleOpenURL:url delegate:self];
}/*! 微信回调,不管是登录还是分享成功与否,都是走这个方法 @brief 发送一个sendReq后,收到微信的回应** 收到一个来自微信的处理结果。调用一次sendReq后会收到onResp。* 可能收到的处理结果有SendMessageToWXResp、SendAuthResp等。* @param resp具体的回应内容,是自动释放的*/
-(void) onResp:(BaseResp*)resp{NSLog(@"resp %d",resp.errCode);/*enum WXErrCode {WXSuccess = 0, 成功WXErrCodeCommon = -1, 普通错误类型WXErrCodeUserCancel = -2, 用户点击取消并返回WXErrCodeSentFail = -3, 发送失败WXErrCodeAuthDeny = -4, 授权失败WXErrCodeUnsupport = -5, 微信不支持};*/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];[alert show];}}
}@end
//
// 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_tokenopenid 授权用户唯一标识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
大功告成。
iOS开发之第三方登录微信-- 史上最全最新第三方登录微信方式实现相关推荐
- iOS开发之第三方登录微博-- 史上最全最新第三方登录微博方式实现
相关资源地址: 本项目demo地址 : https://github.com/zhonggaorong/weiboSDKDemo 最新SDK下载: 最新微博SDK 官网注册地址:点击打开链接 最 ...
- iOS开发之第三方登录QQ -- 史上最全最新第三方登录QQ方式实现
项目地址 : https://github.com/zhonggaorong/QQLoginDemo/tree/master 最新版本的qq登录实现步骤实现: 1. 首先,你需要去向腾讯申请账号. ...
- 大数据开发工程师基本功修炼之史上最全Linux学习笔记(建议)
我正在参加年度博客之星评选,请大家帮我投票打分,您的每一分都是对我的支持与鼓励. 2021年「博客之星」参赛博主:Maynor大数据 (感谢礼品.红包免费送!) https://bbs.csdn.ne ...
- 对外汉语语料库有哪些_[转载]史上最全最新的语料库资源大全【对外汉语教学研究工具】...
果断收藏!分享给好友!史上最全最新的语料库资源大全.本文持续更新,欢迎订阅期刊<对外汉语北京>以便获得最新通知! [期刊] International Journal of Corpus ...
- 魔域无法连接服务器或正在维护,史上最全魔域无法登录或登录超时原因与解决办法...
本文成为今日优秀文章,获得270点可交易魔石的奖励 最近一段时间,老是听到玩家无法登录或登录超时的问题,官方给出的解释是电信和网通两大运营商串线了,还有就是新区人多的问题. 这些原因都是我们无法解决的 ...
- 史上最全最新前端面试题(不定期更新,有问题欢迎评论区对线)
一阶段面试题: CSS浮动怎么理解的 浮动的框可以向左或向右移动,直到它的外边缘碰到包含框或另一个浮动框的边框为止. 由于浮动框不在文档的普通流中,也就是脱离文档流,所以文档的普通流中的块框表现得就像 ...
- 史上最全最新微信小程序自动化教程
背景 微信小程序作为现在流行的一种应用载体,很多小伙伴都有对其做自动化测试的需求,由于腾讯系QQ.微信等是基于腾讯自研X5内核,不是谷歌原生webview,所以调试会有些许差异(现在很多app产品也开 ...
- SpringBoot+SpringSecurity+JWT整合实现单点登录SSO史上最全详解
作者:波波烤鸭 blog.csdn.net/qq_38526573/article/details/103409430 一.什么是单点登陆 单点登录(Single Sign On),简称为 SSO,是 ...
- 应该是史上最全最新Java和Android面试题目(自己总结和收集的)
Android面试题目 Java 基础 int占用几个字节 讲一下常见编码方式? UTF-8编码下中文占几个字节 int和Interger的区别 int.char.long各占多少字节数 string ...
- Java、eclipse、html、mysql 基础常见英语词汇及快捷键(2018史上最全最新最有用) 萌芽call - CSDN博客园
第一章: JDK java开发工具包 Jvm java虚拟机 Javac 编译命令 Java 高级编程语言 Classpath 类路径 Version 版本 author 作者 eclipse 开发工 ...
最新文章
- tp5.0 queue 队列操作
- 科学计算机乱码,软件界面乱码可以这么“破”
- bzoj2561: 最小生成树
- 聊聊高并发(四十)解析java.util.concurrent各个组件(十六) ThreadPoolExecutor源码分析
- java日期工具类DateUtil
- Ambari--告警管理
- 1. 数组之差TapeEquilibrium Minimize the value |(A[0] + ... + A[P-1]) - (A[P] + ... + A[N-1])|.
- 2021 CCF网络推荐会议时间列表
- ApacheMaven使用技巧
- 圆通快递单css样式
- java 比较源文件_Beyond Compare比较Java源代码文件的详细操作方法
- Fabric实战(10)链码(chaincode)开发-shim包API
- OKR-Periods of Words
- springboot实现pdf里面插入图片
- 手把手教你如何微信公众号开发
- pyqt QLineEdit 详细用法
- 使用v2xray或者ss时,显示“500 internal prioxy error”错误
- TSC打印机打印条形码和二维码,java实现方式
- centos7防火墙开放端口,附开放samba特定端口实例
- 论文,更不能培养自己研究问题的能力。