iOS ShareSDK 分享到新浪微博

来源:互联网 发布:巨杉数据库 解决方案 编辑:程序博客网 时间:2024/04/26 18:58

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

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

[objc] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. // 省略头文件  
  2.   
  3. @interface HYBShareSDKHelper : NSObject  
  4.   
  5. + (void)registerShareSDK;  
  6. + (BOOL)handleOpenURL:(NSURL *)url;  
  7. + (BOOL)handleOpenURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation;  
  8.   
  9. // 调用此方法来分享信息  
  10. typedef void (^HYBShareCompletion)(BOOL successful);  
  11. + (void)shareWithContent:(NSString *)content  
  12.             toController:(UIViewController *)controller  
  13.                 pngImage:(UIImage *)pngImage  
  14.                    title:(NSString *)title  
  15.                      url:(NSString *)url  
  16.                mediaType:(SSPublishContentMediaType)mediaType  
  17.        shareViewDelegate:(id<ISSShareViewDelegate>)shareViewDelegate  
  18.               completion:(HYBShareCompletion)completion;  
  19.   
  20. @end  

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

[objc] view plain copy
 print?在CODE上查看代码片派生到我的代码片
  1. //  
  2. //  HYBShareSDKHelper.m  
  3. //  CustomSharedSDKDemo  
  4. //    
  5. #import "HYBShareSDKHelper.h"  
  6. #import "HYBAppCommonInfoTool.h"  
  7. #import "HYBShareView.h"  
  8.   
  9. #define kShareSDKAppKey @""  
  10. #define kShareSDKAppSecret @""  
  11. #define kSinaWeiboAppKey @""  
  12. #define kSinaWeiboAppSecret @""  
  13.   
  14. @implementation HYBShareSDKHelper  
  15.   
  16. + (void)registerShareSDK {  
  17.   [ShareSDK registerApp:kShareSDKAppKey];  
  18.     
  19.   // 添加新浪微博应用  
  20.   NSString *redirectUri = @"";  
  21.   // 添加新浪微博应用  
  22.   [ShareSDK connectSinaWeiboWithAppKey:kSinaWeiboAppKey  
  23.                              appSecret:kSinaWeiboAppSecret  
  24.                            redirectUri:redirectUri];  
  25.   // 当使用新浪微博客户端分享的时候需要按照下面的方法来初始化新浪的平台  
  26.   [ShareSDK  connectSinaWeiboWithAppKey:kSinaWeiboAppKey  
  27.                               appSecret:kSinaWeiboAppSecret  
  28.                             redirectUri:redirectUri  
  29.                             weiboSDKCls:[WeiboSDK class]];  
  30.   return;  
  31. }  
  32.   
  33. + (BOOL)handleOpenURL:(NSURL *)url {  
  34.   return [ShareSDK handleOpenURL:url wxDelegate:self];  
  35. }  
  36.   
  37. + (BOOL)handleOpenURL:(NSURL *)url sourceApplication:(NSString *)sourceApplication annotation:(id)annotation {  
  38.   return [ShareSDK handleOpenURL:url sourceApplication:sourceApplication annotation:annotation wxDelegate:self];  
  39. }  
  40. // 这里是自己定制的弹出分享UI  
  41. + (void)showShareViewInController:(UIViewController *)controller completion:(HYBShareClickBlock)completion {  
  42.   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) {  
  43.     if (completion) {  
  44.       completion(index);  
  45.     }  
  46.   }];  
  47.   [sv showInController:controller];  
  48. }  
  49.   
  50. + (void)shareWithContent:(NSString *)content  
  51.             toController:(UIViewController *)controller  
  52.                 pngImage:(UIImage *)pngImage  
  53.                    title:(NSString *)title  
  54.                      url:(NSString *)url  
  55.                mediaType:(SSPublishContentMediaType)mediaType  
  56.        shareViewDelegate:(id<ISSShareViewDelegate>)shareViewDelegate  
  57.               completion:(HYBShareCompletion)completion {  
  58.   // 分享内容  
  59.   id<ISSContent> sharedContent = [ShareSDK content:content  
  60.                                     defaultContent:content  
  61.                                              image:[ShareSDK pngImageWithImage:pngImage]  
  62.                                              title: title  
  63.                                                url:url  
  64.                                        description:@"自己看着办"  
  65.                                          mediaType:mediaType];  
  66.     
  67.     
  68.   // 验证参数  
  69.   id<ISSAuthOptions> authOptions = [ShareSDK authOptionsWithAutoAuth:YES  
  70.                                                        allowCallback:YES  
  71.                                                        authViewStyle:SSAuthViewStyleFullScreenPopup  
  72.                                                         viewDelegate:nil  
  73.                                              authManagerViewDelegate:nil];  
  74.   // 显示分享列表  
  75.   [self showShareViewInController:controller completion:^(NSUInteger index) {  
  76.     if (index == 4) {// 新浪微博  
  77.       [self shareToSinaWeiboWithContent:sharedContent authOptions:authOptions content:content pngImage:pngImage completion:^(BOOL successful) {  
  78.         if (completion) {  
  79.           completion(successful);  
  80.         }  
  81.       }];  
  82.     }  
  83.   }];  
  84. }  
  85.   
  86.   
  87. // 分享到Sina weibo  
  88. + (void)shareToSinaWeiboWithContent:(id<ISSContent>)sharedContent  
  89.                         authOptions:(id<ISSAuthOptions>)authOptions  
  90.                             content:(NSString *)content  
  91.                            pngImage:(UIImage *)pngImage  
  92.                          completion:(HYBShareCompletion)completion {  
  93.   [sharedContent addSinaWeiboUnitWithContent:content  
  94.                                        image:[ShareSDK pngImageWithImage:pngImage]];  
  95.   // if haven authorized, then call   
  96.   if (![ShareSDK hasAuthorizedWithType:ShareTypeSinaWeibo]) {  
  97.     [ShareSDK authWithType:ShareTypeSinaWeibo options:authOptions result:^(SSAuthState state, id<ICMErrorInfo> error) {  
  98.       if (state == SSAuthStateSuccess) {  
  99.         id<ISSShareOptions> shareOptions = [ShareSDK simpleShareOptionsWithTitle:@"美容总监"  
  100.                                                                shareViewDelegate:nil];  
  101.         [ShareSDK clientShareContent:sharedContent  
  102.                                 type:ShareTypeSinaWeibo  
  103.                          authOptions:authOptions  
  104.                         shareOptions:shareOptions  
  105.                        statusBarTips:YES  
  106.                               result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {  
  107.                                 if (completion && end) {  
  108.                                   DDLogVerbose(@"%@", error.errorDescription);  
  109.                                   completion(state == SSPublishContentStateSuccess);  
  110.                                 }  
  111.                               }];  
  112.       }  
  113.     }];  
  114.   } else {// use client share to Sina App Client  
  115.     id<ISSShareOptions> shareOptions = [ShareSDK simpleShareOptionsWithTitle:@"美容总监"  
  116.                                                            shareViewDelegate:nil];  
  117.     [ShareSDK clientShareContent:sharedContent  
  118.                             type:ShareTypeSinaWeibo  
  119.                      authOptions:authOptions  
  120.                     shareOptions:shareOptions  
  121.                    statusBarTips:YES  
  122.                           result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {  
  123.       if (completion && end) {  
  124.         DDLogVerbose(@"%@", error.errorDescription);  
  125.         completion(state == SSPublishContentStateSuccess);  
  126.       }  
  127.     }];  
  128.   }  
  129. }  
  130.   
  131. @end  
0 0