1

我做了一个服装 UIActivity 类。我喜欢在课堂上发送带有附件的电子邮件,但我无法发送电子邮件,也无法展示课堂上的任何 ViewController。我正在尝试向 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?

4

1 回答 1

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
于 2014-03-17T17:41:13.953 回答