最近一段时间使用shareSDK,看教程和网上都说很简单。确实简单,可是中间仍然有很多错误,经过多方面查找资料,和自己研究修改。终于算是做出来了,不过距离我想要的还有些差距,先把大致的总结下。如果有什么不对的地方,请不吝赐教。让我和看到的人一起get下新技能。。谢谢!!
以下资料来源:shareSDK官方文档,百度文库,csdn,博客园,微博,我自己。。。
1.在shareSDK(http://www.shareSDK.cn)下载官方sdk并导入工程,将sharesdk文件夹拖入工程内你想放入的文件夹下(并不是整个下载的文件夹都要,只要其中的sharesdk),并勾选copy items into destination group's folder (if needed)点击finish保存。
2.添加依赖框架(framework)
打开项目设置(targets)中的build phases,在Link Binary With Libraries一栏中,点击坐下角的+号,在弹出窗口里添加以下库。
SystemConfiguration.framework
QuartzCore.framework
CoreTelephony.framework
libicucore.dylib
libz.1.2.5.dylib
Security.framework
libstdc++.dylib 如果不需要QQ空间SSO登录和QQ好友分享可以不添加(这个如果是建立在你把SDK中对应的平台库文件删除掉,才能不添加,由于下载的SDK中每个平台的库都是默认存在的,所以必须要加上这个库,以下同理)
libsqlite3.dylib 如果不需要QQ空间SSO登录和QQ好友分享可以不添加
CoreMotion.framework 如果不使用Google+可以不添加
CoreLocation.framework 如果不使用Google+可以不添加
MediaPlayer.framework 如果不使用Google+可以不添加
CoreText.framework 如果不使用Google+可以不添加
AssetsLibrary.framework 如果不使用Google+可以不添加
AddressBook.framework 如果不使用Google+可以不添加
MessageUI.framework 如果不集成邮件和短信可以不添加
3.
-
登录ShareSDK的官方网站添加应用并获得AppKey。如果尚未注册,请到官网注册。
-
初始化ShareSDK
打开*AppDelegate.m(*代表你的工程名字) 导入文件头ShareSDK.h
#import <ShareSDK/ShareSDK.h>
在- (BOOL)application: didFinishLaunchingWithOptions:方法中调用registerApp方法来初始化SDK
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
[ShareSDK registerApp:@"api20"]; //参数为ShareSDK官网中添加应用后得到的AppKey
return YES;
}
在处理请求url的委托方法中加入shareSDK的处理方法,如下
-(BOOL)application:(UIApplication*)application handleOpenURL:(NSURL*)url
{
return [ShareSDK handleOpenURL:url wxDelegate:sele];
}
-(BOOL)application:(UIApplication*)application openURL:(NSURL*)url soureApplication:(NSString*)soureApplication annotation:(id)annnotation
{
return [ShareSDK handleOpenURL:url wxDelegate:self];
}
此方法包括微信和qq的回复处理,如果使用sharesdk内置提示功能可以不需要在实现消息的微信和qq的回复消息捕获。
4.配置社会化平台的AppKeys
(温馨提示:因为SDK优先默认的是后台的每个平台的初始化信息,所以如果后台的初始化有开启,也就是社会平台化设置选项那里平台状态有勾上的话,那就一定要与你本地初始化信息配置的一致)
Appdelegate.m中:
在- (BOOL)application: didFinishLaunchingWithOptions:方法中添加如下语句为各个分享平台的应用信息进行设置.
- (BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
//……
//添加新浪微博应用 注册网址 http://open.weibo.com
[ShareSDK connectSinaWeiboWithAppKey:@"568898243"
appSecret:@"38a4f8204cc784f81f9f0daaf31e02e3"
redirectUri:@"http://www.sharesdk.cn"];
//当使用新浪微博客户端分享的时候需要按照下面的方法来初始化新浪的平台
[ShareSDK connectSinaWeiboWithAppKey:@"568898243"
appSecret:@"38a4f8204cc784f81f9f0daaf31e02e3"
redirectUri:@"http://www.sharesdk.cn"
weiboSDKCls:[WeiboSDK class]];
//添加腾讯微博应用 注册网址 http://dev.t.qq.com
[ShareSDK connectTencentWeiboWithAppKey:@"801307650"
appSecret:@"ae36f4ee3946e1cbb98d6965b0b2ff5c"
redirectUri:@"http://www.sharesdk.cn"
wbApiCls:[WeiboApi class]];
//添加QQ空间应用 注册网址 http://connect.qq.com/intro/login/
[ShareSDK connectQZoneWithAppKey:@"100371282"
appSecret:@"aed9b0303e3ed1e27bae87c33761161d"
qqApiInterfaceCls:[QQApiInterface class]
tencentOAuthCls:[TencentOAuth class]];
//添加QQ应用 注册网址 http://open.qq.com/
[ShareSDK connectQQWithQZoneAppKey:@"100371282"
qqApiInterfaceCls:[QQApiInterface class]
tencentOAuthCls:[TencentOAuth class]];
//添加微信应用 注册网址 http://open.weixin.qq.com
[ShareSDK connectWeChatWithAppId:@"wx4868b35061f87885"
wechatCls:[WXApi class]];
//添加网易微博应用 注册网址 http://open.t.163.com
[ShareSDK connect163WeiboWithAppKey:@"T5EI7BXe13vfyDuy"
appSecret:@"gZxwyNOvjFYpxwwlnuizHRRtBRZ2lV1j"
redirectUri:@"http://www.shareSDK.cn"];
//添加搜狐微博应用 注册网址 http://open.t.sohu.com
[ShareSDK connectSohuWeiboWithConsumerKey:@"SAfmTG1blxZY3HztESWx"
consumerSecret:@"yfTZf)!rVwh*3dqQuVJVsUL37!F)!yS9S!Orcsij"
redirectUri:@"http://www.sharesdk.cn"];
//添加豆瓣应用 注册网址 http://developers.douban.com
[ShareSDK connectDoubanWithAppKey:@"07d08fbfc1210e931771af3f43632bb9"
appSecret:@"e32896161e72be91"
redirectUri:@"http://dev.kumoway.com/braininference/infos.php"];
//添加人人网应用 注册网址 http://dev.renren.com
[ShareSDK connectRenRenWithAppId:@"226427"
appKey:@"fc5b8aed373c4c27a05b712acba0f8c3"
appSecret:@"f29df781abdd4f49beca5a2194676ca4"
renrenClientClass:[RennClient class]];
//添加开心网应用 注册网址 http://open.kaixin001.com
[ShareSDK connectKaiXinWithAppKey:@"358443394194887cee81ff5890870c7c"
appSecret:@"da32179d859c016169f66d90b6db2a23"
redirectUri:@"http://www.sharesdk.cn/"];
//添加Instapaper应用 注册网址 http://www.instapaper.com/main/request_oauth_consumer_token
[ShareSDK connectInstapaperWithAppKey:@"4rDJORmcOcSAZL1YpqGHRI605xUvrLbOhkJ07yO0wWrYrc61FA"
appSecret:@"GNr1GespOQbrm8nvd7rlUsyRQsIo3boIbMguAl9gfpdL0aKZWe"];
//添加有道云笔记应用 注册网址 http://note.youdao.com/open/developguide.html#app
[ShareSDK connectYouDaoNoteWithConsumerKey:@"dcde25dca105bcc36884ed4534dab940"
consumerSecret:@"d98217b4020e7f1874263795f44838fe"
redirectUri:@"http://www.sharesdk.cn/"];
//添加Facebook应用 注册网址 https://developers.facebook.com
[ShareSDK connectFacebookWithAppKey:@"107704292745179"
appSecret:@"38053202e1a5fe26c80c753071f0b573"];
//添加Twitter应用 注册网址 https://dev.twitter.com
[ShareSDK connectTwitterWithConsumerKey:@"mnTGqtXk0TYMXYTN7qUxg"
consumerSecret:@"ROkFqr8c3m1HXqS3rm3TJ0WkAJuwBOSaWhPbZ9Ojuc"
redirectUri:@"http://www.sharesdk.cn"];
//添加搜狐随身看应用 注册网址 https://open.sohu.com
[ShareSDK connectSohuKanWithAppKey:@"e16680a815134504b746c86e08a19db0"
appSecret:@"b8eec53707c3976efc91614dd16ef81c"
redirectUri:@"http://sharesdk.cn"];
//添加Pocket应用 注册网址 http://getpocket.com/developer/
[ShareSDK connectPocketWithConsumerKey:@"11496-de7c8c5eb25b2c9fcdc2b627"
redirectUri:@"pocketapp1234"];
//添加印象笔记应用 注册网址 http://dev.yinxiang.com
[ShareSDK connectEvernoteWithType:SSEverNoteTypeSandbox
consumerKey:@"sharesdk-7807"
consumerSecret:@"d05bf86993836004"];
//添加LinkedIn应用 注册网址 https://www.linkedin.com/secure/developer
[ShareSDK connectLinkedInWithApiKey:@"ejo5ibkye3vo"
secretKey:@"cC7B2jpxITqPLZ5M"
redirectUri:@"http://sharesdk.cn"];
//……
}
注意:可以根据自己需要集成的平台来选择部分平台进行初始化。配置中指定的AppKey等信息需要到相关的社会化平台中通过应用登记来获取。
运行当中有可能会报错,部分问题及解决方案如下:
报这个错:Use of undeclared identifier 'ShareSDK'
解决方法:添加#import <ShareSDK/ShareSDK.h>
报这个错:Use of undeclared identifier 'WXApi' //微信应用
解决方法:添加 #import "WXApi.h"
报这个错:Use of undeclared identifier 'WeiboApi'
解决方法:添加 #import "WeiboApi.h"
报这个错:Use of undeclared identifier 'WeiboSDK'
解决方法:添加#import "WeiboSDK.h"
报这个错:Use of undeclared identifier 'RennClient'
解决方法:添加#import <RennSDK/RennSDK.h>
报这个错:Use of undeclared identifier 'QQApilnterface'
解决方法:添加#import <TencentOpenAPI/QQApiInterface.h>
报这个错:Use of undeclared identifier 'TencentOAuth'
解决方法:添加#import <TencentOpenAPI/TencentOAuth.h>
ISSContent.h类里的方法,可以设置需要分享的内容,并获取你分享之后的内容,有中文注释。
typedef enum
{
SSPublishContentMediaTypeText =0,
SSPublishContentMediaTypeImage =1,
SSPublishContentMediaTypeNews =2,
SSPublishContentMediaTypeMusic =3,
SSPublishContentMediaTypeVideo =4,
SSPublishContentMediaTypeApp=5,
SSPublishContentMediaTypeNonGif =6,
SSPublishContentMediaTypeGif=7
}
SSPublishContentMediaType;
5.设置rootviewcontroller
在- (BOOL)application: didFinishLaunchingWithOptions:方法中检查是否有设置window对象的rootViewController属性。如果没有,则进行设置。类似如下代码:
-(BOOL)application:(UIApplication*)application didFinishLaunchingWithOptions:(NSDictionary*)launchOptions
{
//……
self.window = [[[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]] autorelease];
self.viewController = [[[UINavigationController alloc] init] autorelease];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
注意:必须设置rootViewController,否则会导致无法弹出授权界面或者分享界面点击后无反映,卡死现象等。如果您创建应用时使用storyBoard可以省略此步骤,系统会自动设置rootViewController。
6.可能需要配置URL Scheme
打开*-info.plist(*代表你得工程名字)。在配置文件中新增一项URL types (如果存在可以不创建),展开URL types-URL Schemes,在URL Schemes 下新增一项用于新浪微博授权的Scheme(如果不添加则会导致新浪微博的SSO授权方式不发返回应用)。其填写格式为:sinaweibosso.XXXXXXX,其中后面的数字段为你在新浪微博中申请的AppKey。另外,如果需要使用微信或者QQ平台接口也需要在此项下面添加相应的Scheme。如下:
7.分享内容:
在分享内容部分,sharesdk提供了三种接口来满足不同需求的分享功能实现。
第一种:菜单方式分享:
使用这个方法新型分享会首先弹出菜单提供用户选择分享的目标平台,然后再显示内容编辑界面攻用户新型分享内容编辑,最后进行分享。调用接口如下:
ViewController.m文件
- (void)loadView{
[superloadView];
UIButton*button = [UIButton buttonWithType:UIButtonTypeCustom];
button.frame =CGRectMake(50,100,50,60);
[button setTitle:@"分享"forState:UIControlStateNormal];
[button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
button.backgroundColor =[UIColor clearColor];
[button addTarget:selfaction:@selector(share) forControlEvents:UIControlEventTouchUpInside];
[self.viewaddSubview:button];
}
- (void)share{
/* 非官方文档
//分享的底ViewControoler
id<ISSContainer> container = [ShareSDKcontainer];
//自动授权 id<ISSAuthOptions> authOptions = [ShareSDKauthOptionsWithAutoAuth:YES
allowCallback:NO
authViewStyle:SSAuthViewStyleModal
viewDelegate:nil
authManagerViewDelegate:nil];
//这个是你要分享哪个,那么就要写上哪个,我用的是模拟器,所以没有qq,所以你想分享qq必须先安装有qq.否则是不会成功的
//在授权页面中添加关注官方微博 [authOptions setFollowAccounts:@{
SHARE_TYPE_NUMBER(ShareTypeQQSpace): [ShareSDK userFieldWithType:SSUserFieldTypeName
value:@"你妹的"],
SHARE_TYPE_NUMBER(ShareTypeQQ):[ShareSDKuserFieldWithType:SSUserFieldTypeNamevalue:@"坑货"]}];
//要分享的列表 NSArray*shareList = [ShareSDK getShareListWithType:ShareTypeSinaWeibo,ShareTypeTencentWeibo,
ShareTypeSohuWeibo,ShareType163Weibo,ShareTypeDouBan,ShareTypeQQSpace,ShareTypeRenren,ShareTypeKaixin,
ShareTypePengyou,ShareTypeFacebook,ShareTypeTwitter,ShareTypeEvernote,ShareTypeFoursquare,
ShareTypeGooglePlus,ShareTypeInstagram,ShareTypeLinkedIn,ShareTypeTumblr,ShareTypeMail,ShareTypeSMS,
ShareTypeAirPrint,ShareTypeCopy,ShareTypeWeixiSession,ShareTypeWeixiTimeline,ShareTypeQQ,
ShareTypeInstapaper,ShareTypePocket,ShareTypeYouDaoNote,ShareTypeSohuKan,ShareTypePinterest,
ShareTypeFlickr,ShareTypeDropbox,ShareTypeVKontakte,ShareTypeWeixiFav,ShareTypeYiXinSession,
ShareTypeYiXinTimeline,ShareTypeYiXinFav,ShareTypeMingDao,ShareTypeLine,ShareTypeWhatsApp,nil];
*/
/*官方文档*/
//分享的内容
id<ISSContent>publishContent = [ShareSDKcontent:@"分享内容"
defaultContent:@"默认分享内容,没内容时显示"
image:nil
title:@"这说说真的只是在测试,请别相信它!"
url:@"http://www.sharesdk.cn"
description:@"这。。。真的只是在测试吗,骚年!"
mediaType:SSPublishContentMediaTypeNews]; //弹出分享菜单
[ShareSDK showShareActionSheet:self
shareList:shareList
content:publishContent
statusBarTips:YES
authOptions:authOptions
shareOptions:shareOptions result:^(ShareType type,SSResponseState state,
id<ISSPlatformShareInfo>statusInfo,
id<ICMErrorInfo> error, BOOL end)
{
if(state ==SSResponseStateSuccess){
NSLog(@"share success!");
}
if(state ==SSResponseStateFail){
NSLog(@"share fail! error code:%derroe description:%@",[error errorCode],[errorerrorDescription]);
}
}
];
}
第一个参数用于指定菜单显示在哪一个视图上。
第二个参数则用于指定菜单弹出的分享平台列表,传入nil则表示显示所有的分享平台,需要自己定制显示列表则通过此参数进行修改。sharesdk提供了getShareListWithType方法来新型分享列表的构造。
第三个参数为实现了ISSPublishContent协议的对象,需要调用sharesdk的publishContent来构造对象。需要特别说明一下content参数对象除了基础的图片和文字字段外还包括了微信和qq这两个平台的其他分享类型参数字段(包括News、Music、video等,通过mediatype来指定分享出去的类型),如果不需要使用微信和qq平台接口的腐女向功能,可以在构造分享内容对象时把参数设置为nil或默认。
第四个参数为是否在状态栏上显示提示信息(成功和错误)。
第五个参数为授权选项。
第六个参数为分享相关设置。
(关于五六参数相关设置:
其实要修改导航栏需要在分享接口(showShareActionSheet或者showShareView)中指定authOptions的viewDelegate和shareOptions中的shareViewDelegate参数。其中authOptions是授权相关的设置,而shareOptions则是分享相关的设置。那么这两个对象都可以通过ShareSDK相关方法进行初始化,如下面代码所示:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
以上两段代码就是对authOptions和shareOptions的构造,那么authOptions中有一个参数叫viewDelegate,此参数是一个实现了ISSViewDelegate的协议对象。只要实现了ISSViewDelegate的对象都可以传入此参数。而shareOptions中有一个参数叫shareViewDelegate,此参数是实现了ISSShareViewDelegate的协议对象,而ISSShareViewDelegate是继承了ISSViewDelegate的,因此拥有ISSViewDelegate的所有委托方法,并且有自己的ShareViewDelegate的特有方法。
那么对于需要修改导航栏样式来说,只要实现ISSViewDelegate的viewOnWillDisplay方法即可,也就是说在视图将要显示的时候来调整导航样式。我们可以在要显示分享视图的ViewController中实现ISSViewDelegate协议(注:如果分享界面也要调整样式,那么只要实现ISSShareViewDelegate即可)。我们这里要显示分享界面的是DemoViewController,那么在DemoViewController.h中修改代码如下:
|
|
然后转到DemoViewController.m文件,在类实现中写入下面代码:
|
|
|
|
|
|
|
|
此委托方法中第一个参数就是将要显示的ViewController,如果是授权那么就是授权的ViewController,如果是分享那么就是分享的ViewController。在通过这个ViewController的navigationController可以取得导航控制器,然后在导航控制器中就可以去取到导航栏的对象了。那么就可以对导航栏进行调整了。
)
第七个参数为永固处理分享返回后续的处理方法。
第二种 分享内容视图方式:
在用此方式直接弹出内容分享编辑框,用户编辑玩内容后点击发布直接分享出去。其调用代码如下:
[ShareSDK shareContentWithType:ShareTypeSinaWeibo content:[ShareSDK publishContent:@"content"
defaultContent:@""
image:[UIImage imageNamed:@"icon.png"
imageQuality:0.8
mediaType:SSPublishContentMediaTypeText
containerController:self
statusBarTips:YES
oneKyeSareList:[NSArray defaultOneKeyShareList]
shareViewStyle:ShareViewStyleDefault
shareViewTitle:@"内容分享"
result:nil]];
第一个参数为分享的目标平台类型。其他参数与菜单方式中的content参数含义相同。
第三种 直接分享方式:
这种方式不需要通过任何分享界面直接发送到指定的平台。调用代码如下:
[ShareSDK shareContentWithList:[ShareSDK getShareLIstWithType:ShareTypeSinaWeibo,nil]
content:[ShareSDK publishContent:@"content"
defaultContent:@""
image:[UIImage imageNamed:@"icon.png"]
imageQuality:0.8
mediaType:SSPublishContentMediaTypeText
statusBarTips:YES
result:nil]];
其中第一参数为分享的目标平台类型数组,使用ShareSDK的getShareListWithType方法进行数组列表构造,其他参数与上面的所说的分享接口同名参数含义相同。