用shareSDK实现的简单分享

第一步:将ShareSDK导入到你的工程中

然后需要在工程的AppDelegate.m中导入所需要的头文件
比如:
#import <ShareSDK/ShareSDK.h>
#import "WeiboApi.h"
#import “WXApi.h"

第二步:在下面方法中添加如下代码

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    [ShareSDK registerApp:@"2864c8d42dd3];  //@“2864c8d42dd3”ShareSDK官网中添加应用后得到的AppKey
    
   添加相应的应用需要获得相应应用的APPkey和APPSecret

   
 //添加新浪微博应用 注册网址 http://open.weibo.com
    [ShareSDK connectSinaWeiboWithAppKey:@"1028290959"
                               appSecret:@"337c15ce9b1885cf307fd4f5bc5a9594"
                             redirectUri:@"http://www.sina.com"];
   
//    //添加腾讯微博应用 注册网址 http://dev.t.qq.com
    [ShareSDK connectTencentWeiboWithAppKey:@"801528473"
                                  appSecret:@"9101e92b599a40712212547be6977c2a"
                                redirectUri:@"http://www.shishangmao.com"
                                   wbApiCls:[WeiboApi class]];
   
    //添加微信应用 注册网址 http://open.weixin.qq.com
    [ShareSDK connectWeChatWithAppId:@"wx8216675fb452a3d6"
                           wechatCls:[WXApi class]];
    //连接邮件
    [ShareSDK connectMail];
 
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.
    self.window.backgroundColor = [UIColor whiteColor];
    self.viewController = [[ViewController alloc] init];
    self.window.rootViewController = self.viewController;
   
    [self.window makeKeyAndVisible];
    return YES;
}
 
注:以下代码为使用sso授权时添加(若不使用程序运行后将会跳到相应的web页进行授权提示)

使用SSO授权不需要重复输入新浪微博用户名、密码,只需要一步操作,直接点击授权按钮即可完成授权,增强了操作简便性及帐号安全性。SDK中默认是开启SSO授权的,对需要实现SSO的平台进行一下配置就可以使用了。(就是说如果你安装了相应应用的客户端软件之后,只需要登录一次 ,程序运行经过一次授权之后,再运行的话就不会重新提示授权,而是可直接发布(即使工程卸载重装也会记录相应授权的信息))

由于SSO需要跳转到客户端进行授权验证,因此需要处理返回消息。在*AppDelegate.m(*代表你的工程名字)文件中加入如下方法: 

- (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];
}
 
使用sso授权也需要添加相应应用的URL Schemes 如:新浪微博。。。。。

新浪微博

打开*-Info.plist(*代表你的工程名字)。找到URL types配置项(如果没有则新增),展开URL types – URL Schemes,在URL Schemes下分别各新增一项用于新浪微博的Scheme(如果不添加则会导致法返回应用)。其填写格式为:wb+appKey(你在新浪微博申请的AppKey), 如:wb2279784657。如图所示:

ios doc 039.jpg

 
 
 

第三步:分享内容

进行内容分享首选需要构造一个内容对象,该对象用于指定哪些内容需要分享到社会化平台中。然后再通过showShareActionSheet方法进行分享。 分享内容一般在 viewDidAppear 事件之后、响应用户动作的方法中进行,如:响应按钮按下的方法中。 代码如下:

NSString *imagePath = [[NSBundle mainBundle] pathForResource:@“1"  ofType:@"jpg"];

    

//构造分享内容
///#begin zh-cn
/**
 *@brief创建分享内容对象,根据以下每个字段适用平台说明来填充参数值
 *
 *@param content 分享内容(新浪、腾讯、网易、搜狐、豆瓣、人人、开心、有道云笔记、facebook、twitter、邮件、打印、短信、微信、QQ、拷贝)
 *@param defaultContent 默认分享内容(新浪、腾讯、网易、搜狐、豆瓣、人人、开心、有道云笔记、facebook、twitter、邮件、打印、短信、微信、QQ、拷贝)
 *@param image 分享图片(新浪、腾讯、网易、搜狐、豆瓣、人人、开心、facebook、twitter、邮件、打印、微信、QQ、拷贝)
 *@param title 标题(QQ空间、人人、微信、QQ)
 *@param url 链接(QQ空间、人人、instapaper、微信、QQ)
 *@param description 主体内容(人人)
 *@param mediaType 分享类型(QQ、微信)
 *
 *@return分享内容对象
 */
///#end
///#begin en
/**
 *@briefCreate share content object. based on the following description of each field to fill the parameter values
 *
 *@param content Share content string.(Sina Weibo、Tencent Weibo、NetEase Weibo、Sohu Weibo、Douban、RenRen、KaiXin、YouDaoNote、Facebook、Twitter、Mail、Print、SMS、WeChat、QQ、Copy)
 *@param defaultContent Default share content string.(Sina Weibo、Tencent Weibo、NetEase Weibo、Sohu Weibo、Douban、RenRen、KaiXin、YouDaoNote、Facebook、Twitter、Mail、Print、SMS、WeChat、QQ、Copy)
 *@param image Image attachment object.(Sina Weibo、Tencent Weibo、NetEase Weibo、Sohu Weibo、Douban、RenRen、KaiXin、Facebook、Twitter、Mail、Print、WeChat、QQ、Copy)
 *@param title Title string.(QZone、RenRen、WeChat、QQ)
 *@param url Url string.(QZone、RenRen、Instapaper、WeChat、QQ)
 *@param description Description string(RenRen)
 *@param mediaType MediaType(QQ、WeChat)
 *
 *@returnShare content object.
 */
///#end
id<ISSContent> publishContent = [ShareSDK content:@"分享内容"

                                          defaultContent:@"默认分享内容,没内容时显示"

                                          image:[ShareSDK imageWithPath:imagePath]

                                          title: @"北京时尚猫科技有限公司"

                                          url: @"http://www.shishangmao.cn/"

                                          description:@"这是一条测试信息"

                                          mediaType:SSPublishContentMediaTypeNews];
////********************************************************************************************************************
若想要自定义授权界面可实现如下
    //创建容器
    id<ISSContainer> container = [ShareSDK container];
    [container setIPadContainerWithView:sender arrowDirect:UIPopoverArrowDirectionUp];
   
    //通过viewDelegate:参数修改授权界面的导航栏背景
    //    id<ISSAuthOptions> authOptions = [ShareSDK authOptionsWithAutoAuth:YES
    //                                                         allowCallback:YES
    //                                                         authViewStyle:SSAuthViewStyleFullScreenPopup
    //                                                          viewDelegate:_appDelegate.viewDelegate
    //                                               authManagerViewDelegate:nil];
   
    //powerByHidden:这个参数是去掉授权界面Powered by ShareSDK的标志
    id<ISSAuthOptions> authOptions = [ShareSDK authOptionsWithAutoAuth:YES      allowCallback:NO
                                                                scopes:nil powerByHidden:YES followAccounts:nil authViewStyle:SSAuthViewStyleFullScreenPopup viewDelegate:nil authManagerViewDelegate:nil];
   
    //通过shareViewDelegate:参数修改分享界面的导航栏背景
    id<ISSShareOptions> shareOptions = [ShareSDK defaultShareOptionsWithTitle:@"内容分享"
                                                              oneKeyShareList:[NSArray defaultOneKeyShareList]
                                                               qqButtonHidden:YES
                                                        wxSessionButtonHidden:YES
                                                       wxTimelineButtonHidden:YES
                                                         showKeyboardOnAppear:NO
                                                            shareViewDelegate:nil
                                                          friendsViewDelegate:nil
                                                        picViewerViewDelegate:nil];
****************************************************************************************///
///#begin zh-cn
/**
 *@brief显示分享菜单
 *
 *@param container 用于显示分享界面的容器,如果只显示在iPhone客户端可以传入nil。如果需要在iPad上显示需要指定容器。
 *@param shareList 平台类型列表
 *@param content 分享内容
 *  @param  statusBarTips   状态栏提示标识:YES:显示; NO:隐藏
 *  @param  authOptions 授权选项,用于指定接口在需要授权时的一些属性(如:是否自动授权,授权视图样式等),默认可传入nil
 *  @param  shareOptions    分享选项,用于定义分享视图部分属性(如:标题、一键分享列表、功能按钮等),默认可传入nil
 *  @param  result  分享返回事件处理
 */
///#end
///#begin en
/**
 *@briefShow share menu.
 *
 *@param container A container for the share view, if only displayed in the iPhone can pass nil. If you want to display on the iPad needs to be specified container.
 *@param shareList Platform type list.
 *@param content Share content object.
 *  @param  statusBarTips   The status bar Tip flag. YES indicates display. NO indicates hidden.
 *  @param  authOptions Authorized options,Used to authorization for custom configuration(Such as: whether the automatic authorization, authorization view style, etc.)Default nil.
 *  @param  shareOptions    Share options,Used to share for custom configuration(Such as: title, one key sharing, function buttons, etc.)Default nil.
 *  @param  result  Result handler.
 */
///#end
[ShareSDK showShareActionSheet:nil(container)
                     shareList:nil

                     content:publishContent

                     statusBarTips:YES

                     authOptions:nil (authOptions)

                     shareOptions: nil (shareOptions)

                     result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOLend) {

                                               if (state == SSResponseStateSuccess)

                                               {

                                                       NSLog(@"分享成功");

                                               }

                                               else if (state == SSResponseStateFail)

                                               {

                                                       NSLog(@"分享失败,错误码:%d,错误描述:%@"[error errorCode][error errorDescription]);

                                               }

                            }]
 
 

第四步:实现评论功能

原文地址:https://www.cnblogs.com/xubojoy/p/3885932.html