我尝试使用MessageUI框架以编程方式发送邮件。这就是我在MyScene.m中所拥有的。当我调用该方法时,会打开一个邮件窗口。但是当我取消邮件或发送邮件时,它并没有回到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];
}
答案 0 :(得分:3)
您将场景设置为MFMailComposeViewController
的代理人:
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
将其更改为
mc.mailComposeDelegate = self.view.window.rootViewController;
但那当然是一种不好的做法。 SKScene
是一个表示层。您应该从MFMailComposeViewController
提供UIViewController
。
修改强>:
将此代码添加到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];
}
要发送邮件时请调用此方法:
[[NSNotificationCenter defaultCenter] postNotificationName:@"showMailComposer" object:nil];