123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149 |
- //
- // KSUMShareManager.m
- // KulexiuForTeacher
- //
- // Created by 王智 on 2022/6/14.
- //
- #import "KSUMShareManager.h"
- #import <UMShare/UMShare.h>
- #import <UShareUI/UShareUI.h>
- #import "TZImageManager.h"
- #import "KSShareGroupViewController.h"
- @interface KSUMShareManager ()
- @property (nonatomic, copy) KSShareActionCallback callback;
- @property (nonatomic, strong) NSString *shareTitle;
- @property (nonatomic, strong) NSString *shareMessage;
- @property (nonatomic, strong) NSString *shareUrl;
- @property (nonatomic, strong) UIImage *shareImage;
- @property (nonatomic, strong) UIViewController *displayCtrl;
- @property (nonatomic, assign) KSSHARETYPE shareType;
- @end
- @implementation KSUMShareManager
- + (instancetype)shareInstance {
- KSUMShareManager *manager = [[self alloc] init];
- return manager;
- }
- + (instancetype)shareInstanceWithImage:(UIImage *)image url:(NSString *)url shareTitle:(NSString *)shareTitle descMessage:(NSString *)descMessage shareType:(KSSHARETYPE)type showInView:(nonnull UIViewController *)ctrl callback:(KSShareActionCallback)callback {
- KSUMShareManager *manager = [[self alloc] init];
- manager.shareType = type;
- manager.shareImage = image;
- manager.shareUrl = url;
- manager.shareTitle = shareTitle;
- manager.shareMessage = descMessage;
- manager.displayCtrl = ctrl;
- if (callback) {
- manager.callback = callback;
- }
- [manager openShareView];
- return manager;
- }
- - (void)openShareView {
- [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
- if (platformType == UMSocialPlatformType_UserDefine_Begin+1) { // 分享到群组
- if (self.shareType == KSSHARETYPE_IMAGE) {
- KSShareGroupViewController *shareGroupCtrl = [[KSShareGroupViewController alloc] init];
- shareGroupCtrl.shareImage = self.shareImage;
- MJWeakSelf;
- [shareGroupCtrl shareGroupCallback:^(BOOL isSuccess, NSString *descMsg) {
- __strong typeof(weakSelf) strongSelf = weakSelf;
- if (strongSelf.callback) {
- strongSelf.callback(isSuccess,descMsg);
- }
- }];
- [self.displayCtrl.navigationController pushViewController:shareGroupCtrl animated:YES];
- }
- else {
- if (self.callback) {
- self.callback(NO,@"仅支持图片分享到群组");
- }
- }
-
- }
- else if (platformType == UMSocialPlatformType_UserDefine_Begin+2) { // 图片保存到相册
- if (self.shareType == KSSHARETYPE_IMAGE) {
- // 判断相册权限
-
- [[TZImageManager manager] savePhotoWithImage:self.shareImage completion:^(PHAsset *asset, NSError *error) {
- if (!error) {
- if (self.callback) {
- self.callback(YES,@"保存成功");
- }
- }
- else {
- if (self.callback) {
- self.callback(NO,@"保存失败");
- }
- }
- }];
- }
- else {
- if (self.callback) {
- self.callback(NO,@"仅支持图片保存");
- }
- }
- }
- else {
- //创建分享消息对象
- UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
- NSString *shareTitle = @"";
- if (![NSString isEmptyString:self.shareTitle]) {
- shareTitle = self.shareTitle;
- }
- NSString *descMessage = @"";
- if (self.shareMessage) {
- descMessage = self.shareMessage;
- }
- // 创建分享对象
- if (self.shareType == KSSHARETYPE_IMAGE) {
- UMShareImageObject *shareObj = [UMShareImageObject shareObjectWithTitle:shareTitle descr:descMessage thumImage:[UIImage imageNamed:@"shareImage"]];
- shareObj.shareImage = self.shareImage;
- //分享消息对象设置分享内容对象
- messageObject.shareObject = shareObj;
- }
- else if (self.shareType == KSSHARETYPE_VODEO) {
- UMShareWebpageObject *shareObj = [UMShareWebpageObject shareObjectWithTitle:shareTitle descr:descMessage thumImage:[UIImage imageNamed:@"shareImage"]];
- // NSString *shareUrl = [self.shareUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
- shareObj.webpageUrl = self.shareUrl;
- //分享消息对象设置分享内容对象
- messageObject.shareObject = shareObj;
- }
- //调用分享接口
- [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:nil completion:^(id data, NSError *error) {
- if (error) {
- NSLog(@"************分享失败 %@*********",error);
- if (self.callback) {
- self.callback(NO, @"分享失败");
- }
- }else{
- if ([data isKindOfClass:[UMSocialShareResponse class]]) {
- UMSocialShareResponse *resp = data;
- //分享结果消息
- NSLog(@"************分享成功 %@*********",resp.message);
- if (self.callback) {
- self.callback(YES,@"分享成功");
- }
- }else{
- NSLog(@"response data is %@",data);
- }
- }
- }];
- }
-
- }];
- }
- @end
|