如何从我的自定义UIActivity类发送电子邮件?

时间:2013-09-15 09:23:19

标签: ios objective-c mfmailcomposeviewcontroller uiactivityviewcontroller uiactivity

我做了一个服装UIActivity课程。我喜欢在课堂上发送带附件的电子邮件,但我不能发送电子邮件,也不能在课堂上展示任何ViewControllers。我试图用这个来呈现Mail ViewController:

- (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:'
}

有谁能告诉我如何从这个类中呈现一个ViewController?

我使用了这段代码:How can I create a custom UIActivity in iOS?

1 个答案:

答案 0 :(得分:1)

很抱歉我刚刚回答这个问题(我知道这是在不久前问的)但是这是我的回答:

#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