1.导入SDK
将ShareSDK文件夹到项目文件夹中夹并拖入项目中。
2. 添加依赖框架(Framework)
打开项目设置中的Build Phases页,在“LinkBinary With Libraries”一栏中,点击左下角的“+”号;在弹出窗口里面分别以下库加入到项目中:
SystemConfiguration.framework
QuartzCore.framework
MessageUI.framework (如果不集成邮件和短信可以不添加)
libicucore.dylib
libz.1.2.5.dylib
libstdc++.dylib (如果不需要QQ空间SSO登录可以不添加)
libsqlite3.dylib (如果不需要QQ空间SSO登录可以不添加)
CoreTelephony.framework (如果不需要QQ空间SSO登录可以不添加)
3. 导入头文件ShareSDK.h并初始化社交平台的App信息。
打开*AppDelegate.m(*代表你的工程名字)
导入文件头ShareSDK.h:
在- (BOOL)application:didFinishLaunchingWithOptions:方法中添加如下语句对SDK进行初始化并为各个分享平台的应用信息进行设置,
初始化SDK使用的appKey(调用registerApp方法)在ShareSDK官网中可以注册申请,建议为每个应用申请一个appKey,如果多个应用用同一个key会导致不同应用的统计数据混合在一起。
应用相关信息需要到各个平台进行注册申请(申请地址见文档附录),如果不需要使用到的平台可以不进行设置:
4. 为keyWindow设置rootViewController属性
请确认didFinishLaunchingWithOptions方法中是否有设置window对象的rootViewController属性。否则会导致无法弹出授权界面或者分享界面点击后无反映,卡死现象等。
设置类似于下面代码所示:
注:如果您创建应用时使用storyBoard可以省略此步骤,系统以自动为您设置了rootViewController。
5. 加入handleOpenURL处理方法
在使用SSO授权方式(即跳转到相应客户端进行授权的方式)或者集成微信、QQ好友分享时,需要在处理请求URL的委托方法中加入ShareSDK的处理方法(没有以上说明情况可省略此步骤),在*AppDelegate.m(*代表你的工程名字)文件中加入如下方法:
6. 添加微信社交平台(不需要微信分享可略过此步骤)
登录微信开放平台(http://open.weixin.qq.com/)注册应用并取得应用的AppID,然后打开工程中的*AppDelegate.m(*代表你的工程名字)
文件,导入微信SDK的头文件:
在-application: didFinishLaunchingWithOptions: 方法内添加如下语句:
打开*-Info.plist(*代表你的工程名字)。找到URL types配置项(如果没有则新增),展开URL types – URL Schemes,在URLSchemes下分别各新增一项用于微信的Scheme(如果不添加则会导致法返回应用)。将申请的appId填入此配置项中。如图所示:
注:必须执行步骤5中的操作。
7. 添加QQ社交平台(不需要QQ好友分享可略过此步骤)
登录手机QQ开发者平台(http://mobile.qq.com/api/)注册成为开发者,并登记应用取得APPID,然后打开工程中的*AppDelegate.m(*代表你的工程名字)
文件,导入QQSDK的头文件:
注:如果你使用QQ空间SSO授权方式,那么不能引入以上头文件,否则会导致名称空间冲突。应修改为导入以下头文件:
在- (BOOL)application:didFinishLaunchingWithOptions:方法内添加如下语句:
打开*-Info.plist(*代表你的工程名字)。找到URL types配置项(如果没有则新增),展开URL types – URL Schemes,在URLSchemes下分别各新增一项用于QQ的Scheme(如果不添加则会导致法返回应用)。将申请的appId填入此配置项中。如图所示:
注:必须执行步骤5中的操作。
8. 配置SSO授权(没有使用SSO授权可略过此步骤) 注:在执行该步骤时必须完成步骤5中的操作。
8.1新浪微博
打开*-Info.plist(*代表你的工程名字)。找到URLtypes配置项(如果没有则新增),展开URL types – URL Schemes,在URL Schemes下分别各新增一项用于新浪微博的Scheme(如果不添加则会导致法返回应用)。其填写格式为:sinaweibosso.2279784657,后面的数字为你在新浪微博申请的AppKey。如图所示:
8.2 Facebook
打开*-Info.plist(*代表你的工程名字)。找到URLtypes配置项(如果没有则新增),展开URL types – URL Schemes,在URL Schemes下分别各新增一项用于Facebook的Scheme(如果不添加则会导致法返回应用)。其填写格式为:fb107704292745179,后面的数字为你在Facebook申请的AppKey。如图所示:
8.3 QQ空间 先确认是否已把TencentOpenAPI.framework以及对应的资源包导入到项目中。 打开*AppDelegate.h(*代表你的工程名字) 文件,引入头文件:
打开*AppDelegate.m(*代表你的工程名字) 文件,修改初始化QQ空间的代码,由原来的
改为
打开*-Info.plist(*代表你的工程名字)。找到URL types配置项(如果没有则新增),展开URL types – URL Schemes,在URLSchemes下分别各新增一项用于QQ空间的Scheme(如果不添加则会导致法返回应用)。其填写格式为:tencent100371282,后面的数字为你在QQ空间中申请的AppId。如图所示: 8.4 腾讯微博 先确认是否已把libWeiboSDK.a以及对应的头文件导入到项目中。 打开*AppDelegate.h(*代表你的工程名字) 文件,引入头文件:
打开*AppDelegate.m(*代表你的工程名字) 文件,修改初始化腾讯微博的代码,由原来的
改为
打开*-Info.plist(*代表你的工程名字)。找到URL types配置项(如果没有则新增),展开URL types – URL Schemes,在URLSchemes下分别各新增一项用于腾讯微博的Scheme(如果不添加则会导致法返回应用)。其填写格式为:wb801307650,后面的数字为你在腾讯微博中申请的AppKey。如图所示: 8.5 Pocket 打开*-Info.plist(*代表你的工程名字)。找到URL types配置项(如果没有则新增),展开URL types – URL Schemes,在URLSchemes下分别各新增一项用于Pocket的Scheme(如果不添加则会导致法返回应用)。其填写格式为:pocketapp1234,后面的数字可以为任意值。如图所示: 注:初始化中的回调地址必须与填写的url scheme一致。如:
9.分享内容
在分享内容部分ShareSDK提供了三种接口来满足不同需求的分享功能实现。
9.1 菜单方式分享:
使用此方式进行分享会首先弹出菜单供用户选择分享的目标平台,然后再显示内容编辑界面供用户进行分享内容编辑,最后进行分享。调用该方式的接口如下:
以上代码中主要使用[ShareSDKshowShareActionSheet: shareList: content: statusBarTips: authOptions: shareOptions:esult]方法进行分享。
第一个参数用于指定菜单显示在哪个视图控制器上。如果指定为nil,则表示使用显示在最顶层的UIViewController对象。(注:如果需要适配iPad版本,需要调用container对象中的相关方法设置iPad容器,否则无法在iPad中正常显示。)
第二个参数则用于指定菜单弹出的分享平台列表,传入nil则表示显示所有分享平台,如果开发者需要自己定制显示列表则通过此参数进行控制。ShareSDK提供了getShareListWithType方法来进行分享列表的构造。
第三个参数为实现了ISSContent协议的对象,需要调用了ShareSDK的+ (id<ISSContent>)content: defaultContent:image: title: url: description: mediaType:;方法来构造对象。(注:id<ISSContent>对象提供了对部分平台,如:微信、QQ、QQ空间、邮件、短信、有道云笔记、Instapaper平台的内容定制,通过定制可以针对不同的平台发送不同的内容。详细请参考Demo程序)
第四个参数为是否在状态栏上显示提示信息(包括成功和错误提示)。
第五个参数用于指定在分享过程中,需要授权时的一些信息配置。如:是否需要自动弹出授权界面然后授权、授权时是否回调服务器以及授权视图的委托等。需要说明的是在设置是否需要自动授权标志为NO时,则SDK对于需要授权的状态不会弹出授权界面,需要用户自行处理。
第六个参数用于指定分享时的一些信息配置。如:分享视图标题、一键分享列表的定制、微信、QQ分享按钮的隐藏以及分享视图委托等。
第七个参数用于处理分享返回后续的处理方法。返回方法中包括了分享状态、分享成功后的状态信息(StatusInfo)、分享失败时的错误信息、以及本次分享会话是否结束标识。
9.2 分享内容视图方式
使用此方式直接弹出内容分享编辑框,用户编辑完内容后点击发布直接分享出去。其调用代码如下:
以上代码主要使用主要使用[ShareSDKshowShareViewWithType: container: content:statusBarTips: authOptions: shareOptions: result:]方法进行分享。
第一个参数为分享的目标平台类型。其他参数与菜单方式中的content参数含义相同。其显示效果如上面编辑分享内容视图截图。
9.3 直接分享方式
此方式不需要通过任何分享界面直接发送到指定的平台。其提供了两种不同的方法来满足不同的功能,第一种分享到单个平台,调用代码如下:
以上代码中主要使用[ShareSDKshareContent: type: authOptions: statusBarTips: result:]方法进行分享。其参数说明与上面带分享界面的功能接口参数说明相同。
另外一种方法就是可以通过传入多个平台类型来进行一键分享信息。其调用代码如下:
以上代码主要使用[ShareSDKoneKeyShareContent: shareList: authOptions: statusBarTips: result:]方法进行分享,其中shareList参数表示需要将内容分享到的目标平台类型列表,[NSArraydefaultOneKeyShareList表示分享到所有支持一键分享的平台中。其余参数与其他分享接口意义相同。
9.4 定制分享
对于需要定义分享视图的样式、标题以及部分属性(如控制默认分享视图中的QQ、微信分享按钮显示)或者调整分享视图时,可以在各个分享接口的shareOptions参数中进行设定。如:
ShareSDK提供了三种构造ShareOptions对象的方法,分别对应默认、简单以及应用推荐三种分享视图样式,其分别是:
+ defaultShareOptionsWithTitle: oneKeyShareList:qqButtonHidden: wxSessionButtonHidden: wxTimelineButtonHidden: showKeyboardOnAppear:shareViewDelegate: friendsViewDelegate: picViewerViewDelegate:;
+ simpleShareOptionsWithTitle: shareViewDelegate:;
+ appRecommendShareOptionsWithTile: shareViewDelegate:;
title参数表示了设置分享视图的标题,oneKeyShareList参数控制一键分享列表的显示(在默认分享视图底部显示的平台列表),qqButtonHidden、wxSessionButtonHidden、wxTimelineButtonHidden则分别是控制默认分享视图下的QQ、微信好友、微信朋友圈按钮的显示。showKeyboardOnAppear参数控制分享视图显示时是否弹出键盘。shareViewDelegate参数表示分享视图委托,friendsViewDelegate参数表示好友列表视图委托。picViewerViewDelegate参数表示照片查看视图委托。
9.5 定制属于自己的分享UI
结合+ shareContent: type: authOptions: statusBarTips: result:;和+ authWithType: options: result:;以及+ hasAuthorizedWithType:;方法来打造属于自己的分享UI。下面代码使用简单的UITextView和UIButton来搭建简单的新浪微博分享界面。代码如下:
9.6 iPad版本分享界面集成
如果需要在iPad中显示分享,则需要使用+container;功能接口创建一个容器对象并调用容器对象的setIPhoneContainer系列方法来设置iPad版显示容器。其实现代码如下:
10. 激活/禁止SSO登录。
用于控制平台是否使用SSO登录方式进行登录(目前只支持新浪微博、Facebook),如下代码禁止SSO登录:
11. 获取当前用户信息
获取当前授权用户信息,其调用方式如下:
其中第一个参数为平台类型,用于指定获取哪个平台的授权用户信息。如果指定平台的用户尚未进行授权则弹出授权界面。第二个参数为授权选项参数。第三个参数为授权返回的处理方法。
12. 关注用户
关注指定用户(此接口目前仅支持新浪微博和腾讯微博),其调用方式如下:
其中第一个参数为关注用户所属平台类型,第二个参数为关注用户的昵称或者用户ID(具体指示昵称还是用户ID由第三个参数控制),第三个参数为标识第二个参数的字段类型)。第四个参数为授权选项配置,第五个参数为视图委托,主要用于Facebook关注时弹出的添加好友界面的委托。
13. 获取关注列表
获取当前授权用户的关注列表(此接口目前仅支持新浪微博、腾讯微博、Facebook、Twitter),其调用方式如下:
其中第一个参数为平台类型,用于指定获取哪个平台的授权用户关注列表,如果指定平台的用户尚未进行授权则弹出授权界面。第二个参数为取关注列表的分页对象,用于指定要获取那一页的好友列表数据;ShareSDK中提供了两种构造分页对象的方法+ pageWithCursor:;和+ pageWithPageNo: pageSize:;。第三个参数为授权选项设置,第四个参数为关注列表返回的处理方法。
14. 用户授权
要想操作相关平台提供的方法必须要先取得用户授权。ShareSDK提供了单独的授权接口来实现用户的登录授权。其代码如下:
第一个参数为需要进行授权的平台类型。第二个参数为授权选项设置,如果需要定制授权视图的样式需要指定此参数。第三个参数则是授权返回结果的处理方法。需要注意的是ShareSDK中提供的各种需要进行授权的接口(包括分享、获取用户信息等)都已经在内部进行授权检测判断,不需要开发者单独进行授权。
授权界面效果如下所示:
授权界面
15. 用户取消授权
如果需要取消授权可以调用ShareSDK的cancelAuthWithType接口,如下:
16. 从外部授权信息中设置ShareSDK平台授权。
如果代码中已实现某平台的授权,那么可以通过ShareSDK中的+ setCredential:;接口设置平台的授权信息。如下:
其中 sourceData 为授权返回的数据。 |
SDK 使用详解
最新推荐文章于 2024-08-20 15:28:49 发布