Question

I'm using cocos2d-x framework and I need to popup the mail when clicking button.

The code works fine on 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

But on my ipad mini (6.0) the mail popped up correctly but when clicked the "send mail" or "cancel" the view was removed and leaving a black screen (everything on the screen is gone)

Any advice will be appreciated, thanks :)

Was it helpful?

Solution

try this code

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

}

OTHER TIPS

I'm using this code for feedback via email in my cocos2d-x game.

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

You can add subject:

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

This solution tested on iOS and Android.

popup email

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top