LPShareActivity.m 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. //
  2. // LPShareViewController.m
  3. // ShareDemo
  4. //
  5. // Created by Jack on 2018/3/5.
  6. // Copyright © 2018年 Jack. All rights reserved.
  7. //
  8. #import "LPShareActivity.h"
  9. #import <MessageUI/MessageUI.h>
  10. NSString *const LPActivityType = @"com.emerys.www.share.activity";
  11. @interface LPShareActivity ()
  12. @end
  13. @implementation LPShareActivity
  14. /**
  15. * 标示自定义服务的字符串
  16. */
  17. - (NSString *)activityType {
  18. // 这里就迎合苹果官方的命名
  19. return LPActivityType;
  20. }
  21. /**
  22. * 服务的显示内容
  23. */
  24. - (NSString *)activityTitle {
  25. return @"Email";
  26. }
  27. /**
  28. * 服务显示图标
  29. */
  30. - (UIImage *)activityImage {
  31. // Note: These images need to have a transparent background and I recommend these sizes:
  32. // iPadShare@2x should be 126 px, iPadShare should be 53 px, iPhoneShare@2x should be 100
  33. // px, and iPhoneShare should be 50 px. I found these sizes to work for what I was making.
  34. // if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
  35. // {
  36. // return [UIImage imageNamed:@"iPadShare.png"];
  37. // }
  38. // else
  39. // {
  40. // return [UIImage imageNamed:@"iPhoneShare.png"];
  41. // }
  42. return [UIImage imageNamed:@"ic_email"];
  43. }
  44. /**
  45. * 指定空运处理的数据类型,如果可以则返回YES
  46. * 当用户选择展示UIActivityViewController的自定义服务时,在调用相应处理方法之前的准备工作都在该方法中执行
  47. */
  48. - (BOOL)canPerformWithActivityItems:(NSArray *)activityItems {
  49. return YES;
  50. }
  51. /**
  52. * UIActivityCategoryAction:表示在UIActivityViewController最下面一栏的操作型服务
  53. * UIActivityCategoryShare:表示在UIActivityViewController中间一栏的分享型服务(社交分享)
  54. */
  55. + (UIActivityCategory)activityCategory {
  56. return UIActivityCategoryShare;
  57. }
  58. - (UIViewController *)activityViewController
  59. {
  60. // return nil;
  61. NSLog(@"选中了自定义服务");
  62. MFMailComposeViewController *mailComposeViewController = [[MFMailComposeViewController alloc] init];
  63. [mailComposeViewController setToRecipients:self.to];
  64. [mailComposeViewController setSubject:self.subject];
  65. [mailComposeViewController setMessageBody:[NSString stringWithFormat:@"<html><p>%@</p></html>",self.body] isHTML:YES];
  66. if (self.filePath.length) {
  67. NSData *data = [NSData dataWithContentsOfFile:self.filePath];
  68. NSString *fileName = [[self.filePath stringByDeletingPathExtension] lastPathComponent];
  69. [mailComposeViewController addAttachmentData:data mimeType:@"application/pdf" fileName:fileName];
  70. }
  71. mailComposeViewController.mailComposeDelegate = self;
  72. // [self activityDidFinish:YES];
  73. return mailComposeViewController;
  74. // if (_handle) {
  75. // _handle(mailComposeViewController);
  76. // }
  77. }
  78. #pragma mark -MFMailComposeViewControllerDelegate delegate
  79. - (void)mailComposeController:(MFMailComposeViewController*)controller
  80. didFinishWithResult:(MFMailComposeResult)result
  81. error:(NSError*)error {
  82. switch (result)
  83. {
  84. case MFMailComposeResultCancelled:
  85. NSLog(@"Mail send canceled...");
  86. break;
  87. case MFMailComposeResultSaved:
  88. NSLog(@"Mail saved...");
  89. break;
  90. case MFMailComposeResultSent:
  91. NSLog(@"Mail sent...");
  92. break;
  93. case MFMailComposeResultFailed:
  94. NSLog(@"Mail send errored: %@...", [error localizedDescription]);
  95. break;
  96. default:
  97. break;
  98. }
  99. // [self dismissModalViewControllerAnimated:YES];
  100. [self activityDidFinish:YES];
  101. }
  102. /**
  103. * 点击自定义服务之后的操作
  104. */
  105. - (void)performActivity {
  106. //
  107. // MFMailComposeViewController *mailComposer = [MFMailComposeViewController new];
  108. //
  109. // [mailComposer setToRecipients:self.to];
  110. // [mailComposer setSubject:self.subject];
  111. // [mailComposer setMessageBody:[NSString stringWithFormat:@"<html><p>%@</p></html>",self.body] isHTML:YES];
  112. //
  113. // if (self.filePath.length) {
  114. // NSData *data = [NSData dataWithContentsOfFile:self.filePath];
  115. // NSString *fileName = [[self.filePath stringByDeletingPathExtension] lastPathComponent];
  116. // [mailComposer addAttachmentData:data mimeType:@"application/pdf" fileName:fileName];
  117. // }
  118. //
  119. // mailComposer.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
  120. //
  121. // mailComposer.modalPresentationStyle = UIModalPresentationFormSheet;
  122. //
  123. // AppDelegate *delagate =(AppDelegate *) [[UIApplication sharedApplication]delegate];
  124. //
  125. // mailComposer.mailComposeDelegate = [[(UINavigationController *)delagate.window.rootViewController viewControllers] lastObject]; // Set the delegate
  126. //
  127. //
  128. // [(UIViewController *)[[(UINavigationController *)delagate.window.rootViewController viewControllers] lastObject] presentViewController:mailComposer animated:YES completion:nil];
  129. //
  130. //// NSLog(@"选中了自定义服务");
  131. ////
  132. //// MFMailComposeViewController *mailComposeViewController = [[MFMailComposeViewController alloc] init];
  133. ////
  134. //// [mailComposeViewController setToRecipients:self.to];
  135. //// [mailComposeViewController setSubject:self.subject];
  136. //// [mailComposeViewController setMessageBody:[NSString stringWithFormat:@"<html><p>%@</p></html>",self.body] isHTML:YES];
  137. ////
  138. //// if (self.filePath.length) {
  139. //// NSData *data = [NSData dataWithContentsOfFile:self.filePath];
  140. //// NSString *fileName = [[self.filePath stringByDeletingPathExtension] lastPathComponent];
  141. //// [mailComposeViewController addAttachmentData:data mimeType:@"application/pdf" fileName:fileName];
  142. //// }
  143. // [self activityDidFinish:YES];
  144. ////// return mailComposeViewController;
  145. //// if (_handle) {
  146. //// _handle(mailComposeViewController);
  147. //// }
  148. }
  149. - (void)setTo:(NSArray *)to {
  150. _to = to;
  151. }
  152. - (void)setSubject:(NSString *)subject {
  153. if (subject == nil) {
  154. subject = @"";
  155. }
  156. _subject = subject;
  157. }
  158. - (void)setBody:(NSString *)body {
  159. if (body == nil) {
  160. body = @"";
  161. }
  162. _body = body;
  163. }
  164. @end