IOS项目集成ShareSDK实现第三方登录、分享、关注等功能。
(1)官方下载ShareSDK iOS 2.8.8,地址:http://sharesdk.cn/
(2)根据实际情况,引入相关的库,参考官方文档。
(3)在项目的AppDelegate中一般情况下有三个操作,第一是注册ShareSDK,第二是注册各个平台的账号,第三是关于微信等应用的回调处理。
- //
- // AppDelegate.m
- // ShareSDKTest
- //
- // Created by wangdalei on 14-6-23.
- // Copyright (c) 2014年 王大雷. All rights reserved.
- //
- #import "AppDelegate.h"
- #import "RootViewController.h"
- #import <ShareSDK/ShareSDK.h>
- #import "WeiboApi.h"
- #import <TencentOpenAPI/QQApiInterface.h>
- #import <TencentOpenAPI/TencentOAuth.h>
- #import "WXApi.h"
- #import <TencentOpenAPI/QQApiInterface.h>
- #import <TencentOpenAPI/TencentOAuth.h>
- @implementation AppDelegate
- @synthesize rootVC;
- - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
- self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
- if (self.rootVC==nil) {
- self.rootVC = [[RootViewController alloc]initWithNibName:@"RootViewController" bundle:nil];
- }
- UINavigationController *rootNav = [[UINavigationController alloc]initWithRootViewController:self.rootVC];
- self.window.rootViewController = rootNav;
- self.window.backgroundColor = [UIColor whiteColor];
- [self.window makeKeyAndVisible];
- <span style="color:#ff0000;">[ShareSDK registerApp:@"1a2e7ab5fb6c"];</span>
- <span style="color:#3366ff;"> //添加新浪微博应用 注册网址 http://open.weibo.com wdl@pmmq.com 此处需要替换成自己应用的
- [ShareSDK connectSinaWeiboWithAppKey:@"3201194191"
- appSecret:@"0334252914651e8f76bad63337b3b78f"
- redirectUri:@"http://appgo.cn"];
- //添加腾讯微博应用 注册网址 http://dev.t.qq.com wdl@pmmq.com 此处需要替换成自己应用的
- [ShareSDK connectTencentWeiboWithAppKey:@"801307650"
- appSecret:@"ae36f4ee3946e1cbb98d6965b0b2ff5c"
- redirectUri:@"http://www.sharesdk.cn"
- wbApiCls:[WeiboApi class]];
- //添加QQ空间应用 注册网址 http://connect.qq.com/intro/login/ wdl@pmmq.com 此处需要替换成自己应用的
- [ShareSDK connectQZoneWithAppKey:@"100371282"
- appSecret:@"aed9b0303e3ed1e27bae87c33761161d"
- qqApiInterfaceCls:[QQApiInterface class]
- tencentOAuthCls:[TencentOAuth class]];
- //此参数为申请的微信AppID wdl@pmmq.com 此处需要替换成自己应用的
- [ShareSDK connectWeChatWithAppId:@"wx4868b35061f87885" wechatCls:[WXApi class]];
- //添加QQ应用 该参数填入申请的QQ AppId wdl@pmmq.com 此处需要替换成自己应用的
- [ShareSDK connectQQWithQZoneAppKey:@"100371282"
- qqApiInterfaceCls:[QQApiInterface class]
- tencentOAuthCls:[TencentOAuth class]];</span>
- return YES;
- }
- - (void)applicationWillResignActive:(UIApplication *)application {
- // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
- // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
- }
- - (void)applicationDidEnterBackground:(UIApplication *)application {
- // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
- // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
- }
- - (void)applicationWillEnterForeground:(UIApplication *)application {
- // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
- }
- - (void)applicationDidBecomeActive:(UIApplication *)application {
- // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
- }
- - (void)applicationWillTerminate:(UIApplication *)application {
- // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
- }
- <span style="color:#ff6600;">#pragma mark - WX回调
- - (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];
- }
- #pragma mark - WXApiDelegate
- /*! @brief 收到一个来自微信的请求,第三方应用程序处理完后调用sendResp向微信发送结果
- *
- * 收到一个来自微信的请求,异步处理完成后必须调用sendResp发送处理结果给微信。
- * 可能收到的请求有GetMessageFromWXReq、ShowMessageFromWXReq等。
- * @param req 具体请求内容,是自动释放的
- */
- -(void) onReq:(BaseReq*)req{
- }
- /*! @brief 发送一个sendReq后,收到微信的回应
- *
- * 收到一个来自微信的处理结果。调用一次sendReq后会收到onResp。
- * 可能收到的处理结果有SendMessageToWXResp、SendAuthResp等。
- * @param resp具体的回应内容,是自动释放的
- */
- -(void) onResp:(BaseResp*)resp{
- }
- </span>
- @end
(4)信息分享。
- -(IBAction)share:(id)sender{
- NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"card" ofType:@"png"];
- //构造分享内容
- id<ISSContent> publishContent = [ShareSDK content:@"分享内容测试"
- defaultContent:@"默认分享内容测试,没内容时显示"
- image:[ShareSDK imageWithPath:imagePath]
- title:@"pmmq"
- url:@"http://www.sharesdk.cn"
- description:@"这是一条测试信息"
- mediaType:SSPublishContentMediaTypeNews];
- [ShareSDK showShareActionSheet:nil
- shareList:nil
- content:publishContent
- statusBarTips:YES
- authOptions:nil
- shareOptions: nil
- result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
- if (state == SSResponseStateSuccess)
- {
- NSLog(@"分享成功");
- }
- else if (state == SSResponseStateFail)
- {
- NSLog(@"分享失败");
- }
- }];
- }
(5)登录、登出、获取授权信息、关注制定微博
- //
- // LoginViewController.m
- // ShareSDKTest
- //
- // Created by wangdalei on 14-6-23.
- // Copyright (c) 2014年 王大雷. All rights reserved.
- //
- #import "LoginViewController.h"
- #import <ShareSDK/ShareSDK.h>
- @interface LoginViewController ()
- -(IBAction)loginWithSina:(id)sender;
- -(IBAction)loginWithQQ:(id)sender;
- -(IBAction)loginoutWithSina:(id)sender;
- -(IBAction)loginoutWithQQ:(id)sender;
- -(IBAction)guanzhuUs:(id)sender;
- -(void)reloadStateWithType:(ShareType)type;
- @end
- @implementation LoginViewController
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- }
- return self;
- }
- - (void)viewDidLoad {
- [super viewDidLoad];
- }
- - (void)didReceiveMemoryWarning {
- [super didReceiveMemoryWarning];
- }
- - (IBAction)loginWithSina:(id)sender {
- [ShareSDK getUserInfoWithType:ShareTypeSinaWeibo authOptions:nil result:^(BOOL result, id<ISSPlatformUser> userInfo, id<ICMErrorInfo> error) {
- NSLog(@"%d",result);
- if (result) {
- //成功登录后,判断该用户的ID是否在自己的数据库中。
- //如果有直接登录,没有就将该用户的ID和相关资料在数据库中创建新用户。
- [self reloadStateWithType:ShareTypeSinaWeibo];
- }
- }];
- }
- -(IBAction)loginWithQQ:(id)sender{
- [ShareSDK getUserInfoWithType:ShareTypeQQSpace authOptions:nil result:^(BOOL result, id<ISSPlatformUser> userInfo, id<ICMErrorInfo> error) {
- NSLog(@"%d",result);
- if (result) {
- //成功登录后,判断该用户的ID是否在自己的数据库中。
- //如果有直接登录,没有就将该用户的ID和相关资料在数据库中创建新用户。
- [self reloadStateWithType:ShareTypeQQSpace];
- }
- }];
- }
- -(IBAction)loginoutWithSina:(id)sender{
- [ShareSDK cancelAuthWithType:ShareTypeSinaWeibo];
- [self reloadStateWithType:ShareTypeSinaWeibo];
- }
- -(IBAction)loginoutWithQQ:(id)sender{
- [ShareSDK cancelAuthWithType:ShareTypeQQSpace];
- [self reloadStateWithType:ShareTypeQQSpace];
- }
- -(void)reloadStateWithType:(ShareType)type{
- //现实授权信息,包括授权ID、授权有效期等。
- //此处可以在用户进入应用的时候直接调用,如授权信息不为空且不过期可帮用户自动实现登录。
- id<ISSPlatformCredential> credential = [ShareSDK getCredentialWithType:type];
- UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"TEXT_TIPS", @"提示")
- message:[NSString stringWithFormat:
- @"uid = %@\ntoken = %@\nsecret = %@\n expired = %@\nextInfo = %@",
- [credential uid],
- [credential token],
- [credential secret],
- [credential expired],
- [credential extInfo]]
- delegate:nil
- cancelButtonTitle:NSLocalizedString(@"TEXT_KNOW", @"知道了")
- otherButtonTitles:nil];
- [alertView show];
- }
- //关注用户
- -(IBAction)guanzhuUs:(id)sender{
- [ShareSDK followUserWithType:ShareTypeSinaWeibo //平台类型
- field:@"ShareSDK" //关注用户的名称或ID
- fieldType:SSUserFieldTypeName //字段类型,用于指定第二个参数是名称还是ID
- authOptions:nil //授权选项
- viewDelegate:nil //授权视图委托
- result:^(SSResponseState state, id<ISSPlatformUser> userInfo, id<ICMErrorInfo> error) {
- if (state == SSResponseStateSuccess) {
- NSLog(@"关注成功");
- } else if (state == SSResponseStateFail) {
- NSLog(@"%@", [NSString stringWithFormat:@"关注失败:%@", error.errorDescription]);
- }
- }];
- }
- @end
(5)你可能会看到一些应用需要第三方登录的,一种是弹出webView加载的新浪微博或者qq的网页授权,还有一种是跳转到本地的已经安装的新浪微博应用或者qq应用进行授权。第二种授权方式较SSO授权,体验会比较好一些,因为不需要用户输入新浪微博或QQ的用户名与密码。
第二种授权方式需要在plist中配置Scheme。SSO默认是打开的不需要配置。在AppDelegate中实现回调。
(6)测试DEMO截图:
IOS项目集成ShareSDK实现第三方登录、分享、关注等功能。的更多相关文章
- IOS项目集成ShareSDK实现第三方登录、分享、关注等功能(备用)
(1)官方下载ShareSDK iOS 2.8.8,地址:http://sharesdk.cn/ (2)根据实际情况,引入相关的库,参考官方文档. (3)在项目的AppDelegate中一般情况下有三 ...
- IOS项目集成ShareSDK实现第三方登录、分享、关注等功能
(1)官方下载ShareSDK iOS 2.8.8,地址:http://sharesdk.cn/ (2)根据实际情况,引入相关的库,参考官方文档. (3)在项目的AppDelegate中一般情况下有三 ...
- iOS:IOS项目集成ShareSDK实现第三方登录、分享、关注等功能。
原文链接:http://blog.csdn.net/daleiwang/article/details/34081231 (3)在项目的AppDelegate中一般情况下有三个操作,第一是注册Shar ...
- iOS开发---集成ShareSDK实现第三方登录、分享、关注等功能。
(1)官方下载ShareSDK IOS 2.9.6,地址:http://sharesdk.mob.com/Download (2)根据实际情况,引入相关的库,参考官方文档. (3)在项目的AppDel ...
- iOS:友盟SDK第三方登录 分享及友盟统计的使用
本文转载至 http://www.it165.net/pro/html/201503/37101.html Tag标签:第三方 01.#import "UMSocial.h" ...
- iOS项目中常用的第三方开源库
1.项目使用的第三方开源库 项目使用了CocoaPods(类似java中的maven)管理常用的第三方库,一些特殊的单独引用,下面介绍下比较好用的几个. (1)AFNetworking 目前比较推荐的 ...
- 现有iOS项目集成React Native过程记录
在<Mac系统下React Native环境搭建>配置了RN的开发环境,然后,本文记录在现有iOS项目集成React Native的过程,官方推荐使用Cocoapods,项目一开始也是使用 ...
- php+redis实现注册、删除、编辑、分页、登录、关注等功能
本文实例讲述了php+redis实现注册.删除.编辑.分页.登录.关注等功能.分享给大家供大家参考,具体如下: 主要界面 连接redis redis.php <?php //实例化 $red ...
- 利用ShareSDK进行第三方登录和分享
到相应开发者平台注册开发者账号,并添加你要进行分享和使用第三方登录应用的信息. 添加新浪微博应用 注册网址 http://open.weibo.com添加QQ应用 注册网址 http://mobil ...
随机推荐
- windows下面go语言环境搭建
步骤一:golang下载 下载地址是:http://www.golangtc.com/download 下载完成之后解压缩,放到你的c:/根目录下面.然后配置一下环境变量! 环境变量配置如下: 1.新 ...
- IP Failover Setup using Keepalived on CentOS/Redhat 6
source url:http://tecadmin.net/ip-failover-setup-using-keepalived-on-centos-redhat-6/ Keepalived is ...
- Deep Learning 在中文分词和词性标注任务中的应用
开源软件包 SENNA 和 word2vec 中都用到了词向量(distributed word representation),当时我就在想,对于我们的中文,是不是也类似地有字向量(distribu ...
- [python]pip常用命令(转载)
用阿里云服务器,使用pip安装第三方库的时候卡的要死.所以我就想pip能不能安装本地的包. 找到了这篇博客: http://me.iblogc.com/2015/01/01/pip%E5%B8%B8% ...
- error LNK1281: 无法生成 SAFESEH 映像 LNK2026 模块对于 SAFESEH 映像是不安全的 VS2015 /win10
平台 VS2015 /win10 错误 LNK1281 无法生成 SAFESEH 映像. 错误 LNK2026 模块对于 SAFESEH 映像是不安全的. 将 release 改成 debug
- 重构第24天 分解复杂的判断(Remove Arrowhead Antipattern)
理解: 当你的代码中有很深的嵌套条件时,花括号就会在代码中形成一个长长的箭头.我们经常在不同的代码中看到这种情况,并且这种情况也会扰乱代码的可读性. 如下代码所示,HasAccess方法里面包含一些嵌 ...
- Jcrop简单实用
今天有一个项目的功能需求 “在上传照片的时候能进行裁剪”,网上找了下,发现有Jcrop这款插件,自己试了下,感觉很不错,蛮好用的.又能增加用户体验,测试了兼容性也很好,所以在这里分享下 首先,可以到官 ...
- C#基础-css行内元素、块级元素基础
一.行内元素与块级元素 块级元素列表 <address> 定义地址 <caption> 定义表格标题 <dd> 定义列表中定义条目 <div> 定义文档 ...
- 【原创随笔】Sql2008 R2 做CQRS小结
1.做数据同步,订阅服务器只要把数据库建好就可以了,至于表和存储过程以及其它的都不管,订阅的时候会自动创建这些信息. 2.选择事务发布(如果同步表,表至少要带主键,不然不能选择) 3.在发布的时候,用 ...
- Python基础:映射(字典)
一.概述 映射类型(Mapping Types)是一种关联式的容器类型,它存储了对象与对象之间的映射关系. 字典(dict)是Python中唯一的映射类型,它是存储了一个个 键值对(由 键 映射到 值 ...