iOS ShareSDK 分享到新浪微博

来源:互联网 发布:mac最安全ntfs 编辑:程序博客网 时间:2024/03/29 05:06

第一步:在targets->info->url types中添加一项,命名为wb+appid(到官网开放平台去申请)

第二步:写一个分享功能类

// 省略头文件@interface HYBShareSDKHelper : NSObject+ (void)registerShareSDK;+ (BOOL)handleOpenURL:(NSURL *)url;+ (BOOL)handleOpenURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation;// 调用此方法来分享信息typedef void (^HYBShareCompletion)(BOOL successful);+ (void)shareWithContent:(NSString *)content            toController:(UIViewController *)controller                pngImage:(UIImage *)pngImage                   title:(NSString *)title                     url:(NSString *)url               mediaType:(SSPublishContentMediaType)mediaType       shareViewDelegate:(id<ISSShareViewDelegate>)shareViewDelegate              completion:(HYBShareCompletion)completion;@end

外部调用上面封装的方法来实现功能

////  HYBShareSDKHelper.m//  CustomSharedSDKDemo//  #import "HYBShareSDKHelper.h"#import "HYBAppCommonInfoTool.h"#import "HYBShareView.h"#define kShareSDKAppKey @""#define kShareSDKAppSecret @""#define kSinaWeiboAppKey @""#define kSinaWeiboAppSecret @""@implementation HYBShareSDKHelper+ (void)registerShareSDK {  [ShareSDK registerApp:kShareSDKAppKey];    // 添加新浪微博应用  NSString *redirectUri = @"";  // 添加新浪微博应用  [ShareSDK connectSinaWeiboWithAppKey:kSinaWeiboAppKey                             appSecret:kSinaWeiboAppSecret                           redirectUri:redirectUri];  // 当使用新浪微博客户端分享的时候需要按照下面的方法来初始化新浪的平台  [ShareSDK  connectSinaWeiboWithAppKey:kSinaWeiboAppKey                              appSecret:kSinaWeiboAppSecret                            redirectUri:redirectUri                            weiboSDKCls:[WeiboSDK class]];  return;}+ (BOOL)handleOpenURL:(NSURL *)url {  return [ShareSDK handleOpenURL:url wxDelegate:self];}+ (BOOL)handleOpenURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {  return [ShareSDK handleOpenURL:url sourceApplication:sourceApplication annotation:annotation wxDelegate:self];}// 这里是自己定制的弹出分享UI+ (void)showShareViewInController:(UIViewController *)controller completion:(HYBShareClickBlock)completion {  HYBShareView *sv = [[HYBShareView alloc] initWithImages:@[@"sns_wx_icon", @"sns_wx_fr_icon", @"sns_qq_icon", @"sns_qzone_icon", @"sns_sina_icon"] titles:@[@"微信好友", @"微信朋友圈", @"QQ好友", @"QQ空间", @"新浪微博"] completion:^(NSUInteger index) {    if (completion) {      completion(index);    }  }];  [sv showInController:controller];}+ (void)shareWithContent:(NSString *)content            toController:(UIViewController *)controller                pngImage:(UIImage *)pngImage                   title:(NSString *)title                     url:(NSString *)url               mediaType:(SSPublishContentMediaType)mediaType       shareViewDelegate:(id<ISSShareViewDelegate>)shareViewDelegate              completion:(HYBShareCompletion)completion {  // 分享内容  id<ISSContent> sharedContent = [ShareSDK content:content                                    defaultContent:content                                             image:[ShareSDK pngImageWithImage:pngImage]                                             title: title                                               url:url                                       description:@"自己看着办"                                         mediaType:mediaType];      // 验证参数  id<ISSAuthOptions> authOptions = [ShareSDK authOptionsWithAutoAuth:YES                                                       allowCallback:YES                                                       authViewStyle:SSAuthViewStyleFullScreenPopup                                                        viewDelegate:nil                                             authManagerViewDelegate:nil];  // 显示分享列表  [self showShareViewInController:controller completion:^(NSUInteger index) {    if (index == 4) {// 新浪微博      [self shareToSinaWeiboWithContent:sharedContent authOptions:authOptions content:content pngImage:pngImage completion:^(BOOL successful) {        if (completion) {          completion(successful);        }      }];    }  }];}// 分享到Sina weibo+ (void)shareToSinaWeiboWithContent:(id<ISSContent>)sharedContent                        authOptions:(id<ISSAuthOptions>)authOptions                            content:(NSString *)content                           pngImage:(UIImage *)pngImage                         completion:(HYBShareCompletion)completion {  [sharedContent addSinaWeiboUnitWithContent:content                                       image:[ShareSDK pngImageWithImage:pngImage]];  // if haven authorized, then call   if (![ShareSDK hasAuthorizedWithType:ShareTypeSinaWeibo]) {    [ShareSDK authWithType:ShareTypeSinaWeibo options:authOptions result:^(SSAuthState state, id<ICMErrorInfo> error) {      if (state == SSAuthStateSuccess) {        id<ISSShareOptions> shareOptions = [ShareSDK simpleShareOptionsWithTitle:@"美容总监"                                                               shareViewDelegate:nil];        [ShareSDK clientShareContent:sharedContent                                type:ShareTypeSinaWeibo                         authOptions:authOptions                        shareOptions:shareOptions                       statusBarTips:YES                              result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {                                if (completion && end) {                                  DDLogVerbose(@"%@", error.errorDescription);                                  completion(state == SSPublishContentStateSuccess);                                }                              }];      }    }];  } else {// use client share to Sina App Client    id<ISSShareOptions> shareOptions = [ShareSDK simpleShareOptionsWithTitle:@"美容总监"                                                           shareViewDelegate:nil];    [ShareSDK clientShareContent:sharedContent                            type:ShareTypeSinaWeibo                     authOptions:authOptions                    shareOptions:shareOptions                   statusBarTips:YES                          result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {      if (completion && end) {        DDLogVerbose(@"%@", error.errorDescription);        completion(state == SSPublishContentStateSuccess);      }    }];  }}@end


2 1
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 孩子发烧38度4怎么办 宝宝发烧38度2怎么办 一到晚上就发热怎么办 身上起红疙瘩很痒怎么办 一周连续4天梦遗怎么办 怀孕了脸上长斑怎么办 大腿内侧有肥胖纹怎么办 大三阳出现肝掌怎么办 每天尿的次数多怎么办 脚上脱皮很痒怎么办 ems快递太慢急用怎么办 痘痘留下来的斑怎么办 痘痘留下的斑怎么办 脚有水泡很痒怎么办 怀孕8个月耻骨疼怎么办 白带清洁度4度怎么办 孕期白带清洁度3怎么办 交社保7年人死了怎么办 逆战画面不流畅怎么办 逆战画面卡顿怎么办 老公心里没你的怎么办 老公出轨了妻子到底应该怎么办 函调对方不复函怎么办 新车自己刮蹭了怎么办 新车被蹭了点漆怎么办 白色的车刮了漆怎么办 车蹭了一点漆怎么办 汽车被蹭了点漆怎么办 新车蹭了一点漆怎么办 手表金属刮花了怎么办 碰了别人车跑了怎么办 老赖拘留15天后怎么办 刮到别人车跑了怎么办 被班里人孤立了怎么办 游戏停服了玩家怎么办 u盘提示要格式化怎么办 微博账号冻结了怎么办 微博登录名空白怎么办 知道微博登录名怎么办 打球跳不起来了怎么办 拉杆箱把手坏了怎么办