I don't think you can directly access the mail controller without creating your own UIActivity subclass (YourActivity).
Set up your MFMailComposeViewController in "YourActivity" and it will operate as it did in your main code. Here is how I did it:
In YourActivity.h:
Make yourself the mail controller delegate and set up method wide variables for the mail view controller and the selected view controller:
@interface YourActivity : UIActivity <MFMailComposeViewControllerDelegate>
{
MFMailComposeViewController *mailController;
UIViewController *activityViewController;
}
In YourActivity.m:
(Optional) I recommend you check for the availability of mail services early. (This will prevent the user from being offered an option that can't be completed):
- (BOOL)canPerformWithActivityItems:(NSArray *)activityItems
{
// If mail is unavailable, can't perform activity
if (![MFMailComposeViewController canSendMail]) {
return NO;
}
for (id item in activityItems) {
// whatever other checks you want to do
return YES;
}
return NO;
}
In YourActivity -prepareWithActivityItems: method set up your MFMailComposeViewController:
- (void)prepareWithActivityItems:(NSArray *)activityItems
{
// See if we can send mail (shouldn't happen if we checked already in -canPerformActivityWithItems)
if (![MFMailComposeViewController canSendMail]) {
UIAlertController *mailAlertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"Mail Unavailable", @"mail unavailable")
message:nil
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:NSLocalizedString(@"Cancel", @"cancel")
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * _Nonnull action) {
[self activityDidFinish:NO];
}];
[mailAlertController addAction:cancel];
// Set the alert as the view to return
activityViewController = mailAlertController;
}
// Create a mail view controller
mailController = [[MFMailComposeViewController alloc] init];
// Set Delegate
[mailController setMailComposeDelegate:self];
// Set mail controller as the view to return
activityViewController = mailController;
// Paste the rest of your MFMailComposeViewController code here
}
In YourActivity -activityViewController method return your selected view controller:
- (UIViewController *)activityViewController
{
return activityViewController;
}
Remember to implement the mail finish handler, at a minimum to dismiss the composer view:
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
// Send any messages, if desired, to the controller before dismissing
NSString *message = nil;
NSString *errorMessage = nil;
if (result == MFMailComposeResultFailed) {
message = NSLocalizedString(@"Unable to send email", @"Unable to send email");
}
if (error) {
errorMessage = [message stringByAppendingString:[NSString stringWithFormat:NSLocalizedString(@"Error:\n%@", @"error:\n%@"), [error localizedDescription]]];
}
// Send mail status alert message, if needed
if (message) {
UIAlertController *mailAlert = [UIAlertController alertControllerWithTitle:message
message:errorMessage
preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:NSLocalizedString(@"OK", @"OK")
style:UIAlertActionStyleCancel
handler:^(UIAlertAction * _Nonnull action) {
// Dismiss the mail controller
[controller dismissViewControllerAnimated:true completion:^{}];
[controller release];
mailController = nil;
}];
[mailAlert addAction:cancel];
[controller presentViewController:mailAlert animated:YES completion:^{
//
}];
}
else {
// Dismiss the mail controller
[controller dismissViewControllerAnimated:true completion:^{}];
[controller release];
mailController = nil;
}
}