第四十一篇:微信第三方登录

2024-05-12 20:08

本文主要是介绍第四十一篇:微信第三方登录,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

登录前效果图:


登录后效果图:


一、准备工作

第一步:下载友盟SDK,下载友盟SDK 根据需要选择下载,下载完成后把 UMSocial 文件添加到工程中;

第二步:添加配置,在 Build Settings --> Linking --> Other Linker Flags --> Debug 或 Release 对应的行后加入-ObjC ,

第三步:在项目的.plist 文件中添加如下配制,使允许网络加载


第四步:添加在项目中的所有 SDK 所需的 依懒库,下列是一些常见的登录或分享所需的依懒库

libsqlite3.tbd        // 这两个是必须添加的
CoreGraphics.framework // 微信的依懒库
SystemConfiguration.framework
CoreTelephony.framework
libsqlite3.tbd
libc++.tbd
libz.tbd// QQ 的依懒库
SystemConfiguration.framework
libc++.tbd // 新浪微博的依懒库
CoreTelephony.framework
ImageIO.framework
libsqlite3.tbd
libz.tbd // 短信 的依懒库
MessageUI.framework

第五步:配制SSO白名单

         如果你的应用使用了如SSO授权登录或跳转到第三方分享功能,在iOS9/10下就需要增加一个可跳转的白名单,即LSApplicationQueriesSchemes,否则将在SDK判断是否跳转时用到的canOpenURL时返回NO,进而只进行webview授权或授权/分享失败。 在项目中的info.plist中加入应用白名单,右键info.plist选择source code打开(plist具体设置在Build Setting -> Packaging -> Info.plist File可获取plist路径) 请根据选择的平台对以下配置进行裁剪:

<key>LSApplicationQueriesSchemes</key>
<array><!-- 微信 URL Scheme 白名单--><string>wechat</string><string>weixin</string><!-- 新浪微博 URL Scheme 白名单--><string>sinaweibohd</string><string>sinaweibo</string><string>sinaweibosso</string><string>weibosdk</string><string>weibosdk2.5</string><!-- QQ、Qzone URL Scheme 白名单--><string>mqqapi</string><string>mqq</string><string>mqqOpensdkSSoLogin</string><string>mqqconnect</string><string>mqqopensdkdataline</string><string>mqqopensdkgrouptribeshare</string><string>mqqopensdkfriend</string><string>mqqopensdkapi</string><string>mqqopensdkapiV2</string><string>mqqopensdkapiV3</string><string>mqqopensdkapiV4</string><string>mqzoneopensdk</string><string>wtloginmqq</string><string>wtloginmqq2</string><string>mqqwpa</string><string>mqzone</string><string>mqzonev2</string><string>mqzoneshare</string><string>wtloginqzone</string><string>mqzonewx</string><string>mqzoneopensdkapiV2</string><string>mqzoneopensdkapi19</string><string>mqzoneopensdkapi</string><string>mqqbrowser</string><string>mttbrowser</string><!-- 支付宝 URL Scheme 白名单--><string>alipay</string><string>alipayshare</string><!-- 钉钉 URL Scheme 白名单--><string>dingtalk</string><string>dingtalk-open</string><!--Linkedin URL Scheme 白名单--><string>linkedin</string><string>linkedin-sdk2</string><string>linkedin-sdk</string><!-- 点点虫 URL Scheme 白名单--><string>laiwangsso</string><!-- 易信 URL Scheme 白名单--><string>yixin</string><string>yixinopenapi</string><!-- instagram URL Scheme 白名单--><string>instagram</string><!-- whatsapp URL Scheme 白名单--><string>whatsapp</string><!-- line URL Scheme 白名单--><string>line</string><!-- Facebook URL Scheme 白名单--><string>fbapi</string><string>fb-messenger-api</string><string>fbauth2</string><string>fbshareextension</string><!-- Kakao URL Scheme 白名单-->  <!-- 注:以下第一个参数需替换为自己的kakao appkey--> <!-- 格式为 kakao + "kakao appkey"-->    <string>kakaofa63a0b2356e923f3edd6512d531f546</string><string>kakaokompassauth</string><string>storykompassauth</string><string>kakaolink</string><string>kakaotalk-4.5.0</string><string>kakaostory-2.9.0</string><!-- pinterest URL Scheme 白名单-->  <string>pinterestsdk.v1</string><!-- Tumblr URL Scheme 白名单-->  <string>tumblr</string><!-- 印象笔记 --><string>evernote</string><string>en</string><string>enx</string><string>evernotecid</string><string>evernotemsg</string><!-- 有道云笔记--><string>youdaonote</string><string>ynotedictfav</string><string>com.youdao.note.todayViewNote</string><string>ynotesharesdk</string><!-- Google+--><string>gplus</string><!-- Pocket--><string>pocket</string><string>readitlater</string><string>pocket-oauth-v1</string><string>fb131450656879143</string><string>en-readitlater-5776</string><string>com.ideashower.ReadItLaterPro3</string><string>com.ideashower.ReadItLaterPro</string><string>com.ideashower.ReadItLaterProAlpha</string><string>com.ideashower.ReadItLaterProEnterprise</string><!-- VKontakte--><string>vk</string><string>vk-share</string><string>vkauthorize</string>
</array>

第六步:在项目的 plist 文件中配制 URL Scheme




二、实现第三方登录功能

       前言:现在已经有了一个空项目里面有一个友盟SDK,并做了相关的配制后,接下来就是要实现该功能。在实现之前需要自己去申请一个友盟AppKey


1.创建一个公共的文件 CommentHeader.h ,用来存放 key 值

//
//  CommentHeader.h
//  QJWeixinLogin
//
//  Created by 瞿杰 on 2017/6/15.
//  Copyright © 2017年 yiniu. All rights reserved.
//#ifndef CommentHeader_h
#define CommentHeader_h//测试微信相关参
static NSString *kTestWxAppId = @"wxdc1e388c3822c80b";static NSString *kTestWxAppSecret = @"3baf1193c85774b3fd9d18447d76cab0";//友盟AppKey
static NSString *kUMengAppKey = @"xxxxxxxxxx";#endif /* CommentHeader_h */




2.项目的 AppDelegate 文件

//
//  AppDelegate.h
//  QJWeixinLogin
//
//  Created by 瞿杰 on 2017/6/15.
//  Copyright © 2017年 yiniu. All rights reserved.
//#import <UIKit/UIKit.h>
#import <CoreData/CoreData.h>@interface AppDelegate : UIResponder <UIApplicationDelegate>@property (strong, nonatomic) UIWindow *window;@property (readonly, strong) NSPersistentContainer *persistentContainer;- (void)saveContext;@end
//
//  AppDelegate.m
//  QJWeixinLogin
//
//  Created by 瞿杰 on 2017/6/15.
//  Copyright © 2017年 yiniu. All rights reserved.
//#import "AppDelegate.h"#import "ViewController.h"
#import "CommentHeader.h"
#import <UMSocialCore/UMSocialCore.h>
#import "WXApi.h"@interface AppDelegate ()@end@implementation AppDelegate// 初始化环境
-(void)initEnvironment
{//打开调试日志[[UMSocialManager defaultManager] openLog:YES];//设置友盟AppKey[[UMSocialManager defaultManager] setUmSocialAppkey:kUMengAppKey];//设置微信的appKey和appSecret[[UMSocialManager defaultManager] setPlaform:UMSocialPlatformType_WechatSession appKey:kTestWxAppId appSecret:kTestWxAppSecret redirectURL:@"http://mobile.umeng.com/social"];
}- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {// Override point for customization after application launch.// 初始化微信登录环境[self initEnvironment];self.window = [[UIWindow alloc] init];self.window.backgroundColor = [UIColor grayColor];self.window.rootViewController = [[ViewController alloc] init];[self.window makeKeyAndVisible];return YES;
}#pragma mark - 回调
-(BOOL)application:(UIApplication *)app openURL:(NSURL *)url options:(NSDictionary<UIApplicationOpenURLOptionsKey,id> *)options
{return [[UMSocialManager defaultManager] handleOpenURL:url];
}

3.控制器文件 ViewController

//
//  ViewController.h
//  QJWeixinLogin
//
//  Created by 瞿杰 on 2017/6/15.
//  Copyright © 2017年 yiniu. All rights reserved.
//#import <UIKit/UIKit.h>@interface ViewController : UIViewController@end
//
//  ViewController.m
//  QJWeixinLogin
//
//  Created by 瞿杰 on 2017/6/15.
//  Copyright © 2017年 yiniu. All rights reserved.
//#import "ViewController.h"#import "ThirdPartyLoginTools.h"
#import "UIImageView+QJWebImage.h"@interface ViewController ()<ThirdPartyLoginToolsDelegate>@property (nonatomic , strong)UIImageView * imageView ;
@property (nonatomic , strong)UILabel * nameLabel ;
@property (nonatomic , strong)UILabel * sexLabel ;@property (nonatomic , strong)UIButton * weixinLogin ;@end@implementation ViewController- (void)viewDidLoad {[super viewDidLoad];self.view.backgroundColor = [UIColor redColor];[self setUpViews];
}-(void)setUpViews
{self.imageView = [[UIImageView alloc] init];self.imageView.frame = CGRectMake(100, 100, 100, 100);self.imageView.backgroundColor = [UIColor blueColor];[self.view addSubview:self.imageView];self.nameLabel = [[UILabel alloc] init];self.nameLabel.backgroundColor = [UIColor whiteColor];self.nameLabel.textColor = [UIColor blackColor];self.nameLabel.frame = CGRectMake(100, 210, 100, 50);self.nameLabel.text = [NSString stringWithFormat:@"用户名:xxx"];[self.view addSubview:self.nameLabel];self.sexLabel = [[UILabel alloc] init];self.sexLabel.backgroundColor = [UIColor whiteColor];self.sexLabel.textColor = [UIColor blackColor];self.sexLabel.frame = CGRectMake(100, 270, 100, 50);self.sexLabel.text = [NSString stringWithFormat:@"性别:x"];[self.view addSubview:self.sexLabel];UIButton * weixinLogin = [[UIButton alloc] init];self.weixinLogin = weixinLogin ;[weixinLogin setTitle:@"微信登录" forState:UIControlStateNormal];[weixinLogin setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];weixinLogin.backgroundColor = [UIColor whiteColor];[weixinLogin addTarget:self action:@selector(weixinLoginBtnDidClicked) forControlEvents:UIControlEventTouchUpInside];[self.view addSubview:weixinLogin];weixinLogin.frame = CGRectMake(100, 380, 100, 100);
}-(void)weixinLoginBtnDidClicked
{NSLog(@"weixinLoginBtnDidClicked");[ThirdPartyLoginTools weChatLogin:self delegate:self];
}#pragma mark - ThirdPartyLoginToolsDelegate
-(void)loginToolsWithUserMessgate:(UMSocialUserInfoResponse *)response
{[self.imageView qj_setImageWithUrlStr:response.iconurl];self.nameLabel.text = [NSString stringWithFormat:@"用户名:%@",response.name];self.sexLabel.text = [NSString stringWithFormat:@"性别:%@",response.gender];
}@end

4.第三方登录工具文件 ThirdPartyLoginTools
//
//  ThirdPartyLoginTools.h
//  DuoBao
//
//  Created by 瞿杰 on 17/6/15.
//
//#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
#import <UMSocialCore/UMSocialCore.h>typedef enum : NSUInteger {ThirdPartyPlatformTypeQQ = 3,ThirdPartyPlatformTypeWechat = 4,ThirdPartyPlatformTypeSina = 5,
} ThirdPartyPlatformType;@protocol ThirdPartyLoginToolsDelegate <NSObject>@required
-(void)loginToolsWithUserMessgate:(UMSocialUserInfoResponse *)response ;@end@interface ThirdPartyLoginTools : NSObject/***  新浪登陆**  @param controller*/
+(void)sinaLogin:(UIViewController *)controller  delegate:(id<ThirdPartyLoginToolsDelegate>) delegate;
/***  微信登陆**  @param controller*/
+(void)weChatLogin:(UIViewController *)controller delegate:(id<ThirdPartyLoginToolsDelegate>) delegate;
/***  QQ登陆**  @param controller*/+(void)qqLogin:(UIViewController *)controller delegate:(id<ThirdPartyLoginToolsDelegate>) delegate;/**** 登陆*  @param platformName 平台名字*  @param controller*/
+(void)loginWithPlatformName:(ThirdPartyPlatformType)type withController:(UIViewController *)controller  delegate:(id<ThirdPartyLoginToolsDelegate>) delegate;/***  第三方授权成功,登陆接口**  @param loginType  登陆类型*  @param openId     第三方唯一标识*  @param avatar     头像地址*  @param sex        性别*  @param birthday   生日*  @param controller */
+(void)thirdPartyLoginWithPlatformType:(NSInteger)loginType  openId:(NSString *)openId name:(NSString *)name avatar:(NSString *)avatar sex:(NSString *)sex  birthDay:(NSString *)birthday iconUrl:(NSString*)iconUrl  controller:(UIViewController *)controller;
@end
//
//  ThirdPartyLoginTools.m
//  DuoBao
//
//  Created by 瞿杰 on 17/6/15.
//
//#import "ThirdPartyLoginTools.h"
//#import "UserInfoUtils.h"
#import "WXApi.h"
#import "WeiboSDK.h"
#import <TencentOpenAPI/TencentOAuth.h>@implementation ThirdPartyLoginTools+(void)sinaLogin:(UIViewController *)controller  delegate:(id<ThirdPartyLoginToolsDelegate>) delegate
{if ([WeiboSDK isWeiboAppInstalled]) {[self loginWithPlatformName:ThirdPartyPlatformTypeSina withController:controller delegate:delegate];}else{NSLog(@"请安装新浪客户端");}
}+(void)weChatLogin:(UIViewController *)controller delegate:(id<ThirdPartyLoginToolsDelegate>) delegate
{if ([WXApi isWXAppInstalled]) {[self loginWithPlatformName:ThirdPartyPlatformTypeWechat withController:controller delegate:delegate];}else{NSLog(@"请安装微信客户端");
//        [ToastUtil toast:@"请安装微信客户端"];}
}+(void)qqLogin:(UIViewController *)controller delegate:(id<ThirdPartyLoginToolsDelegate>) delegate
{if ([TencentOAuth iphoneQQInstalled]) {[self loginWithPlatformName:ThirdPartyPlatformTypeQQ withController:controller delegate:delegate];}else{NSLog(@"请安装QQ客户端");}
}+(void)loginWithPlatformName:(ThirdPartyPlatformType)type withController:(UIViewController *)controller  delegate:(id<ThirdPartyLoginToolsDelegate>) delegate
{//此处调用授权的方法,你可以把下面的platformName 替换成 UMShareToSina,UMShareToTencent等UMSocialPlatformType platformName  ;if (ThirdPartyPlatformTypeWechat == type) {platformName = UMSocialPlatformType_WechatSession ;}else if (ThirdPartyPlatformTypeQQ == type){platformName = UMSocialPlatformType_QQ ;}else{platformName = UMSocialPlatformType_Sina ;}[[UMSocialManager defaultManager] getUserInfoWithPlatform:platformName currentViewController:controller completion:^(id result, NSError *error) {if (error) {} else {UMSocialUserInfoResponse *resp = result;if ([delegate respondsToSelector:@selector(loginToolsWithUserMessgate:)]) {[delegate loginToolsWithUserMessgate:resp];}// 授权信息NSLog(@"Wechat uid: %@", resp.uid);NSLog(@"Wechat openid: %@", resp.openid);NSLog(@"Wechat accessToken: %@", resp.accessToken);NSLog(@"Wechat refreshToken: %@", resp.refreshToken);NSLog(@"Wechat expiration: %@", resp.expiration);// 用户信息NSLog(@"Wechat name: %@", resp.name);NSLog(@"Wechat iconurl: %@", resp.iconurl);NSLog(@"Wechat gender: %@", resp.gender);// 第三方平台SDK源数据NSLog(@"Wechat originalResponse: %@", resp.originalResponse);[self thirdPartyLoginWithPlatformType:type openId:resp.uid name:resp.name avatar:resp.iconurl sex:[NSString stringWithFormat:@"%ld",[[resp.originalResponse objectForKey:@"sex"] integerValue]] birthDay:nil iconUrl:resp.iconurl controller:controller];}}];
}+(void)thirdPartyLoginWithPlatformType:(NSInteger)loginType  openId:(NSString *)openId name:(NSString *)name avatar:(NSString *)avatar sex:(NSString *)sex  birthDay:(NSString *)birthday iconUrl:(NSString*)iconUrl  controller:(UIViewController *)controller
{// 自家APP登录获取数据
}@end

5.对于 UIImageView+QJWebImage.h 文件是用于下载图片的,自个封装的,如有不足之处,可用SDWebImage框架代替。下载 QJWebImage,结构如下:




三、学习参考网址:友盟官方网址






这篇关于第四十一篇:微信第三方登录的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/983618

相关文章

JWT + 拦截器实现无状态登录系统

《JWT+拦截器实现无状态登录系统》JWT(JSONWebToken)提供了一种无状态的解决方案:用户登录后,服务器返回一个Token,后续请求携带该Token即可完成身份验证,无需服务器存储会话... 目录✅ 引言 一、JWT 是什么? 二、技术选型 三、项目结构 四、核心代码实现4.1 添加依赖(pom

Spring Security重写AuthenticationManager实现账号密码登录或者手机号码登录

《SpringSecurity重写AuthenticationManager实现账号密码登录或者手机号码登录》本文主要介绍了SpringSecurity重写AuthenticationManage... 目录一、创建自定义认证提供者CustomAuthenticationProvider二、创建认证业务Us

Springboot项目登录校验功能实现

《Springboot项目登录校验功能实现》本文介绍了Web登录校验的重要性,对比了Cookie、Session和JWT三种会话技术,分析其优缺点,并讲解了过滤器与拦截器的统一拦截方案,推荐使用JWT... 目录引言一、登录校验的基本概念二、HTTP协议的无状态性三、会话跟android踪技术1. Cook

使用Redis快速实现共享Session登录的详细步骤

《使用Redis快速实现共享Session登录的详细步骤》在Web开发中,Session通常用于存储用户的会话信息,允许用户在多个页面之间保持登录状态,Redis是一个开源的高性能键值数据库,广泛用于... 目录前言实现原理:步骤:使用Redis实现共享Session登录1. 引入Redis依赖2. 配置R

Spring Security 单点登录与自动登录机制的实现原理

《SpringSecurity单点登录与自动登录机制的实现原理》本文探讨SpringSecurity实现单点登录(SSO)与自动登录机制,涵盖JWT跨系统认证、RememberMe持久化Token... 目录一、核心概念解析1.1 单点登录(SSO)1.2 自动登录(Remember Me)二、代码分析三、

Ubuntu 24.04启用root图形登录的操作流程

《Ubuntu24.04启用root图形登录的操作流程》Ubuntu默认禁用root账户的图形与SSH登录,这是为了安全,但在某些场景你可能需要直接用root登录GNOME桌面,本文以Ubuntu2... 目录一、前言二、准备工作三、设置 root 密码四、启用图形界面 root 登录1. 修改 GDM 配

Spring Boot 结合 WxJava 实现文章上传微信公众号草稿箱与群发

《SpringBoot结合WxJava实现文章上传微信公众号草稿箱与群发》本文将详细介绍如何使用SpringBoot框架结合WxJava开发工具包,实现文章上传到微信公众号草稿箱以及群发功能,... 目录一、项目环境准备1.1 开发环境1.2 微信公众号准备二、Spring Boot 项目搭建2.1 创建

nginx 负载均衡配置及如何解决重复登录问题

《nginx负载均衡配置及如何解决重复登录问题》文章详解Nginx源码安装与Docker部署,介绍四层/七层代理区别及负载均衡策略,通过ip_hash解决重复登录问题,对nginx负载均衡配置及如何... 目录一:源码安装:1.配置编译参数2.编译3.编译安装 二,四层代理和七层代理区别1.二者混合使用举例

java向微信服务号发送消息的完整步骤实例

《java向微信服务号发送消息的完整步骤实例》:本文主要介绍java向微信服务号发送消息的相关资料,包括申请测试号获取appID/appsecret、关注公众号获取openID、配置消息模板及代码... 目录步骤1. 申请测试系统2. 公众号账号信息3. 关注测试号二维码4. 消息模板接口5. Java测试

CSS3打造的现代交互式登录界面详细实现过程

《CSS3打造的现代交互式登录界面详细实现过程》本文介绍CSS3和jQuery在登录界面设计中的应用,涵盖动画、选择器、自定义字体及盒模型技术,提升界面美观与交互性,同时优化性能和可访问性,感兴趣的朋... 目录1. css3用户登录界面设计概述1.1 用户界面设计的重要性1.2 CSS3的新特性与优势1.