IOS5:如何从NSObject类调用电子邮件

时间:2012-03-18 14:37:13

标签: ios email nsobject

我的应用程序有一个公共类,只要从许多NIB中的任何一个点击“联系我们”,就会显示一个actionSheet。

如果用户从actionSheet弹出窗口中选择“向我们发送电子邮件”,我想从同一个公共类中调用电子邮件方法。经过研究我实现了这个:

-(void)SendEmail {

rootViewController = (UIViewController*)
[(AppDelegate*)[[UIApplication sharedApplication] delegate] viewController];

// compose
MFMailComposeViewController* controller = [[MFMailComposeViewController alloc] init];
controller.mailComposeDelegate = rootViewController;


//format message
NSArray *recipientsArray = [[NSArray alloc] initWithObjects:@"support@somename.com", nil];
[controller setToRecipients:recipientsArray];
[controller setSubject:[NSString stringWithFormat:@"A question about %@",string]];
[controller setMessageBody:outputMutString isHTML:YES];

//send
if (controller) [rootViewController presentModalViewController:controller animated:YES];

 }

// didFinishWithResult

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

if (result == MFMailComposeResultSent) {
}
[rootViewController dismissModalViewControllerAnimated:YES];
 }

然而,这将启动一封新电子邮件:

  1. didFinishWithResult不起作用,因为在发送电子邮件或按取消按钮后未删除模态视图

  2. 我收到此警告:从不兼容的类型'UIViewController * __ strong'分配'id' controller.mailComposeDelegate = rootViewController;

  3. 帮助表示赞赏。

1 个答案:

答案 0 :(得分:2)

您需要将rootViewController设置为采用委托MFMailComposeViewControllerDelegate

即。在RootViewController.h文件中,将该部分添加到接口声明中,使其看起来类似于:

@interface RootViewController : UIViewController <MFMailComposeViewControllerDelegate>