// // LPShareViewController.m // ShareDemo // // Created by Jack on 2018/3/5. // Copyright © 2018年 Jack. All rights reserved. // #import "LPShareActivity.h" #import 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:@"

%@

",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:@"

%@

",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:@"

%@

",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