ios - 在 Sprite Kit 中以编程方式发送邮件

标签 ios email sprite-kit messageui

我尝试使用 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];
}

最佳答案

您正在将场景设置为 MFMailComposeViewController 的委托(delegate):

MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;

改成

mc.mailComposeDelegate = self.view.window.rootViewController;

但这当然是一种不好的做法。 SKScene 是表示层。您应该显示 UIViewController 中的 MFMailComposeViewController

编辑:

将此代码添加到 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];

关于ios - 在 Sprite Kit 中以编程方式发送邮件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24955768/

相关文章:

iOS mailto 带有换行符 (%0A) 的链接被转换为 <BR>

xcode - El Capitan 更新后 SpriteKit View 显示随机噪声

ios - 在 xCode 中使用多个 Assets 文件夹

ios - 我使用递归对 Twitter 好友进行分页的代码不起作用 - iOS

iOS 8 交互式通知默认不显示按钮

ios - 未找到包含任何钥匙串(keychain)签名证书的未过期配置文件

bash - 如何设置 Airflow 的电子邮件配置以发送有关错误的电子邮件?

ios - 将 UILabel 的基类 UIView 更改为自定义 UIView?

安卓。发送多附件电子邮件

swift - 简单的坐标位置