使用UIViewController类别发送电子邮件

时间:2014-11-14 12:53:18

标签: ios objective-c uiviewcontroller

从UIViewController发送电子邮件效果很好 - 但是,如果我尝试将其放入某个类别,那么事情就不再适用了。感谢任何帮助!

这是来自我的UIViewController的调用:

#import "UIViewController+EmailSend.h"
[self sendMailTo:@"myemailadress@xyz.com" :@"this is my subject"];

这是UIViewController类别:

的UIViewController + EmailSend.h

#import <Foundation/Foundation.h>
#import <MessageUI/MessageUI.h>

@interface UIViewController (iKKEmailSend) <MFMailComposeViewControllerDelegate>

@property (strong, nonatomic) MFMailComposeViewController *mailer;

- (void) sendMailTo :(NSString *)recipient :(NSString*)subject;

@end

的UIViewController + EmailSend.m

#import "UIViewController+iKKEmailSend.h"

@implementation UIViewController (iKKEmailSend)

@dynamic mailer;

- (void) sendMailTo :(NSString *)recipient :(NSString*)subject {

if ([MFMailComposeViewController canSendMail]) {

    self.mailer = [[MFMailComposeViewController alloc] init];
    // here is where the exception happens - why ?????????????????????????????

    self.mailer.mailComposeDelegate = self;

    // setSubject
    [self.mailer setSubject:subject];

    // setToRecipients
    NSArray *toRecipients = [NSArray arrayWithObjects:recipient, nil];
    [self.mailer setToRecipients:toRecipients];

    // setCcRecipients
    // NSArray *toCCRecipients = [NSArray arrayWithObjects:recipient, nil];
    // [self.mailer setCcRecipients:toCCRecipients];

    // setBccRecipients:
    // NSArray *toBccRecipients = [NSArray arrayWithObjects:recipient, nil];
    // [self.mailer setBccRecipients:toBccRecipients];

    // setMessageBody:isHTML
    NSString *emailBody = @"";
    [self.mailer setMessageBody:emailBody isHTML:NO];

    // addAttachmentData:mimeType:fileName:
    // UIImage *myImage = [UIImage imageNamed:@"myfabolousimage.png"];
    // NSData *imageData = UIImagePNGRepresentation(myImage);
    // [self.mailer addAttachmentData:imageData
    //              mimeType:@"image/png" fileName:@"myfabolousimage.png"];

    [self presentViewController:self.mailer animated:YES completion:nil];
    }
    else {
        NSLog(@"This device cannot send email");
    }
}

- (void)mailComposeController:(MFMailComposeViewController *)controller
      didFinishWithResult:(MFMailComposeResult)result
                    error:(NSError *)error {
    if (error) {

    } else {
        [self.mailer dismissViewControllerAnimated:YES completion:nil];
    }
}

@end

3 个答案:

答案 0 :(得分:2)

您不需要MFMailComposeViewController属性。

只需进行类似的更改:

MFMailComposeViewController *mailComposeViewController = [[MFMailComposeViewController alloc] init];
[mailComposeViewController setMailComposeDelegate:self];

[self presentViewController:mailComposeViewController animated:YES completion:nil];

然后

[self dismissViewControllerAnimated:YES completion:nil];

答案 1 :(得分:2)

使用“Categories”的灵魂目的是将方法和方法实现添加到现有类,而不必具有源代码。现在想想如果你想在“NSString”类中添加一些新方法,你可以通过一个类别添加你的新方法,但你不能将iVars添加到“NSString”上课,这是违规行为。因此,它不允许您以这种方式使用消息视图控制器。

@property (strong, nonatomic) MFMailComposeViewController *mailer;

只需在视图控制器中声明此属性并在类别中编写实现,然后它就可以工作。

答案 2 :(得分:1)

您无法在类别中声明属性

但您可以使用ObjectiveC运行时来获取和设置关联对象到实例,
所以这个解决方案应该适合你。

#import "UIViewController+iKKEmailSend.h"

#import <objc/runtime.h>

@implementation UIViewController (iKKEmailSend)

- (void)setMailer:(MFMailComposeViewController *)aMailer
{
     objc_setAssociatedObject(self, "mailer", aMailer, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (MFMailComposeViewController *)mailer
{
     return objc_getAssociatedObject(self, "mailer");
}

- (void) sendMailTo :(NSString *)recipient :(NSString*)subject {

if ([MFMailComposeViewController canSendMail]) {

    self.mailer = [[MFMailComposeViewController alloc] init];
    // here is where the exception happens - why ?????????????????????????????

    self.mailer.mailComposeDelegate = self;

    // setSubject
    [self.mailer setSubject:subject];

    // setToRecipients
    NSArray *toRecipients = [NSArray arrayWithObjects:recipient, nil];
    [self.mailer setToRecipients:toRecipients];

    // setCcRecipients
    // NSArray *toCCRecipients = [NSArray arrayWithObjects:recipient, nil];
    // [self.mailer setCcRecipients:toCCRecipients];

    // setBccRecipients:
    // NSArray *toBccRecipients = [NSArray arrayWithObjects:recipient, nil];
    // [self.mailer setBccRecipients:toBccRecipients];

    // setMessageBody:isHTML
    NSString *emailBody = @"";
    [self.mailer setMessageBody:emailBody isHTML:NO];

    // addAttachmentData:mimeType:fileName:
    // UIImage *myImage = [UIImage imageNamed:@"myfabolousimage.png"];
    // NSData *imageData = UIImagePNGRepresentation(myImage);
    // [self.mailer addAttachmentData:imageData
    //              mimeType:@"image/png" fileName:@"myfabolousimage.png"];

    [self presentViewController:self.mailer animated:YES completion:nil];
    }
    else {
        NSLog(@"This device cannot send email");
    }
}

- (void)mailComposeController:(MFMailComposeViewController *)controller
      didFinishWithResult:(MFMailComposeResult)result
                    error:(NSError *)error {
    if (error) {

    } else {
        [self.mailer dismissViewControllerAnimated:YES completion:nil];
    }
}

@end