第三方登录
shareSDK集成第三方登录平台
重点:流程-- 去mob官网注册->选择文档中心创建应用得到appkey等相关信息->集成
一、准备工作
1、首先去Mob官网注册你的app,http://mob.com/。
2、选择文档中心。
3、创建应用得到的App Key和App Secret。
4、集成Mob的sdk。这里是第一个坑,虽然你可以自定义下载SDK,如果你做QQ分享和登录,一定不要忘记下载QQ空间的SDK。因为QQ本身不支持第三方登录,但支持第三方分享包括QQ空间分享,QQ空间支持第三方登录,所以如果你做QQ分享和登录,一定要记得下载QQ空间的SDK。如果只做分享,那只下载QQ的就行。这里我被坑过,ShareSDK提供的文档里面没有说明。
5、添加相应的依赖库
1 2 3 | 1、 SystemConfiguration.framework、QuartzCore.framework、CoreTelephony.framework、libicucore.dylib、libz.1.2.5.dylib、Security.framework 2、以下依赖库根据社交平台添加新浪微博SDK依赖库 (从v2.10.5开始) ImageIO.framework 3、QQ好友和QQ空间SSO必要(新注册腾讯开放平台帐号只支持SSO授权,只是老开发者才可以使用网页授权) libstdc++.dylib、libsqlite3.dylib |
6、下载简洁版SDK,SDK简单介绍
1 2 3 4 5 6 7 8 9 | 1、extends:第三方平台SDK。(不需要的平台的SDK可直接移除) 2.MOBFoundation.framework:基础功能框架。(必要) 3.ShareSDK.bundle:ShareSDK资源文件。(必要) 4.ShareSDK.framework:核心静态库。(必要) 5.ShareSDKConnector.framework:用于ShareSDK框架与外部框架连接 的代理框架插件。使用第三方SDK时必要。 6.ShareSDKExtension.framework:对ShareSDK功能的扩展框架插件。目前主要提供第三方平台登录、一键分享、截屏分享、摇一摇分享等相关功能。需要使用以上功能时必要。 7.ShareSDKInterfaceAdapter.framework:ShareSDK v2.x对简洁版的接口兼容包。(建议直接使用简洁版接口,尽量不使用这个兼容包) 8.ShareSDKUI.bundle:分享菜单栏和分享编辑页面资源包。(如果自定义这些UI可直接移除) 9.ShareSDKUI.framework:分享菜单栏和分享编辑页面。(如果自定义这些UI可直接移除) |
7、在AppDelegate.m
中添加以下头文件,再运行项目看看,这是不会报错的,如果报错看提示信息,多搜索,相信是能够解决的。
1 2 3 4 5 6 7 8 9 10 11 12 13 | #import <ShareSDK/ShareSDK.h> #import <ShareSDKConnector/ShareSDKConnector.h> //腾讯开放平台(对应QQ和QQ空间)SDK头文件 #import <TencentOpenAPI/TencentOAuth.h> #import <TencentOpenAPI/QQApiInterface.h> //微信SDK头文件 #import "WXApi.h" //新浪微博SDK头文件 #import "WeiboSDK.h" //新浪微博SDK需要在项目Build Settings中的Other Linker Flags添加"-ObjC" |
8、到相应开发者平台注册开发者账号,并添加你要进行分享和使用第三方登录应用的信息。
1 2 3 | 添加新浪微博应用 注册网址 http: //open.weibo.com 添加QQ应用 注册网址 http: //mobile.qq.com/api/ 添加微信应用 http: //open.weixin.qq.com |
9、添加白名单
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 | <key>LSApplicationQueriesSchemes</key>这是qq的 <array> < 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 >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 > </array> |
10、拿到这些AppID、AppSecret、AppKey等东西后就可以开始对ShareSDK进行初始化。
在AppDelegate.m
中添加下面这个方法,并记得在- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
方法中调用。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions { /** * 设置ShareSDK的appKey,如果尚未在ShareSDK官网注册过App,请移步到http://mob.com/login 登录后台进行应用注册, * 在将生成的AppKey传入到此方法中。 * 方法中的第二个第三个参数为需要连接社交平台SDK时触发, * 在此事件中写入连接代码。第四个参数则为配置本地社交平台时触发,根据返回的平台类型来配置平台信息。 * 如果您使用的时服务端托管平台信息时,第二、四项参数可以传入nil,第三项参数则根据服务端托管平台来决定要连接的社交SDK。 */ [ShareSDK registerApp: @"iosv1101" ` activePlatforms:@[ @(SSDKPlatformTypeSinaWeibo), @(SSDKPlatformTypeMail), @(SSDKPlatformTypeSMS), @(SSDKPlatformTypeCopy), @(SSDKPlatformTypeWechat), @(SSDKPlatformTypeQQ), @(SSDKPlatformTypeRenren), @(SSDKPlatformTypeGooglePlus)] onImport:^(SSDKPlatformType platformType) { switch (platformType) { case SSDKPlatformTypeWechat: [ShareSDKConnector connectWeChat:[WXApi class ]]; break ; case SSDKPlatformTypeQQ: [ShareSDKConnector connectQQ:[QQApiInterface class ] tencentOAuthClass:[TencentOAuth class ]]; break ; case SSDKPlatformTypeSinaWeibo: [ShareSDKConnector connectWeibo:[WeiboSDK class ]]; break ; case SSDKPlatformTypeRenren: [ShareSDKConnector connectRenren:[RennClient class ]]; break ; case SSDKPlatformTypeGooglePlus: [ShareSDKConnector connectGooglePlus:[GPPSignIn class ] shareClass:[GPPShare class ]]; break ; default : break ; } } onConfiguration:^(SSDKPlatformType platformType, NSMutableDictionary *appInfo) { switch (platformType) { case SSDKPlatformTypeSinaWeibo: //设置新浪微博应用信息,其中authType设置为使用SSO+Web形式授权 [appInfo SSDKSetupSinaWeiboByAppKey: @"568898243" appSecret: @"38a4f8204cc784f81f9f0daaf31e02e3" redirectUri: @"http://www.sharesdk.cn" authType:SSDKAuthTypeBoth]; break ; case SSDKPlatformTypeWechat: [appInfo SSDKSetupWeChatByAppId: @"wx4868b35061f87885" appSecret: @"64020361b8ec4c99936c0e3999a9f249" ]; break ; case SSDKPlatformTypeQQ: [appInfo SSDKSetupQQByAppId: @"100371282" appKey: @"aed9b0303e3ed1e27bae87c33761161d" authType:SSDKAuthTypeBoth]; break ; case SSDKPlatformTypeRenren: [appInfo SSDKSetupRenRenByAppId: @"226427" appKey: @"fc5b8aed373c4c27a05b712acba0f8c3" secretKey: @"f29df781abdd4f49beca5a2194676ca4" authType:SSDKAuthTypeBoth]; break ; case SSDKPlatformTypeGooglePlus: [appInfo SSDKSetupGooglePlusByClientID: @"232554794995.apps.googleusercontent.com" clientSecret: @"PEdFgtrMw97aCvf0joQj7EMk" redirectUri: @"http://localhost" authType:SSDKAuthTypeBoth]; break ; default : break ; } }]; return YES; } |
11、添加URL Schemes。(项目名 > TARGETS > info > URL Types 点击添加URL Types)SSO设置
1 2 3 4 5 6 | 1、新浪微博的URL Schemes是:wb 加上在微博开放平台申请的AppKey。 2、微信的URL Schemes是:微信开放平台申请的AppID。 3、QQ空间的URL Schemes是:QQ开放平台申请的 tencent 加上 AppID。(如果只做分享的话,可以不添加,做登录的话必须添加) QQ的URL Schemes是:QQ 加上 AppID的16进制(如果AppID转换的16进制数不够8位则在前面补0,如转换的是:5FB8B52,则最终填入为:QQ05FB8B52 注意:转换后的字母要大写) 在URL Types中添加QQ的AppID,其格式为:”QQ” + AppId的16进制(如果appId转换的16进制数不够8位则在前面补0,如转换的是:5FB8B52,则最终填入为:QQ05FB8B52 注意:转换后的字母要大写) 转换16进制的方法:这里给出开源中国的在线转码 http: //tool.oschina.net/hexconvert/,呵呵,秒转! 转码完成后,需要在十六进制码的前面加上QQ的前缀,以及将十六进制码的字母大写,一定要大写,切记切记! |
12、添加完URL Schemes后,在AppDelegate.m
文件中添加以下对URL处理的代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 | - (BOOL)application:(UIApplication *)application handleOpenURL:(NSURL *)url { return [ShareSDK handleOpenURL:url wxDelegate:self]; } - (BOOL)application:(UIApplication *)application openURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation { return [ShareSDK handleOpenURL:url sourceApplication:sourceApplication annotation:annotation wxDelegate:self]; } //到此第三方的准备工作就算是做完 |
13、 相关配置
1、设置https安全配置
2、大部分社交平台SDK不支持bitcode,在搜索框中输入bitcode,在Build Setting输入bitcode,设置为NO
14、分享代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 | 1.需要在分享的试图中导入的头文件 #import <ShareSDK/ShareSDK.h> #import <ShareSDKUI/ShareSDK+SSUI.h> 2.调用构造分享参数接口和分享的接口 //1、创建分享参数 NSArray* imageArray = @[[UIImage imageNamed: @"shareImg.png" ]]; (注意:图片必须要在Xcode左边目录里面,名称必须要传正确,如果要分享网络图片,可以这样传iamge参数 images:@[ @"http://mob.com/Assets/images/logo.png?v=20150320" ]) if (imageArray) { NSMutableDictionary *shareParams = [NSMutableDictionary dictionary]; [shareParams SSDKSetupShareParamsByText: @"分享内容" images:imageArray url:[NSURL URLWithString: @"http://mob.com" ] title: @"分享标题" type:SSDKContentTypeAuto]; //2、分享(可以弹出我们的分享菜单和编辑界面) [ShareSDK showShareActionSheet:nil //要显示菜单的视图, iPad版中此参数作为弹出菜单的参照视图,只有传这个才可以弹出我们的分享菜单,可以传分享的按钮对象或者自己创建小的view 对象,iPhone可以传nil不会影响 items:nil shareParams:shareParams onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) { switch (state) { case SSDKResponseStateSuccess: { UIAlertView *alertView = [[UIAlertView alloc] initWithTitle: @"分享成功" message:nil delegate :nil cancelButtonTitle: @"确定" otherButtonTitles:nil]; [alertView show]; break ; } case SSDKResponseStateFail: { UIAlertView *alert = [[UIAlertView alloc] initWithTitle: @"分享失败" message:[NSString stringWithFormat: @"%@" ,error] delegate :nil cancelButtonTitle: @"OK" otherButtonTitles:nil, nil]; [alert show]; break ; } default : break ; } } ];} |
15、方法中的shareType
参数传什么值就分享到什么平台:
1 2 3 4 | ShareTypeSinaWeibo //新浪微博 ShareTypeQQSpace //QQ空间 (注意:填写这个类型就可以分享到好友和QQ空间,也可以用来做登录) ShareTypeWeixiSession //微信好友 (这个类型也可以用来做微信登录) ShareTypeWeixiTimeline //微信朋友圈 |
16、登录
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 | //例如QQ的登录 [ShareSDK getUserInfo:SSDKPlatformTypeQQ onStateChanged:^(SSDKResponseState state, SSDKUser *user, NSError *error) { if (state == SSDKResponseStateSuccess) { NSLog( @"uid=%@" ,user.uid); NSLog( @"%@" ,user.credential); NSLog( @"token=%@" ,user.credential.token); NSLog( @"nickname=%@" ,user.nickname); } else { NSLog( @"%@" ,error); } }]; |
17、shareSDKUI设置
1 2 | (1)、如果你想修改图标,把你的定制的图标讲此文件下对应的图标替换即可,注意要先删除shareSDK自带的再倒入,不然导不进去,固定名称:sns_icon_xx@2x.png。 (2)、shareSDK 自带的分享平台名称默认是英文的,如果想汉化的话,执行操作如下:工程->info->localizations本地化->可以在国际化中修改 |
(3)、跳过分享页面
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | //先构造分享参数: NSMutableDictionary *shareParams = [NSMutableDictionary dictionary]; [shareParams SSDKSetupShareParamsByText: @"分享内容" images:@[[UIImage imageNamed: @"shareImg.png" ]] url:[NSURL URLWithString: @"http://mob.com" ] title: @"分享标题" type:SSDKContentTypeAuto]; //调用分享的方法 SSUIShareActionSheetController *sheet = [ShareSDK showShareActionSheet:view items:nil shareParams:shareParams onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) { switch (state) { case SSDKResponseStateSuccess: NSLog( @"分享成功!" ); break ; case SSDKResponseStateFail: NSLog( @"分享失败%@" ,error); break ; case SSDKResponseStateCancel: NSLog( @"分享已取消" ); break ; default : break ; } }]; //删除和添加平台示例 [sheet.directSharePlatforms removeObject:@(SSDKPlatformTypeWechat)];(默认微信,QQ,QQ空间都是直接跳客户端分享,加了这个方法之后,可以跳分享编辑界面分享) [sheet.directSharePlatforms addObject:@(SSDKPlatformTypeSinaWeibo)];(加了这个方法之后可以不跳分享编辑界面,直接点击分享菜单里的选项,直接分享) |
(4)、自定义分享菜单栏样式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 | // 弹出分享菜单需要导入的头文件 #import <ShareSDKUI/ShareSDK+SSUI.h> // 自定义分享菜单栏需要导入的头文件 #import <ShareSDKUI/SSUIShareActionSheetStyle.h> //1、创建分享参数(必要) NSMutableDictionary *shareParams = [NSMutableDictionary dictionary]; NSArray* imageArray = @[[UIImage imageNamed: @"图片名.png" ]]; (注意:图片必须要在Xcode左边目录里面,名称必须要传正确,如果要分享网络图片,可以这样传iamge参数 images:@[ @"http://mob.com/Assets/images/logo.png?v=20150320" ]) [shareParams SSDKSetupShareParamsByText: @"分享内容" images:imageArray url:[NSURL URLWithString: @"http://mob.com" ] title: @"分享标题" type:SSDKContentTypeAuto]; // 设置分享菜单栏样式(非必要) // 设置分享菜单的背景颜色 [SSUIShareActionSheetStyle setActionSheetBackgroundColor:[UIColor colorWithRed:249/255.0 green:0/255.0 blue:12/255.0 alpha:0.5]]; // 设置分享菜单颜色 [SSUIShareActionSheetStyle setActionSheetColor:[UIColor colorWithRed:21.0/255.0 green:21.0/255.0 blue:21.0/255.0 alpha:1.0]]; // 设置分享菜单-取消按钮背景颜色 [SSUIShareActionSheetStyle setCancelButtonBackgroundColor:[UIColor colorWithRed:21.0/255.0 green:21.0/255.0 blue:21.0/255.0 alpha:1.0]]; // 设置分享菜单-取消按钮的文本颜色 [SSUIShareActionSheetStyle setCancelButtonLabelColor:[UIColor blackColor]]; // 设置分享菜单-社交平台文本颜色 [SSUIShareActionSheetStyle setItemNameColor:[UIColor whiteColor]]; // 设置分享菜单-社交平台文本字体 [SSUIShareActionSheetStyle setItemNameFont:[UIFont systemFontOfSize:10]]; //设置分享编辑界面的导航栏颜色 [SSUIEditorViewStyle setiPhoneNavigationBarBackgroundColor:[UIColor blackColor]]; //设置编辑界面标题颜色 [SSUIEditorViewStyle setTitleColor:[UIColor redColor]]; //设置取消发布标签文本颜色 [SSUIEditorViewStyle setCancelButtonLabelColor:[UIColor blueColor]]; [SSUIEditorViewStyle setShareButtonLabelColor:[UIColor blueColor]]; //设置分享编辑界面状态栏风格 [SSUIEditorViewStyle setStatusBarStyle:UIStatusBarStyleLightContent]; //设置简单分享菜单样式 [SSUIShareActionSheetStyle setShareActionSheetStyle:ShareActionSheetStyleSimple]; //2、弹出ShareSDK分享菜单 [ShareSDK showShareActionSheet:view items:nil shareParams:shareParams onShareStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) { ...... } |
(5)、自定义UI
1 2 3 4 5 6 7 8 9 10 11 12 | //创建分享参数 NSMutableDictionary *shareParams = [NSMutableDictionary dictionary]; [shareParams SSDKSetupShareParamsByText: @"分享内容" images:images //传入要分享的图片 url:[NSURL URLWithString: @"http://mob.com" ] title: @"分享标题" type:SSDKContentTypeAuto]; //进行分享 [ShareSDK share:SSDKPlatformTypeSinaWeibo //传入分享的平台类型 parameters:shareParams onStateChanged:^(SSDKResponseState state, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error) { // 回调处理....}]; } |
18、相关错误原因
1 2 3 | 1、去新浪微博开放平台的应用信息-->高级信息-->OAuth2.0授权设置,填授权回调页 与取消授权回调页链接,二者相同;然后将该链接复制到项目中初始化微博的地方的redirectUri参数中的链接。 2、出现sso的报错,是因为微博开放平台的bundleid和工程的不一致 |
开源shareSDK UI地址:https://github.com/MobClub/ShareSDKUI
参考网址:http://www.jianshu.com/p/91f0d3d180eb
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现