iPad上的MFMailComposeViewController黑屏

时间:2014-02-08 17:41:02

标签: ios objective-c ipad cocos2d-x mfmailcomposeviewcontroller

我正在使用cocos2d-x框架,我需要在点击按钮时弹出邮件。

代码在iphone5(6.0),ipod touch 5(6.0)上运行良好:

MailSender.h

@interface MailSender : UIViewController <MFMailComposeViewControllerDelegate>

{

    UIViewController *currentModalViewController;
}

-(void)sendMail:(const char *)subject receiver:(const char *)receiver;

@end

MailSender.mm

#import "MailSender.h"
#import "../cocos2dx/platform/ios/EAGLView.h"

@implementation MailSender

- (void)sendMail:(const char *)subject receiver:(const char *)receiver
{
    if ([MFMailComposeViewController canSendMail])
    {
        MFMailComposeViewController *mailer = [[MFMailComposeViewController alloc] init];

        mailer.mailComposeDelegate = self;

        [mailer setSubject:[NSString stringWithUTF8String:subject]];

        NSArray *toRecipients = [NSArray arrayWithObject:[NSString stringWithFormat:@"%s", receiver]];
        [mailer setToRecipients: toRecipients];

        //NSString *emailBody = [NSString stringWithFormat:@"<p>This is a sample posting in iOS. My Score is %s!</p>",score];
        NSString *emailBody = @"";

        [mailer setMessageBody:emailBody isHTML:YES];

        // only for iPad
        // mailer.modalPresentationStyle = UIModalPresentationFormSheet;

        UIViewController* rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
        currentModalViewController = [UIViewController alloc];
        [rootViewController.view addSubview:currentModalViewController.view];
        [currentModalViewController presentViewController:mailer animated:true completion:nil];
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Failure"
                                                        message:@"Your device doesn't support the composer sheet"
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles: nil];
        [alert show];
        [alert release];
    }

}

- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error
{
    const char *message;
    switch (result)
    {
        case MFMailComposeResultCancelled:
            message = "Mail cancelled";
            break;
        case MFMailComposeResultSaved:
            message = "Mail saved";
            break;
        case MFMailComposeResultSent:
            message = "Mail send";
            break;
        case MFMailComposeResultFailed:
            message = "Mail failed";
            break;
        default:
            message = "Mail cancelled";
            break;
    }
    NSLog(@"%s",message);

    [currentModalViewController dismissViewControllerAnimated:true completion:nil];
    [currentModalViewController.view.superview removeFromSuperview];
    [currentModalViewController release];
}

@end

但是在我的ipad mini(6.0)上,邮件正确弹出,但点击“发送邮件”或“取消”后,视图被移除并留下黑屏(屏幕上的所有内容都消失了)

任何建议都将不胜感激,谢谢:)

2 个答案:

答案 0 :(得分:1)

试试这段代码

if ([MFMailComposeViewController canSendMail]) {

                mailComposer = [[MFMailComposeViewController alloc]init];
                mailComposer.mailComposeDelegate = self;
                [mailComposer setToRecipients:@[@"yourmail@com"]];
                [mailComposer setSubject:@"Subject"];
                [mailComposer setMessageBody:@"hello \n" isHTML:NO];
                [self presentViewController:mailComposer animated:YES completion:nil];

            }
            else{

                UIAlertView *alert=[[UIAlertView alloc] initWithTitle:@"Error"
                                                              message:@"can not send mail with this device"
                                                             delegate:nil
                                                    cancelButtonTitle:@"Ok"
                                                    otherButtonTitles:nil, nil];
                [alert show];
            }

pragma mark MFMailComposeViewControllerDelegate

-(void)mailComposeController:(MFMailComposeViewController *)controller
     didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error{
if (result) {
    NSLog(@"Result : %d",result);
}
if (error) {
    NSLog(@"Error : %@",error);
}
[self dismissViewControllerAnimated:YES completion:nil];

}

答案 1 :(得分:0)

我在cocos2d-x游戏中使用此代码通过电子邮件发送反馈。

Application::getInstance()->openURL("mailto:" + SUPPORT_EMAIL);

您可以添加主题:

Application::getInstance()->openURL("mailto:" + SUPPORT_EMAIL + "?subject=Hello from NX");

此解决方案在iOS和Android上进行了测试。

popup email