[英]Sending email from the iOS sdk
我正在尝试从iOS应用程序中发送电子邮件。
想法是拥有一个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:@"ashisha@moj.io" withMessage:@"This is app feedback" withCompletionBlock:nil withFailure:nil];
}
}
问题是,尽管我能够发送电子邮件,但未调用委托方法。 我如何解决它 ?
您的委托是UIViewController
,因此在此实现mailComposeController:didFinishWithResult:error
委托方法。
我认为问题在于您的对象sendFeedback
在该块的末尾被释放。
最好的解决方案是创建单例对象并使用它
在SendMessage.h
文件中添加
+ (instancetype) sharedManager;
在SendMessage.m
文件中添加
+ (instancetype) sharedManager{
static SendMessage* instance = nil;
if(!instance){
instance = [[SendMessage alloc] init];
}
return instance;
}
比您可以在代码中的任何地方使用单例
-(void) tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
if (indexPath.row == 3) {
SendMessage *sendFeedback = [SendMessage sharedManager];
[sendFeedback sendEmailFromViewController:self withSubject:@"App Feedback" withRecipient:@"ashisha@moj.io" withMessage:@"This is app feedback" withCompletionBlock:nil withFailure:nil];
}
}
同样, messageController.mailComposeDelegate
必须设置为self
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.