How can I send emails from my custom UIActivity class?

拈花ヽ惹草 提交于 2019-12-11 10:39:54

问题


I made a costume UIActivity class. I like to send emails with attachments with the class, but I can't send emails, or present any ViewControllers from the class. I am trying to present the Mail ViewController with this:

- (void)prepareWithActivityItems:(NSArray *)activityItems
{
    NSString *subject = [NSString stringWithFormat:@"%@", [self.filePath lastPathComponent]];
    NSString *messageBody = [NSString stringWithFormat:@"%@ was extracted with @FilyForiOS, visit", [self.filePath lastPathComponent]];
        NSData *attachment = [NSData dataWithContentsOfFile:self.filePath];

    MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
    mc.mailComposeDelegate = self;
    [mc setSubject:subject];
    [mc setMessageBody:messageBody isHTML:NO];
    [mc setToRecipients:nil];
    [mc addAttachmentData:attachment mimeType:[[self.filePath lastPathComponent] pathExtension] fileName:[self.filePath lastPathComponent]];
    [self presentViewController:mc animated:YES completion:nil]; // Here is the error: No visible @interface for 'MailTo' declares the selector 'presentViewController:animated:completion:'
}

Can anyone tell me how I can present a ViewController from this class?

I used this code: How can I create a custom UIActivity in iOS?


回答1:


Sorry that I'm just now answering this (I know this was asked along time ago) But here is my answer:

#import <MessageUI/MessageUI.h>

@interface EmailActivity : UIActivity <MFMailComposeViewControllerDelegate>

@end

@implementation EmailActivity

- (NSString *)activityTitle
{
    // Your title
    return @"Email";
}

- (UIImage *)activityImage
{
    // Your image
    return [UIImage imageNamed:@"Email"];
}

- (BOOL)canPerformWithActivityItems:(NSArray *)activityItems
{
    return YES;
}

- (void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
    [self activityDidFinish:YES];
}

- (UIViewController *)activityViewController
{
    MFMailComposeViewController *mc = [[MFMailComposeViewController alloc]init];

    if ([MFMailComposeViewController canSendMail])
    {
        NSString *subject = [NSString stringWithFormat:@"%@", [self.filePath lastPathComponent]];
        NSString *messageBody = [NSString stringWithFormat:@"%@ was extracted with @FilyForiOS, visit", [self.filePath lastPathComponent]];
        NSData *attachment = [NSData dataWithContentsOfFile:self.filePath];

        [mc setSubject:subject];
        [mc setMessageBody:messageBody isHTML:NO];
        [mc setToRecipients:nil];
        [mc addAttachmentData:attachment mimeType:[[self.filePath lastPathComponent] pathExtension] fileName:[self.filePath lastPathComponent]];

        mc.mailComposeDelegate = self;

        return mc;
    }
    else
    {
        return nil;
    }
}

@end


来源:https://stackoverflow.com/questions/18810822/how-can-i-send-emails-from-my-custom-uiactivity-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!