| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189 |
- //
- // LPShareViewController.m
- // ShareDemo
- //
- // Created by Jack on 2018/3/5.
- // Copyright © 2018年 Jack. All rights reserved.
- //
- #import "LPShareActivity.h"
- #import <MessageUI/MessageUI.h>
- NSString *const LPActivityType = @"com.emerys.www.share.activity";
- @interface LPShareActivity ()
- @end
- @implementation LPShareActivity
- /**
- * 标示自定义服务的字符串
- */
- - (NSString *)activityType {
-
- // 这里就迎合苹果官方的命名
- return LPActivityType;
- }
- /**
- * 服务的显示内容
- */
- - (NSString *)activityTitle {
- return @"Email";
- }
- /**
- * 服务显示图标
- */
- - (UIImage *)activityImage {
-
- // Note: These images need to have a transparent background and I recommend these sizes:
- // iPadShare@2x should be 126 px, iPadShare should be 53 px, iPhoneShare@2x should be 100
- // px, and iPhoneShare should be 50 px. I found these sizes to work for what I was making.
-
- // if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad)
- // {
- // return [UIImage imageNamed:@"iPadShare.png"];
- // }
- // else
- // {
- // return [UIImage imageNamed:@"iPhoneShare.png"];
- // }
- return [UIImage imageNamed:@"ic_email"];
- }
- /**
- * 指定空运处理的数据类型,如果可以则返回YES
- * 当用户选择展示UIActivityViewController的自定义服务时,在调用相应处理方法之前的准备工作都在该方法中执行
- */
- - (BOOL)canPerformWithActivityItems:(NSArray *)activityItems {
- return YES;
- }
- /**
- * UIActivityCategoryAction:表示在UIActivityViewController最下面一栏的操作型服务
- * UIActivityCategoryShare:表示在UIActivityViewController中间一栏的分享型服务(社交分享)
- */
- + (UIActivityCategory)activityCategory {
- return UIActivityCategoryShare;
- }
- - (UIViewController *)activityViewController
- {
- // return nil;
- NSLog(@"选中了自定义服务");
-
- MFMailComposeViewController *mailComposeViewController = [[MFMailComposeViewController alloc] init];
-
- [mailComposeViewController setToRecipients:self.to];
- [mailComposeViewController setSubject:self.subject];
- [mailComposeViewController setMessageBody:[NSString stringWithFormat:@"<html><p>%@</p></html>",self.body] isHTML:YES];
-
- if (self.filePath.length) {
- NSData *data = [NSData dataWithContentsOfFile:self.filePath];
- NSString *fileName = [[self.filePath stringByDeletingPathExtension] lastPathComponent];
- [mailComposeViewController addAttachmentData:data mimeType:@"application/pdf" fileName:fileName];
- }
-
- mailComposeViewController.mailComposeDelegate = self;
- // [self activityDidFinish:YES];
- return mailComposeViewController;
- // if (_handle) {
- // _handle(mailComposeViewController);
- // }
- }
- #pragma mark -MFMailComposeViewControllerDelegate delegate
- - (void)mailComposeController:(MFMailComposeViewController*)controller
- didFinishWithResult:(MFMailComposeResult)result
- error:(NSError*)error {
- switch (result)
- {
- case MFMailComposeResultCancelled:
- NSLog(@"Mail send canceled...");
- break;
- case MFMailComposeResultSaved:
- NSLog(@"Mail saved...");
- break;
- case MFMailComposeResultSent:
- NSLog(@"Mail sent...");
- break;
- case MFMailComposeResultFailed:
- NSLog(@"Mail send errored: %@...", [error localizedDescription]);
- break;
- default:
- break;
- }
- // [self dismissModalViewControllerAnimated:YES];
- [self activityDidFinish:YES];
- }
- /**
- * 点击自定义服务之后的操作
- */
- - (void)performActivity {
- //
- // MFMailComposeViewController *mailComposer = [MFMailComposeViewController new];
- //
- // [mailComposer setToRecipients:self.to];
- // [mailComposer setSubject:self.subject];
- // [mailComposer setMessageBody:[NSString stringWithFormat:@"<html><p>%@</p></html>",self.body] isHTML:YES];
- //
- // if (self.filePath.length) {
- // NSData *data = [NSData dataWithContentsOfFile:self.filePath];
- // NSString *fileName = [[self.filePath stringByDeletingPathExtension] lastPathComponent];
- // [mailComposer addAttachmentData:data mimeType:@"application/pdf" fileName:fileName];
- // }
- //
- // mailComposer.modalTransitionStyle = UIModalTransitionStyleCoverVertical;
- //
- // mailComposer.modalPresentationStyle = UIModalPresentationFormSheet;
- //
- // AppDelegate *delagate =(AppDelegate *) [[UIApplication sharedApplication]delegate];
- //
- // mailComposer.mailComposeDelegate = [[(UINavigationController *)delagate.window.rootViewController viewControllers] lastObject]; // Set the delegate
- //
- //
- // [(UIViewController *)[[(UINavigationController *)delagate.window.rootViewController viewControllers] lastObject] presentViewController:mailComposer animated:YES completion:nil];
- //
- //// NSLog(@"选中了自定义服务");
- ////
- //// MFMailComposeViewController *mailComposeViewController = [[MFMailComposeViewController alloc] init];
- ////
- //// [mailComposeViewController setToRecipients:self.to];
- //// [mailComposeViewController setSubject:self.subject];
- //// [mailComposeViewController setMessageBody:[NSString stringWithFormat:@"<html><p>%@</p></html>",self.body] isHTML:YES];
- ////
- //// if (self.filePath.length) {
- //// NSData *data = [NSData dataWithContentsOfFile:self.filePath];
- //// NSString *fileName = [[self.filePath stringByDeletingPathExtension] lastPathComponent];
- //// [mailComposeViewController addAttachmentData:data mimeType:@"application/pdf" fileName:fileName];
- //// }
- // [self activityDidFinish:YES];
- ////// return mailComposeViewController;
- //// if (_handle) {
- //// _handle(mailComposeViewController);
- //// }
- }
- - (void)setTo:(NSArray *)to {
- _to = to;
- }
- - (void)setSubject:(NSString *)subject {
- if (subject == nil) {
- subject = @"";
- }
- _subject = subject;
- }
- - (void)setBody:(NSString *)body {
- if (body == nil) {
- body = @"";
- }
- _body = body;
- }
- @end
|