第一步:在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