I've tried to send mails programmatically using the MessageUI framework. This is what I have in MyScene.m. When I call the method, a mail window opens. But when I cancel the mail or when I send the mail, it doesn't goes back to MyScene.
#import <MessageUI/MessageUI.h>
-(void)sendMail
{
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:@"Subject"];
[mc setMessageBody:@"Hello!" isHTML:NO];
[mc setToRecipients:[NSArray arrayWithObject:@"my.email@gmail.com"]];
// Present mail view controller on screen
[self.view.window.rootViewController presentViewController:mc animated:YES completion:NULL];
}
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(@"Mail cancelled");
break;
case MFMailComposeResultSaved:
NSLog(@"Mail saved");
break;
case MFMailComposeResultSent:
NSLog(@"Mail sent");
break;
case MFMailComposeResultFailed:
NSLog(@"Mail sent failure: %@", [error localizedDescription]);
break;
default:
break;
}
// Close the Mail Interface
[self.view.window.rootViewController dismissViewControllerAnimated:YES completion:NULL];
}
You are setting the scene as a delegate for MFMailComposeViewController
:
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
Change it to
mc.mailComposeDelegate = self.view.window.rootViewController;
But that's of course a bad practice. SKScene
is a presentation layer. You should present MFMailComposeViewController
from UIViewController
.
EDIT:
Add this code to GameSceneViewController
:
- (void)viewDidLoad {
[super viewDidLoad];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(sendMail) name:@"showMailComposer" object:nil];
}
-(void)sendMail
{
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:@"Subject"];
[mc setMessageBody:@"Hello!" isHTML:NO];
[mc setToRecipients:[NSArray arrayWithObject:@"my.email@gmail.com"]];
// Present mail view controller on screen
[self.view.window.rootViewController presentViewController:mc animated:YES completion:NULL];
}
- (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
{
switch (result)
{
case MFMailComposeResultCancelled:
NSLog(@"Mail cancelled");
break;
case MFMailComposeResultSaved:
NSLog(@"Mail saved");
break;
case MFMailComposeResultSent:
NSLog(@"Mail sent");
break;
case MFMailComposeResultFailed:
NSLog(@"Mail sent failure: %@", [error localizedDescription]);
break;
default:
break;
}
// Close the Mail Interface
[self dismissViewControllerAnimated:YES completion:NULL];
}
Call this when you want to send a mail:
[[NSNotificationCenter defaultCenter] postNotificationName:@"showMailComposer" object:nil];