#import "ShareSDKTool.h"
#import "Reachability.h"
#define titleMsg @"手环"
#define contentMsg @"健康的智能手环"
#define defaultContentMsg @"defaultContentMsg"
#define fileNameMsg @"imageName"
#define mimeTypeMsg @"image/jpeg"
#define descriptionMsg @"description"
//#define mediaTypeMsg SSPublishContentMediaTypeNews 新闻形式
#define mediaTypeMsg SSPublishContentMediaTypeImage // 纯图片形式
#define SuccessMsg LOCALIZATION1(@"sharedSuccess", nil)
#define FailMsg LOCALIZATION1(@"sharedFail", nil)
@interface ShareSDKTool(Extension)
// 构造分享内容
+ (id<ISSContent>)shareSDKInitMsg:(NSData *)data webUrl:(NSString *)url;
// 授权判断和分享
+ (void)shareSDKDone:(ShareType) type content:(id<ISSContent>) publishContent dialog:(UIView *)view auth:(BOOL)authFlag;
// 分享微博
+ (void)shareContent:(ShareType) type content:(id<ISSContent>) publishContent dialog:(UIView *)view auth:(BOOL)authFlag;
@end
@implementation ShareSDKTool
static bool sinaFlag = false;
static NSString *contentStr = contentMsg;
static id<ISSAuthOptions> authOptions = nil;
#pragma mark 初始化分享数据
+ (id<ISSContent>)shareSDKInitMsg:(NSData *)data webUrl:(NSString *)url{
// 构造权限
id<ISSAuthOptions> auth = [ShareSDK authOptionsWithAutoAuth:YES
allowCallback:YES
authViewStyle:SSAuthViewStyleFullScreenPopup
viewDelegate:nil
authManagerViewDelegate:nil];
authOptions = auth;
//构造分享内容
if (sinaFlag) {
contentStr = [url stringByAppendingString:contentMsg];
}
id<ISSContent> publishContent = [ShareSDK content:contentStr
defaultContent:defaultContentMsg
image:[ShareSDK imageWithData:data fileName:fileNameMsg mimeType:mimeTypeMsg]
title:titleMsg
url:url
description:descriptionMsg
mediaType:mediaTypeMsg];
return publishContent;
}
#pragma mark 分享微信,微信朋友圈
/**
* 分享微信,微信朋友圈
*
* @param data 分享需要的图片数据
* @param url 分享点击链接
* @param flag 区分是微信还是微信朋友圈标识
* @param view 弹出回调的提示框view
*/
+ (void)shareSDKWeChat:(NSData *)data webUrl:(NSString *)url type:(BOOL)flag dialog:(UIView *)view{
bool installFlag = [self checkInstall:ShareTypeWeixiSession dialog:view];
if (!installFlag) {
return;
}
id<ISSContent> publishContent = [self shareSDKInitMsg:data webUrl:url];
if (flag) {
// 分享微信
[self shareSDKDone:ShareTypeWeixiSession content:publishContent dialog:view auth:false];
}else{
// 分享微信朋友圈
[self shareSDKDone:ShareTypeWeixiTimeline content:publishContent dialog:view auth:false];
}
}
#pragma mark 分享QQ,QQ空间
/**
* 分享QQ,QQ空间
*
* @param data 分享需要的图片数据
* @param url 分享点击链接
* @param flag 区分是QQ还是QQ空间标识
* @param view 弹出回调的提示框view
*/
+ (void)shareSDKQQ:(NSData *)data webUrl:(NSString *)url type:(BOOL)flag dialog:(UIView *)view{
// 分享QQ
bool installFlag = [self checkInstall:ShareTypeQQ dialog:view];
if (!installFlag) {
return;
}
id<ISSContent> publishContent = [self shareSDKInitMsg:data webUrl:url];
if (flag) {
[self shareSDKDone:ShareTypeQQ content:publishContent dialog:view auth:false];
}else{
/** 分享QQ空间
bool installFlag = [self checkInstall:ShareTypeQQ dialog:view];
if (!installFlag) { // 跳网页
UIWebView *webView = [[UIWebView alloc] init];
webView.frame = CGRectMake(0, 0, ScreenWidth,ScreenHeight);
NSURL *url = [NSURL URLWithString:@"wap://qq.com"];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
[view addSubview:webView];
}else{
[self shareSDKDone:ShareTypeQQSpace content:publishContent dialog:view auth:true];
}**/
[self shareSDKDone:ShareTypeQQSpace content:publishContent dialog:view auth:true];
}
}
#pragma mark 分享新浪微博
/**
* 分享新浪微博
*
* @param data 分享需要的图片数据
* @param url 分享点击链接
* @param view 弹出回调的提示框view
*/
+ (void)shareSDKSina:(NSData *)data webUrl:(NSString *)url dialog:(UIView *)view{
sinaFlag = true;
id<ISSContent> publishContent = [self shareSDKInitMsg:data webUrl:url];
[self shareSDKDone:ShareTypeSinaWeibo content:publishContent dialog:view auth:true];
}
#pragma mark 判断是否授权
/**
* 判断是否授权,如果不需要直接发送, 如果需要,先授权然后再分享。
*
* @param type 分享平台类型
* @param publishContent 分享内容
* @param view 弹出窗口提示View
* @param authFlag 是否授权标识
*/
+ (void)shareSDKDone:(ShareType) type content:(id<ISSContent>) publishContent dialog:(UIView *)view auth:(BOOL)authFlag{
if (!authFlag) {
[self shareContent:type content:publishContent dialog:view auth:authFlag];
}else{
if (!authFlag) {//没有授权
[ShareSDK authWithType:type options:authOptions result:^(SSAuthState state, id<ICMErrorInfo> error) {
if (state == SSAuthStateSuccess) { // 授权成功
[self shareContent:type content:publishContent dialog:view auth:authFlag];
}else if(state == SSAuthStateFail){
[error description];
}
}];
}else{
[self shareContent:type content:publishContent dialog:view auth:authFlag];
}
}
sinaFlag = false;
contentStr = contentMsg;
}
#pragma mark 分享微博
/**
* 分享微博
*
* @param type 分享平台类型
* @param publishContent 分享内容
* @param view 弹出窗口提示View
* @param authFlag 是否授权标识
*/
+ (void)shareContent:(ShareType) type content:(id<ISSContent>) publishContent dialog:(UIView *)view auth:(BOOL)authFlag{
if (authFlag) {
[ShareSDK clientShareContent:publishContent
type:type
authOptions:authOptions
shareOptions:nil
statusBarTips:YES
result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {
if (state == SSPublishContentStateSuccess){
[self showDialog:SuccessMsg dialog:view];
}else if (state == SSPublishContentStateFail){
[self showDialog:FailMsg dialog:view];
NSLog(NSLocalizedString(@"TEXT_SHARE_FAI", @"分享失败!"), [error errorCode], [error errorDescription]);
}
}];
}else{
[ShareSDK clientShareContent:publishContent
type:type
statusBarTips:YES
result:^(ShareType type, SSResponseState state, id<ISSPlatformShareInfo> statusInfo, id<ICMErrorInfo> error, BOOL end) {//返回事件
if (state == SSPublishContentStateSuccess){
[self showDialog:SuccessMsg dialog:view];
}else if (state == SSPublishContentStateFail){
[self showDialog:FailMsg dialog:view];
NSLog(NSLocalizedString(@"TEXT_SHARE_FAI", @"分享失败!"),
[error errorCode], [error errorDescription]);
}
}];
}
}
#pragma mark - 分享后,回调弹出提示框
+ (void)showDialog:(NSString*)text dialog:(UIView *)view {
MBProgressHUD *progress = [MBProgressHUD showHUDAddedTo:view animated:true];
progress.backgroundColor = [UIColor clearColor];
progress.labelText = text;
progress.dimBackground = NO;
progress.mode = MBProgressHUDModeText;
[progress hide:true afterDelay:2];
}
#pragma mark -- 检查是否要安装应用
/**
* <#Description#>
*
* @param type 分享平台
* @param view 弹出窗口View
*
* @return
*/
+(bool)checkInstall:(ShareType)type dialog:(UIView *)view{
id<ISSPlatformApp> app = [ShareSDK getClientWithType:type];
bool flag = [app isClientInstalled];
if (!flag) {
switch (type) {
case ShareTypeWeixiSession:{// 微信
[self showDialog:LOCALIZATION(@"weixinNotInstall") dialog:view];
}
break;
case ShareTypeQQ:{// QQ
[self showDialog:LOCALIZATION(@"QQNotInstall") dialog:view];
}
break;
default:
break;
}
}
return flag;
}
#pragma mark 分享常用平台列表
+(void)SheetbuttonIndex:(NSInteger)buttonIndex dialog:(UIView *)view webUrl:shareUrl{
bool flag = [self checkNetworkStateDialog:view];
if (!flag) {
return ;
}
//获取图像
UIGraphicsBeginImageContext(view.frame.size);
[view.layer renderInContext:UIGraphicsGetCurrentContext()];
UIImage *croppedImage = UIGraphicsGetImageFromCurrentImageContext();
NSData *data = UIImageJPEGRepresentation(croppedImage,0.5);
switch (buttonIndex) {
case 0:
break;
case 1://微信
{
[self shareSDKWeChat:data webUrl:shareUrl type:true dialog:view];
break;
}
case 2://微信朋友圈
{
[self shareSDKWeChat:data webUrl:shareUrl type:false dialog:view];
break;
}
break;
case 3://新浪微博
{
[self shareSDKSina:data webUrl:shareUrl dialog:view];
break;
}
case 4://QQ好友
{
[self shareSDKQQ:data webUrl:shareUrl type:true dialog:view];
break;
}
case 5://QQ空间
{
[self shareSDKQQ:data webUrl:shareUrl type:false dialog:view];
break;
}
default:
break;
}
}
#pragma mark 检测网络是否可用
+ (BOOL)checkNetworkStateDialog:(UIView *)view{
Reachability *wifi = [Reachability reachabilityForLocalWiFi];
Reachability *conn = [Reachability reachabilityForInternetConnection];
if ([wifi currentReachabilityStatus] != NotReachable) { // 有wifi
NSLog(@"有wifi");
return true;
} else if ([conn currentReachabilityStatus] != NotReachable) { // 没有使用wifi, 使用手机自带网络进行上网
NSLog(@"使用手机自带网络进行上网");
return true;
} else { // 没有网络
NSLog(@"没有网络");
[self showDialog:LOCALIZATION(@"netFaild") dialog:view];
return false;
}
}
@end
shareSDK 简单用法记录
最新推荐文章于 2020-04-02 16:30:54 发布