[英]iOS: MFMailComposeViewController not working on model
Having trouble with the MFMessageComposeViewController. 遇到MFMessageComposeViewController问题。 The situation goes like this:
情况是这样的:
I am creating a model that will implement the MFMessageComposeViewController instead of doing it in the ViewController directly. 我正在创建一个模型,它将实现MFMessageComposeViewController而不是直接在ViewController中执行它。 Now, when I implement the presentModalViewController:: method, it works fine (the mail.app interface appears) but when I click the cancel / send button in the mail.app interface, it will not dismiss the mail.app..
现在,当我实现presentModalViewController ::方法时,它工作正常(出现mail.app接口)但是当我单击mail.app界面中的取消/发送按钮时,它不会忽略mail.app ..
Its like something like this: 它像这样的东西:
a method snippet from MSGViewController that implements the send mail model: MSGViewController的一个方法片段,它实现了发送邮件模型:
- (IBAction)openEmail:(id)sender {
Messaging *mail = [[Messaging alloc]initWithModal:self];
[mail emailInvitation:@"Eventz Date" eventAt:@"Eventz Location" withImage:nil];
[self.sendingStatus setText:mail.sendingStatus];
}
my model for implementing the Messaging: 我实现消息传递的模型:
Messaging.h Messaging.h
#import <Foundation/Foundation.h>
#import <MessageUI/MessageUI.h>
@interface Messaging : NSObject <MFMailComposeViewControllerDelegate, MFMessageComposeViewControllerDelegate>
@property (nonatomic, copy) NSString *sendingStatus;
@property (nonatomic, retain) id modal;
- (id)initWithModal:(id)modal;
- (void)emailInvitation:(NSString *)eventDate eventAt:(NSString *)eventLocation withImage:(UIImage *)imageAttachment;
@end
Messaging.m Messaging.m
#import "Messaging.h"
@implementation Messaging
@synthesize sendingStatus = _sendingStatus;
@synthesize modal = _modal;
- (id)initWithModal:(id)modal{
self = [super init];
self.modal = modal;
return self;
}
- (void)emailInvitation:(NSString *)eventDate eventAt:(NSString *)eventLocation withImage:(UIImage *)imageAttachment{
MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];
if ([MFMailComposeViewController canSendMail]){
mailer.mailComposeDelegate = self;
[mailer setSubject:@"Event Invitation"];
[mailer setMessageBody:@"message body"] isHTML:NO];
if(imageAttachment != nil){
NSData *imageData = UIImagePNGRepresentation(imageAttachment);
[mailer addAttachmentData:imageData mimeType:@"image/png" fileName:@"imageFileName"];
}
[self.modal presentModalViewController:mailer animated:YES];
return;
}
[self deviceDoNotSupportMessaging];
}
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
NSLog(@"called");
switch (result)
{
case MFMailComposeResultCancelled:
self.sendingStatus = @"Mail sending cancelled.";
break;
case MFMailComposeResultSaved:
self.sendingStatus = @"Mail saved.";
break;
case MFMailComposeResultSent:
self.sendingStatus = @"Mail sent.";
break;
case MFMailComposeResultFailed:
self.sendingStatus = @"Mail sending failed.";
break;
default:
self.sendingStatus = @"Mail not sent.";
break;
}
[controller dismissModalViewControllerAnimated:YES];
}
BTW, the NSLog(@"called"); BTW,NSLog(@“叫”); in the .m is not called actually... Can someone have any suggestion?
在.m中实际上并未调用...有人可以提出任何建议吗? thanks.. :D
感谢:D
I am wondering why you aren't getting an error when your pointer get dealloc'd. 我想知道为什么当你的指针被dealloc'd时你没有得到错误。 I used your code and got that error right away when instantiating the Message class:
我使用你的代码并在实例化Message类时立即得到了这个错误:
Messaging *mail = [[Messaging alloc]initWithModal:self];
So I changed one thing, I made mail a Strong Property in my calling class: 所以我改变了一件事,我在我的通话类中发送了一个Strong Property:
@property (strong, nonatomic) Messaging *mail;
And then called it this way: 然后这样称呼它:
mail = [[Messaging alloc]initWithModal:self];
[mail emailInvitation:@"Eventz Date" eventAt:@"Eventz Location" withImage:nil];
NSLog(@"Called the emailInvitation");
The log statements were called and I even made sure that the MailComposer got back the proper response: 调用了日志语句,我甚至确保MailComposer得到了正确的响应:
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
NSLog(@"called");
switch (result)
{
case MFMailComposeResultCancelled:
self.sendingStatus = @"Mail sending cancelled.";
NSLog(@"Mail Cancelled");
break;
case MFMailComposeResultSaved:
self.sendingStatus = @"Mail saved.";
NSLog(@"Mail Saved");
break;
case MFMailComposeResultSent:
self.sendingStatus = @"Mail sent.";
NSLog(@"Mail Sent");
break;
case MFMailComposeResultFailed:
self.sendingStatus = @"Mail sending failed.";
NSLog(@"Mail Failed");
break;
default:
self.sendingStatus = @"Mail not sent.";
NSLog(@"Mail Not Sent");
break;
}
[controller dismissModalViewControllerAnimated:YES];
}
And it did. 它确实如此。
BTW - I love the way you are doing this. 顺便说一句 - 我喜欢你这样做的方式。 This is the way OOP should be done.
这是OOP应该完成的方式。
write 写
[self.modal dismissModalViewControllerAnimated:YES];
instead of [controller dismissModalViewControllerAnimated:YES];
而不是
[controller dismissModalViewControllerAnimated:YES];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.