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

Posted

技术标签:

【中文标题】在 Sprite Kit 中以编程方式发送邮件【英文标题】:Send a mail programmatically in Sprite Kit 【发布时间】:2014-07-25 12:23:25 【问题描述】:

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

【问题讨论】:

【参考方案1】:

您正在将场景设置为MFMailComposeViewController 的代理:

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];

【讨论】:

那也行不通。如何从 UIViewController 呈现它? - (void) mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error 被解雇了吗?

以上是关于在 Sprite Kit 中以编程方式发送邮件的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Chrome 扩展程序中以编程方式关闭 gmail 撰写对话框

如何使用 Apex 在 SalesForce 中以编程方式访问报告

在 iphone 中以编程方式发送短信

Sprite 如何在 cocos2dx 中以编程方式进行修剪?

如何在Objective-C中以编程方式发送带有一些正文文本的短信[重复]

如何在 Linux Mint 中以编程方式发送密钥(模拟密钥输入)?