iPhone, Obj-C, How can I use in app email from an

2020-05-06 12:36发布

问题:

I found some great example code some months ago to add a compose button to a view to allows emails to be sent within an app...

http://blog.mugunthkumar.com/coding/iphone-tutorial-in-app-email/

However, I want to be able to show the email, have it handled for use with any of my views. I have a standard class / method to show the actionsheet, so I can use it in all my views.

This is the code from another button in my class.

-(UIViewController *)showHelpClickButtonAtIndex:(int)buttonIndex:(UIView *)
      vw:(UIViewController *)vc:(BOOL)useNav:(HelpPage)page{

if (buttonIndex == CommonUIInfoHelpPagesBtnIdx) {
vc = [[HelpViewController alloc] initWithNibName:@"HelpView" 
         bundle:nil onPage:page];
   [vw addSubview:vc.view];
   return [vc autorelease]; 

Heres the example code, without any of my many attempts to make it work..

MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:titleText.text];
NSString *emailBody = @"Whatever";
[picker setMessageBody:emailBody isHTML:YES]; 
picker.navigationBar.barStyle = UIBarStyleBlack; 
[self presentModalViewController:picker animated:YES];
[picker release];

Its mainly the MFMailComposeViewController and replace presentModalViewController with addSubview where I'm having problems.

Can anyone point me in the right direction ?

回答1:

According to the docs for MFMailComposeViewController:

To display the view managed by this view controller, you can use any of the standard techniques for displaying view controllers. However, the most common way to present this interface is do so modally using the presentModalViewController:animated: method

The first sentence is telling us that you should be showing the view controller for the mail composer in one of the regular ways; adding the view controller's view as a subview to an existing UIView, if that's what you're trying to do, isn't one of the standard ways Apple want you to use, and so might not work or be unpredictable.

Related to this is Apple's advice that a UIViewController should be responsible for showing a whole part of your UI, not just part of it -- which your approach also seems to go against.

So, the solution is to present the mail composer like a normal view controller would be: modally, or pushed onto a nav stack, or presented as the top view under UIWindow, etc.