第三方登陆:微信官方登陆
第三方登陆:微信官方登陆
<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key>
<true/>
</dict>
<key>LSApplicationQueriesSchemes</key>
<array>
<string>weixin</string>
<string>wechat</string>
</array>
- //
- // AppDelegate.h
- // WeiXinLogin
- //
- // Created by 王园园 on 16/6/26.
- // Copyright © 2016年 Wangyuanyuan. All rights reserved.
- //
- #import <UIKit/UIKit.h>
- #import "WXApi.h"
- @protocol WXDelegate <NSObject>
- - (void)loginSuccessByCode:(NSString *)code;
- @end
- @interface AppDelegate : UIResponder <UIApplicationDelegate,WXApiDelegate>
- @property (strong, nonatomic) UIWindow *window;
- ///声明微信代理属性
- @property (nonatomic,assign)id<WXDelegate>wxDelegate;
- @end
- //
- // AppDelegate.m
- // WeiXinLogin
- //
- // Created by 王园园 on 16/6/26.
- // Copyright © 2016年 Wangyuanyuan. All rights reserved.
- //
- #import "AppDelegate.h"
- #define weixinLoginAppId @""
- #define weixinLoginAppSecret @""
- @interface AppDelegate ()
- @end
- @implementation AppDelegate
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
- // Override point for customization after application launch.
- //向微信注册APPID
- [WXApi registerApp:weixinLoginAppId withDescription:@"wechat"];
- return YES;
- }
- #pragma mark - 微信登陆
- /*! @brief 处理微信通过URL启动App时传递的数据
- *
- * 需要在 application:openURL:sourceApplication:annotation:或者application:handleOpenURL中调用。
- * @param url 微信启动第三方应用时传递过来的URL
- * @param delegate WXApiDelegate对象,用来接收微信触发的消息。
- * @return 成功返回YES,失败返回NO。
- */
- - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {
- return [WXApi handleOpenURL:url delegate:self];
- }
- /*! 微信回调,不管是登录还是分享成功与否,都是走这个方法 @brief 发送一个sendReq后,收到微信的回应
- *
- * 收到一个来自微信的处理结果。调用一次sendReq后会收到onResp。
- * 可能收到的处理结果有SendMessageToWXResp、SendAuthResp等。
- * @param resp具体的回应内容,是自动释放的
- */
- -(void) onResp:(BaseResp*)resp
- {
- // [[NSNotificationCenter defaultCenter]postNotificationName:@"weixinNoticationAboutLogin" object: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 nil];
- [alert show];
- }
- }
- }
- @end
- //
- // ViewController.m
- // WeiXinLogin
- //
- // Created by 王园园 on 16/6/26.
- // Copyright © 2016年 Wangyuanyuan. All rights reserved.
- //
- #import "ViewController.h"
- #import "WXApi.h"
- #import "AppDelegate.h"
- #define weixinLoginAppId @""
- #define weixinLoginAppSecret @""
- @interface ViewController ()<WXDelegate>
- @property (nonatomic,strong)AppDelegate *appDelegate;
- @end
- @implementation ViewController
- - (void)viewDidLoad {
- [super viewDidLoad];
- // Do any additional setup after loading the view, typically from a nib.
- //在微信开发者平台:应用管理中心/移动应用 获取AppID
- UIButton *weiXinLoginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
- [weiXinLoginBtn setTitle:@"微信登陆" forState:UIControlStateNormal];
- weiXinLoginBtn.backgroundColor = [UIColor cyanColor];
- [weiXinLoginBtn addTarget:self action:@selector(weiXinLoginButtonAction) forControlEvents:UIControlEventTouchUpInside];
- weiXinLoginBtn.frame = CGRectMake(100, 100, 100, 100);
- [self.view addSubview:weiXinLoginBtn];
- }
- #pragma mark - 登陆按钮的响应方法
- - (void)weiXinLoginButtonAction{
- //解决isWXAppInstalled的值不准确,需要iOS9白名单适配:https://github.com/ChenYilong/iOS9AdaptationTips#常见-url-scheme
- if ([WXApi isWXAppInstalled])
- {
- //构造SendAuthReq结构体
- SendAuthReq* req =[[SendAuthReq alloc ] init] ;
- req.scope = @"snsapi_userinfo";
- req.openID = weixinLoginAppId;
- req.state = @"1245";
- _appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
- _appDelegate.wxDelegate = self;
- //第三方向微信终端发送一个SendAuthReq消息结构
- [WXApi sendReq:req];
- }else{
- UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"您没有安装微信,是否去下载" preferredStyle:UIAlertControllerStyleAlert];
- UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
- }];
- UIAlertAction *otherAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
- }];
- // Add the actions.
- [alertController addAction:cancelAction];
- [alertController addAction:otherAction];
- [self presentViewController:alertController animated:YES completion:nil];
- }
- }
- #pragma mark - 此方法是,当用户点击允许授权之后,注册的通知的方法
- //微信 成功回调
- -(void)loginSuccessByCode:(NSString *)code{
- //第一步 得到code
- NSLog(@"code %@",code);
- //第二步 通过code获取access_token
- NSString * grantStr = @"grant_type=authorization_code";
- //通过code获取access_token https://api.weixin.qq.com/sns/oauth2/access_token?appid=APPID&secret=SECRET&code=CODE&grant_type=authorization_code
- NSString * tokenUrl = @"https://api.weixin.qq.com/sns/oauth2/access_token?";
- NSString * tokenUrl1 = [tokenUrl stringByAppendingString:[NSString stringWithFormat:@"appid=%@&",weixinLoginAppId]];
- NSString * tokenUrl2 = [tokenUrl1 stringByAppendingString:[NSString stringWithFormat:@"secret=%@&",weixinLoginAppSecret]];
- NSString * tokenUrl3 = [tokenUrl2 stringByAppendingString:[NSString stringWithFormat:@"code=%@&",code]];
- NSString * tokenUrlend = [tokenUrl3 stringByAppendingString:grantStr];
- NSLog(@"%@",tokenUrlend);
- //请求:获取access_token 和 openid
- NSURL *url = [NSURL URLWithString:tokenUrlend];
- NSURLRequest * request = [[NSURLRequest alloc]initWithURL:url];
- __weak typeof(self)weakSelf = self;
- [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
- //转换为字典
- NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
- NSString * access_token = [dic objectForKey:@"access_token"];
- //NSString * expires_in = [dic objectForKey:@"expires_in"];
- //NSString * refresh_token = [dic objectForKey:@"refresh_token"];
- NSString * openid = [dic objectForKey:@"openid"];
- [weakSelf requestUserInfoByToken:access_token andOpenid:openid];
- }];
- }
- #pragma mark - 根据accessToken和openID获取用户信息
- -(void)requestUserInfoByToken:(NSString *)token andOpenid:(NSString *)openID{
- //第三步:通过access_token得到昵称、unionid等信息
- NSString * userfulStr = [NSString stringWithFormat:@"https://api.weixin.qq.com/sns/userinfo?access_token=%@&openid=%@",token,openID];
- NSURL * userfulUrl = [NSURL URLWithString:userfulStr];
- NSURLRequest * userfulRequest = [[NSURLRequest alloc]initWithURL:userfulUrl];
- [NSURLConnection sendAsynchronousRequest:userfulRequest queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
- //转换为字典
- NSDictionary *userfulResultDic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
- NSString * weixinOpenid = [userfulResultDic objectForKey:@"openid"];
- NSString * unionidStr = [userfulResultDic objectForKey:@"unionid"];//每个用户所对应的每个开放平台下的每个应用是同一个唯一标识
- NSString * langue = [userfulResultDic objectForKey:@"language"];
- NSString * nickStr = [userfulResultDic objectForKey:@"nickname"];
- NSString * filePath = [userfulResultDic objectForKey:@"headimgurl"];
- NSString * gender = [userfulResultDic objectForKey:@"sex"];
- NSString *sex;
- if ([gender intValue] == 1) {
- sex = @"1";
- }else if ([gender intValue] == 2){
- sex = @"0";
- }
- NSString * province = [userfulResultDic objectForKey:@"province"];
- NSString * city = [userfulResultDic objectForKey:@"city"];
- }];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- // Dispose of any resources that can be recreated.
- }
- @end
第三方登陆:微信官方登陆相关推荐
- uni-app第三方登陆-微信
uni-app第三方登陆-微信 结合上文全局登陆校验,实现微信授权登录 官方手册地址: https://uniapp.dcloud.io/api/plugins/login?id=getuserinf ...
- 微信授权登陆接入第三方App(步骤总结)Android。
这几天开发要用到微信授权的功能,所以就研究了一下.可是微信开放平台接入指南里有几个地方写的不清不楚.在此总结一下,以便需要的人. 很多微信公众平台的应用如果移植到app上的话就需要微信授权登陆了. 目 ...
- 如何实现自有App上的小程序第三方微信授权登陆?
对于微信小程序来说,有 OpenID 或 UnionID 作为唯一标识,微信授权登陆小程序账号是很容易实现的,但对于其他应用上的小程序来说(如支付宝.百度等),打通该登陆方式是比较麻烦的. 之前在Fi ...
- 微信授权登陆接入第三方App(步骤总结)Android
微信授权登陆接入第三方App(步骤总结) 这几天开发要用到微信授权的功能,所以就研究了一下.可是微信开放平台接入指南里有几个地方写的不清不楚.在此总结一下,以便需要的人. 很多微信公众平台的应用如果移 ...
- 微信授权登陆介入第三方APP
这几天开发要用到微信授权的功能,所以就研究了一下.可是微信开放平台接入指南里有几个地方写的不清不楚.在此总结一下,以便需要的人. 很多微信公众平台的应用如果移植到app上的话就需要微信授权登陆了. 目 ...
- joomla QQ登录,微信登录,微博登陆,人人登陆,明道登录的第三方登录
在joomla网站上集成QQ,微信,支付宝,人人,明道,短信,微博登录现在已经成为可能.只需要安装ZMAX程序人开发的ZMAX第三方登录组件,一键就可以让你的网站轻松集成当前流行的第三方登录. 国产开 ...
- 【坑爹微信】微信开发基础 --- 微信快捷登陆问题解决
目录 List 那一抹淡淡的忧伤-–微信开发基础 用纯js是不可能用纯js了,这辈子都不用纯js了 -– 微信JSSDK开发以及问题解答 要你命3000 -- 微信支付开发系列问题解决 导语 微信快捷 ...
- 微信小程序之微信登陆 —— 微信小程序教程系列(20)
简介: 微信登陆,在新建一个微信小程序Hello World项目的时候,就可以看到项目中出现了我们的微信头像,其实这个Hello World项目,就有一个简化版的微信登陆.只不过是,还没有写入到咱们自 ...
- 服务器挂微信显示登陆异常怎么办,王者荣耀微信大区登录异常怎么办 王者荣耀微信提示网络繁忙/登录失败如何解决...
导 读 相信有不少的王者荣耀玩家们刚刚都遇到了微信大区登录异常的问题吧?因此肯定想要知道该怎么办,所以下面就来为各位王者荣耀的玩家们就微信提示的网络繁忙以及登录失败问题做一番讲解,希望能够帮到各位让你 ...
最新文章
- 2016.01.04 论文改重
- 马斯克说到做到!特斯拉牌呼吸机“交货”,用Model 3零部件打造;网友:我滴神啊...
- Hologres是如何完美支撑双11智能客服实时数仓的?
- BZOJ-1968 COMMON 约数研究 数论+奇怪的姿势
- Pots (BFS ➕ 输出路径)
- x509trustmanager怎么验证证书_GeoTrust通配符证书和多域名SSL证书对比评测
- 关于Django中JsonResponse返回中文字典编码错误的解决方案
- 45:十进制到八进制(OpenJudge在线题库 :NOI / 1.13编程基础之综合应用)
- Android搜索手机文件
- k8s边缘节点_KubeEdge v0.2发布,全球首个K8S原生的边缘计算平台开放云端代码
- 搜狗推出庭审语音识别系统 人工智能下的全新应用
- Android dalvik GC相关的属性详解
- 不要以为写写代码就是计算机科学
- 正则表达式语法和常用表达式列表
- uvalive 3713 Astronauts
- [转帖]从壹开始前后端分离【重要】║最全的部署方案 最丰富的错误分析
- http 503 service
- C++ 字符跑酷#3 游戏制作实录
- 苹果手机无法验证应用怎么办_苹果关闭iOS 14.0.1验证,用户将无法降级_
- Python利用selenium简单的爬取网易云歌曲排行榜