原文 http://www.cnblogs.com/CoderAlex/p/4860352.html

一: 快速集成

1.前言  

作为现在App里必不可少的用户分享需要,社交化分享显然是我们开发app里较为常用的。
最近因为公司App有社交化分享的需要,就特此研究了会,拿出来与大家分享。
想要集成社交会分享,我们可以使用
ShareSDK - 优点功能丰富,缺点体积较大
百度分享SDK - 缺点功能相对ShareSDK较少,优点体积较小
 
这是现在较为常用的两种社交化分享工具。
使用哪一种,就看个人的app的需要来决定了。
今天我主要说的是ShareSDK的简单集成和使用。
 

2.集成前的准备工作  

2.1. 拿自己的Appkey 去下载ShareSDK  

 
 

2.2. 申请分享工具的Appkey  

 
例如: 新浪微博、腾讯微博、豆瓣应用、人人网、QQ空间
 
去各大社交网站的开发者平台,进行注册申请即可。
 
获取如下:
 
    //新浪微博:

    //App Key:2258477553

    //App Secret:1e2f275afc375109e456f550fb3918e8

    //腾讯微博:

    //App key:2620460989

    //App secret:58c55f572d5ae35e0c355f4c0ee11283
 

3.集成ShareSDK     

3.1.注册使用ShareSDK   

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    //注册ShareSDK

    [ShareSDK registerApp:@"1983bf0916db”];

    return YES;

}

3.2.添加要集成的分享平台  

注意: 新浪微博需要提供回调地址才行

回调地址去新浪开发者平台获取

如图:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions

{

    //注册ShareSDK

    [ShareSDK registerApp:@"1983bf0916db”];

   //添加新浪微博应用

    [ShareSDK connectSinaWeiboWithAppKey:@"2620460989"

                               appSecret:@"58c55f572d5ae35e0c355f4c0ee11283"

                             redirectUri:@"http://weibo.cn/ext/share?ru=http%3A%2F%2F16kxs.com%2Fwap%2FBook%2FShow.aspx%3Fid%3D7983%26lmid%3D0%26uid%3D0%26ups%3D0&rt=%E9%83%BD%E5%B8%82%E7%89%A7%E9%AC%BC%E4%BA%BA&st=1301645308&appkey=2620460989”];

     //添加腾讯微博应用

    [ShareSDK connectTencentWeiboWithAppKey:@"801307650"

                                  appSecret:@"ae36f4ee3946e1cbb98d6965b0b2ff5c"

                                redirectUri:@"http://www.sharesdk.cn"];

    //添加豆瓣应用

    [ShareSDK connectDoubanWithAppKey:@"07d08fbfc1210e931771af3f43632bb9"

                            appSecret:@"e32896161e72be91"

                          redirectUri:@"http://dev.kumoway.com/braininference/infos.php"];

    //添加人人网应用

    [ShareSDK connectRenRenWithAppKey:@"fc5b8aed373c4c27a05b712acba0f8c3"

                            appSecret:@"f29df781abdd4f49beca5a2194676ca4"];

    //添加Facebook应用

    [ShareSDK connectFacebookWithAppKey:@"107704292745179"

                              appSecret:@"38053202e1a5fe26c80c753071f0b573"];

}

3.3.弹出分享View  

a.初始化默认分享内容

  NSString *imagePath = [[NSBundle mainBundle] pathForResource:@"123"  ofType:@"png"];

    //构建优秀的SDK

    //构造分享内容
id<ISSContent> publishContent = [ShareSDK content:@"iOS社交化分享测试内容。"
defaultContent:@"默认分享内容,没内容时显示"
image:[ShareSDK imageWithPath:imagePath]
title:@"ShareSDK"
url:@"http://www.sharesdk.cn"
description:@"这是一条测试信息"
mediaType:SSPublishContentMediaTypeNews];

b.弹出分享View

 [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(@"分享失败,错误码:%d,错误描述:%@", [error errorCode], [error errorDescription]);
}
}];

效果图:

二:

(1)官方下载ShareSDK IOS 2.9.6,地址:http:// http://www.cnblogs.com/CoderAlex/p/4860352.html/Download

(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]; [ShareSDK registerApp:@"1a2e7ab5fb6c"]; //添加新浪微博应用 注册网址 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]]; 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{ } @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 三方分享/登录使用的更多相关文章

  1. iOS - ShareSDK第三方分享(图文和视频)和登录

    由于近期工作需要自己抽时间搞了一下第三方分享,这里使用的是shareSDK的第三方,在使用的过程中有一些心得和体会,特在此和大家分享一下~ 1.在经过将近一周时间的开发,终于搞定ios分享了. 2.由 ...

  2. iOS:ShareSDk的分享

    使用分享类的SDK其实有很多,例如友盟.ShareSDK等等,参照他们的文档集成起来并不是很难,可能出的一些问题也就是配置文件的问题,这里我个人使用了ShareSDK分享,具体操作可出现的问题如下: ...

  3. iOS之ShareSDK实现分享、第三方登录等功能

    (1)官方下载ShareSDK iOS 2.8.8,地址:http://sharesdk.cn/ (2)根据实际情况,引入相关的库,参考官方文档. (3)在项目的AppDelegate中一般情况下有三 ...

  4. iOS开发---集成ShareSDK实现第三方登录、分享、关注等功能。

    (1)官方下载ShareSDK IOS 2.9.6,地址:http://sharesdk.mob.com/Download (2)根据实际情况,引入相关的库,参考官方文档. (3)在项目的AppDel ...

  5. IOS项目集成ShareSDK实现第三方登录、分享、关注等功能。

    (1)官方下载ShareSDK iOS 2.8.8,地址:http://sharesdk.cn/ (2)根据实际情况,引入相关的库,参考官方文档. (3)在项目的AppDelegate中一般情况下有三 ...

  6. IOS项目集成ShareSDK实现第三方登录、分享、关注等功能(备用)

    (1)官方下载ShareSDK iOS 2.8.8,地址:http://sharesdk.cn/ (2)根据实际情况,引入相关的库,参考官方文档. (3)在项目的AppDelegate中一般情况下有三 ...

  7. IOS项目集成ShareSDK实现第三方登录、分享、关注等功能

    (1)官方下载ShareSDK iOS 2.8.8,地址:http://sharesdk.cn/ (2)根据实际情况,引入相关的库,参考官方文档. (3)在项目的AppDelegate中一般情况下有三 ...

  8. iOS:IOS项目集成ShareSDK实现第三方登录、分享、关注等功能。

    原文链接:http://blog.csdn.net/daleiwang/article/details/34081231 (3)在项目的AppDelegate中一般情况下有三个操作,第一是注册Shar ...

  9. Android开发利用shareSDK等第三方分享,弹出的是英文名称。例如Genymotion模拟器

    作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985 Android开发利用shareSDK等第三方分享,弹出的是英文名称.例如Genymotion模拟器就 ...

随机推荐

  1. C# 处理Word自动生成报告 二、数据源例子

    还是以学生.语文.数学.分数为例吧, 感觉这个和helloworld都有一拼了. 造一张表如下, 整张报表就围绕这个表转圈了, 顺便说下就是名字如有雷同纯属巧合 新建个存储过程 ALTER PROCE ...

  2. sqli-libs

    这是抄袭你们  铃兰师姐总结的,尽管她很不情愿. (现在只是第八关,相信她会努力的,一定会做完,我也会随时在这个上面更新的,这个是在本地搭建的,想要这个的可以找你们铃兰师姐要啊!!!) less-1: ...

  3. Cubieboard2安装Fedora20

    几天前入手一块Cubieboard2,又买了张16G的TF卡,装个linux折腾折腾.以前都是在虚拟机上用linux,个人比较喜欢Fedora,因为总能用上最新版的软件,像支持C++11的GCC.Cl ...

  4. Lua API 小记1

    这些东西是平时遇到的, 觉得有一定的价值, 所以记录下来, 以后遇到类似的问题可以查阅, 同时分享出来也能方便需要的人, 转载请注明来自RingOfTheC[ring.of.the.c@gmail.c ...

  5. npm模块管理器入门

    什么是 NPM npm 是 Node 官方提供的包管理工具,他已经成了 Node 包的标准发布平台,用于 Node 包的发布.传播.依赖控制.npm 提供了命令行工具,使你可以方便地下载.安装.升级. ...

  6. debug断点调试

    debug断点调试  1,虫子启动2,F6   执行断点的下一步,下一个语句     F5    进入方法     F8   执行到结束    查看表达式的值:选中查看的表达式,接着按   ctrl ...

  7. (四):C++分布式框架——状态中心模块

    (四):C++分布式框架--状态中心模块 上篇:(三):C++分布式实时应用框架--系统管理模块 技术交流合作QQ群:436466587 欢迎讨论交流 版权声明:本文版权及所用技术归属smartguy ...

  8. Mariadb Galera Cluster 群集 安装部署

    #Mariadb Galera Cluster 群集 安装部署 openstack pike 部署  目录汇总 http://www.cnblogs.com/elvi/p/7613861.html # ...

  9. OBS源码解析(3)OBSApp类介绍

    OBSApp类有以下功能: 1.负责配置文件管理 2.版本信息管理 3.主界面OBSBasic对象管理 4.obs模块初始化 class OBSApp : public QApplication { ...

  10. 采用Opserver来监控你的ASP.NET项目系列(二、监控SQL Server与Asp.Net项目)

    前言 之前有过2篇关于如何监控ASP.NET core项目的文章,有兴趣的也可以看看. 今天我们主要来介绍一下,如何使用Opserver监控我们的SQL Server 和ASP.NET项目的异常监控 ...