2015-02-07 89 views
0

我想从我的iOS应用程序发送电子邮件。从iOS发送电子邮件sdk

这个想法是有一个类SendMessage,其中所有类用于从应用程序内发送电子邮件。这是NSObject一个子类,是MFMailComposeViewControllerDelegate

这是SendMessage函数类的样子

@implementation SendMessage 

- (void) sendEmailFromViewController : (UIViewController *)viewController withSubject : (NSString *) subject withRecipient : (NSString *)recipient withMessage : (NSString *)message withCompletionBlock : (void(^)(void))completionBlock withFailure : (void(^)(void))failure { 

    self.viewController = viewController; 

    if (![MFMailComposeViewController canSendMail]){ 
     if (failure) 
      failure(); 
    } 
    else { 
     MFMailComposeViewController *messageController = [[MFMailComposeViewController alloc] init]; 
     messageController.mailComposeDelegate = self.viewController; 
     [messageController setSubject:subject]; 
     [messageController setToRecipients:[NSArray arrayWithObject:recipient]]; 
     [messageController setMessageBody:message isHTML:NO]; 

     [self.viewController presentModalViewController:messageController animated:YES]; 

    } 
} 

-(void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error { 
    [self.viewController dismissViewControllerAnimated:YES completion:nil]; 
} 

@end 

我试图用调用类:

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (indexPath.row == 3) { 
     SendMessage *sendFeedback = [[SendMessage alloc] init]; 
     [sendFeedback sendEmailFromViewController:self withSubject:@"App Feedback" withRecipient:@"[email protected]" withMessage:@"This is app feedback" withCompletionBlock:nil withFailure:nil]; 
    } 
} 

的问题是,虽然我能够发送电子邮件,委托方法没有被调用。我如何解决它 ?

+0

组messageController.mailComposeDelegate到** **自不self.viewController; – Brain89 2015-02-07 06:44:13

+0

进行了更改。同样的问题 – 2015-02-07 06:46:32

回答

1

您的代表是UIViewController,因此在那里实施mailComposeController:didFinishWithResult:error委托方法。

+0

我只是将SendMessage更改为UIViewController的子类并设置了messageController.mailComposeDelegate = self – 2015-02-07 06:46:16

+0

您可以在具有'didSelectRowAtIndexPath'方法的视图控制器中实现该方法。你不必为它创建子类,并且将delegate设置为self将不起作用,因为'sendFeedback'是一个实例变量,将会被释放出作用域。 – gabbler 2015-02-07 06:49:31

+0

我将实例变量更改为属性。它现在有效。谢谢 – 2015-02-07 07:01:53

0

我认为问题在于你的对象sendFeedback在块的末尾被释放。

最好的解决方案是创建对象,不是用它

SendMessage.h文件添加

+ (instancetype) sharedManager; 

SendMessage.m文件添加

+ (instancetype) sharedManager{ 
    static SendMessage* instance = nil; 
    if(!instance){ 
     instance = [[SendMessage alloc] init]; 
    } 
    return instance; 
} 

比你可以使用你的单身人士任何地方E码

-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath { 
    if (indexPath.row == 3) { 
     SendMessage *sendFeedback = [SendMessage sharedManager]; 
     [sendFeedback sendEmailFromViewController:self withSubject:@"App Feedback" withRecipient:@"[email protected]" withMessage:@"This is app feedback" withCompletionBlock:nil withFailure:nil]; 
    } 
} 

另外messageController.mailComposeDelegate必须设置为