iPhone In App电子邮件问题

时间:2011-03-04 18:10:09

标签: iphone objective-c ios4 iphone-sdk-3.0

当我按下sendMail按钮时,它会转到邮件按钮,但是当我点击发送或取消它时,它不会将我带回我的应用程序。有什么建议吗?

-(IBAction)sendMail {

    MFMailComposeViewController *mailComposer = [[[MFMailComposeViewController alloc] init] autorelease] ;

    if ([MFMailComposeViewController canSendMail]) {
        [mailComposer setToRecipients:nil];
        [mailComposer setSubject:nil];
        [mailComposer setMessageBody:@"Default text" isHTML:NO];

        [self presentModalViewController:mailComposer animated:YES];
    }
}

3 个答案:

答案 0 :(得分:4)

您需要设置一个委托(通常与呈现MFMailComposeViewController的视图控制器相同)。然后当用户点击Save或Cancel按钮时,MFMailComposeViewController将在委托上调用-mailComposeController:didFinishWithResult:error。因此,将自己设置为委托并定义以下方法:

#pragma mark -
#pragma mark MessageUI Delegate Methods

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

    [controller dismissModalViewControllerAnimated:YES];
}

答案 1 :(得分:1)

在邮件编辑器初始化

下面添加以下行
 mailComposer.mailComposeDelegate = self;//very important if you want feedbacks on what the user did with your email sheet.

然后按照Kenny的建议实现委托方法。您可以使用此方法执行自定义操作。

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{ 
 // Notifies users about errors associated with the interface
 switch (result)
 {
 case MFMailComposeResultCancelled:
     {
      //Do something, If you need to
     }
 break;

 default:
 break;
 }
 [self dismissModalViewControllerAnimated:YES];
}

请记住通过添加

来符合委托
@interface YourViewController : UIViewController <MFMailComposeViewControllerDelegate> { }

如果您仍然遇到问题,可以访问以下教程,其中包含所有内容: http://blog.mugunthkumar.com/coding/iphone-tutorial-in-app-email/

答案 2 :(得分:1)

这条精彩的路线:

mailComposer.mailComposeDelegate = self;

是让它在不知道出了什么问题的情况下发生了几天的事情。

不要忘记他们:

# import <UIKit/UIKit.h>
# import <MessageUI/MessageUI.h>
# import <MessageUI/MFMailComposeViewController.h>

除了在项目中导入MessageUI.framework

在IOS5中验证