开发者

iPhone In App Email Issue

when I press the sendMail button, it will go to the mail button, but when I hit send or cancel it will not take me back to my application. Any suggestions?

-(IBAction)sendMail {

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

    if ([MFMailComposeViewController canSendMail]) {
        [mailComposer setToRecipients:nil];
开发者_C百科        [mailComposer setSubject:nil];
        [mailComposer setMessageBody:@"Default text" isHTML:NO];

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


You need to set a delegate (generally the same view controller that presented the MFMailComposeViewController). Then when the user taps the Save or Cancel button the MFMailComposeViewController will call -mailComposeController:didFinishWithResult:error on the delegate. So set yourself as the delegate and define the following method:

#pragma mark -
#pragma mark MessageUI Delegate Methods

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

    [controller dismissModalViewControllerAnimated:YES];
}


Add the following line below your mail composer initialization

 mailComposer.mailComposeDelegate = self;//very important if you want feedbacks on what the user did with your email sheet.

Then implement the delegate method as Kenny suggested. You can use this method to take custom actions.

- (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];
}

Remember to conform to the delegate by adding

@interface YourViewController : UIViewController <MFMailComposeViewControllerDelegate> { }

If you are still having trouble, You may visit the following tutorial where everything nicely explained: http://blog.mugunthkumar.com/coding/iphone-tutorial-in-app-email/


This wonderful line:

mailComposer.mailComposeDelegate = self;

is what made ​​it happen for days without knowing that was what went wrong.

And do not forget them:

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

In addition to importing the MessageUI.framework in the project.

Verified in IOS5

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜