My app has a common class that displays an actionSheet whenever “Contact Us” is clicked from any one of the many NIBs.
If the user chooses “Email Us” from the actionSheet popup, I’d like to call the email methods from the same common class. After researching I implemented this:
-(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];
}
This will launch a new email, however:
-
The didFinishWithResult doesn’t work as the modal view is not removed either after sending the email or pressing the Cancel button
-
I am getting this warning: assigning to ‘id’ from incompatible type ‘UIViewController *__strong’
controller.mailComposeDelegate = rootViewController;
Help appreciated.
You need to set your
rootViewControlleras adopting the delegateMFMailComposeViewControllerDelegate.i.e. in your RootViewController.h file, add that part to the interface declaration so that it looks similar to: