KSUMShareManager.m 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. //
  2. // KSUMShareManager.m
  3. // KulexiuForTeacher
  4. //
  5. // Created by 王智 on 2022/6/14.
  6. //
  7. #import "KSUMShareManager.h"
  8. #import <UMShare/UMShare.h>
  9. #import <UShareUI/UShareUI.h>
  10. #import "TZImageManager.h"
  11. #import "KSShareGroupViewController.h"
  12. @interface KSUMShareManager ()
  13. @property (nonatomic, copy) KSShareActionCallback callback;
  14. @property (nonatomic, strong) NSString *shareTitle;
  15. @property (nonatomic, strong) NSString *shareMessage;
  16. @property (nonatomic, strong) NSString *shareUrl;
  17. @property (nonatomic, strong) UIImage *shareImage;
  18. @property (nonatomic, strong) UIViewController *displayCtrl;
  19. @property (nonatomic, assign) KSSHARETYPE shareType;
  20. @end
  21. @implementation KSUMShareManager
  22. + (instancetype)shareInstance {
  23. KSUMShareManager *manager = [[self alloc] init];
  24. return manager;
  25. }
  26. + (instancetype)shareInstanceWithImage:(UIImage *)image url:(NSString *)url shareTitle:(NSString *)shareTitle descMessage:(NSString *)descMessage shareType:(KSSHARETYPE)type showInView:(nonnull UIViewController *)ctrl callback:(KSShareActionCallback)callback {
  27. KSUMShareManager *manager = [[self alloc] init];
  28. manager.shareType = type;
  29. manager.shareImage = image;
  30. manager.shareUrl = url;
  31. manager.shareTitle = shareTitle;
  32. manager.shareMessage = descMessage;
  33. manager.displayCtrl = ctrl;
  34. if (callback) {
  35. manager.callback = callback;
  36. }
  37. [manager openShareView];
  38. return manager;
  39. }
  40. - (void)openShareView {
  41. [UMSocialUIManager showShareMenuViewInWindowWithPlatformSelectionBlock:^(UMSocialPlatformType platformType, NSDictionary *userInfo) {
  42. if (platformType == UMSocialPlatformType_UserDefine_Begin+1) { // 分享到群组
  43. if (self.shareType == KSSHARETYPE_IMAGE) {
  44. KSShareGroupViewController *shareGroupCtrl = [[KSShareGroupViewController alloc] init];
  45. shareGroupCtrl.shareImage = self.shareImage;
  46. MJWeakSelf;
  47. [shareGroupCtrl shareGroupCallback:^(BOOL isSuccess, NSString *descMsg) {
  48. __strong typeof(weakSelf) strongSelf = weakSelf;
  49. if (strongSelf.callback) {
  50. strongSelf.callback(isSuccess,descMsg);
  51. }
  52. }];
  53. [self.displayCtrl.navigationController pushViewController:shareGroupCtrl animated:YES];
  54. }
  55. else {
  56. if (self.callback) {
  57. self.callback(NO,@"仅支持图片分享到群组");
  58. }
  59. }
  60. }
  61. else if (platformType == UMSocialPlatformType_UserDefine_Begin+2) { // 图片保存到相册
  62. if (self.shareType == KSSHARETYPE_IMAGE) {
  63. // 判断相册权限
  64. [[TZImageManager manager] savePhotoWithImage:self.shareImage completion:^(PHAsset *asset, NSError *error) {
  65. if (!error) {
  66. if (self.callback) {
  67. self.callback(YES,@"保存成功");
  68. }
  69. }
  70. else {
  71. if (self.callback) {
  72. self.callback(NO,@"保存失败");
  73. }
  74. }
  75. }];
  76. }
  77. else {
  78. if (self.callback) {
  79. self.callback(NO,@"仅支持图片保存");
  80. }
  81. }
  82. }
  83. else {
  84. //创建分享消息对象
  85. UMSocialMessageObject *messageObject = [UMSocialMessageObject messageObject];
  86. NSString *shareTitle = @"";
  87. if (![NSString isEmptyString:self.shareTitle]) {
  88. shareTitle = self.shareTitle;
  89. }
  90. NSString *descMessage = @"";
  91. if (self.shareMessage) {
  92. descMessage = self.shareMessage;
  93. }
  94. // 创建分享对象
  95. if (self.shareType == KSSHARETYPE_IMAGE) {
  96. UMShareImageObject *shareObj = [UMShareImageObject shareObjectWithTitle:shareTitle descr:descMessage thumImage:[UIImage imageNamed:@"shareImage"]];
  97. shareObj.shareImage = self.shareImage;
  98. //分享消息对象设置分享内容对象
  99. messageObject.shareObject = shareObj;
  100. }
  101. else if (self.shareType == KSSHARETYPE_VODEO) {
  102. UMShareWebpageObject *shareObj = [UMShareWebpageObject shareObjectWithTitle:shareTitle descr:descMessage thumImage:[UIImage imageNamed:@"shareImage"]];
  103. // NSString *shareUrl = [self.shareUrl stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
  104. shareObj.webpageUrl = self.shareUrl;
  105. //分享消息对象设置分享内容对象
  106. messageObject.shareObject = shareObj;
  107. }
  108. //调用分享接口
  109. [[UMSocialManager defaultManager] shareToPlatform:platformType messageObject:messageObject currentViewController:nil completion:^(id data, NSError *error) {
  110. if (error) {
  111. NSLog(@"************分享失败 %@*********",error);
  112. if (self.callback) {
  113. self.callback(NO, @"分享失败");
  114. }
  115. }else{
  116. if ([data isKindOfClass:[UMSocialShareResponse class]]) {
  117. UMSocialShareResponse *resp = data;
  118. //分享结果消息
  119. NSLog(@"************分享成功 %@*********",resp.message);
  120. if (self.callback) {
  121. self.callback(YES,@"分享成功");
  122. }
  123. }else{
  124. NSLog(@"response data is %@",data);
  125. }
  126. }
  127. }];
  128. }
  129. }];
  130. }
  131. @end