@CJ: Thank you, thank you, thank you! That did the trick. I thought I was going completely crazy last night.
The key was the '[director stopAnimation]'. I assumed that it was included in [Directore pause], but I guess assuming was my mistake.
Here is what my implementation looks like:
I include the MessageUI and MFMailComposeViewController in the header of my Scene/Layer, make the layer a MFMailComposeViewControllerDelegate a and setup a mailComposer:
#import <MessageUI/MessageUI.h>
#import <MessageUI/MFMailComposeViewController.h>
@interface MyCCLayer : CCLayer <MFMailComposeViewControllerDelegate> {
...
UIViewController *mailComposer;
}
Then in the layer I added code to show the controller, and a mailComposeController callback:
Note, I am currently building a plist for level data, and making a level Thumbnail, and attaching both to the email. I am using this for beta testers to send me levels they create for the game. When I release it, I will redo this a bit so that people can send each other their levels to play.
-(void) showEmail: (id) sender {
CCDirector *director = [CCDirector sharedDirector];
[director pause];
[director stopAnimation];
[director.openGLView setUserInteractionEnabled:NO];
int levelNum = [self getCurrentLevelNum];
// This takes my dictionary that stores level data, and creates xml data that we will attach to the email
NSDictionary *levelDict = [levelArray objectAtIndex: levelNum];
NSString *error = [[NSString alloc] init];
NSData *levelData = [NSPropertyListSerialization dataFromPropertyList: levelDict
format: NSPropertyListXMLFormat_v1_0
errorDescription: &error];
NSLog(@"Error: %@", error);
// This creates the level thumbnail, which we will include in the email as well
// There is probably a better way to do this
NSString *levelString = [levelDict objectForKey: @"data"];
LevelPickLayer *levelPick = [LevelPickLayer node];
CCSprite *levelSprite = [levelPick createThumbnailLevel: levelString shadows: NO];
CCRenderTexture *levelThumb = [CCRenderTexture renderTextureWithWidth: 224 height: 160];
[levelThumb begin];
[levelSprite setScale: LVL_SPRITE_SCALE*2];
[levelSprite setPosition: ccp(0,0)];
[levelSprite setAnchorPoint: ccp(0,0)];
[levelSprite visit];
[levelThumb end];
[levelThumb saveBuffer: @"mylevel.png" format: kImageFormatPNG];
// Read back the level thumbnail so we can attach the PNG to the email
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *fullPath = [documentsDirectory stringByAppendingPathComponent: @"mylevel.png"];
NSData *imageData = [NSData dataWithContentsOfFile: fullPath];
// The actual mail window call
mailComposer = [[UIViewController alloc] init];
[mailComposer setView:[[CCDirector sharedDirector] openGLView]];
[mailComposer setModalTransitionStyle: UIModalTransitionStyleCoverVertical];
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject: @"My Game Level"];
[picker addAttachmentData: imageData mimeType:@"image/png" fileName: @"mylevel.png"];
[picker addAttachmentData: levelData mimeType:@"text/xml" fileName: @"mylevel.plist"];
// Fill out the email body text
NSString *content = @"Blah blah.\n";
NSString *pageLink = @"http://mylink.com";
NSString *iTunesLink = @"http://iTunes.com/mylevellink";
NSString *emailBody = [NSString stringWithFormat: @"Sent from <a href = '%@'>Fun Game</a> on iPhone. <a href = '%@'>Download</a> yours from AppStore now!", pageLink, iTunesLink];
[picker setMessageBody:emailBody isHTML:YES];
[mailComposer presentModalViewController:picker animated:NO];
[picker release];
}
// Dismisses the email composition interface when users tap Cancel or Send.
-(void) mailComposeController: (MFMailComposeViewController*)controller didFinishWithResult: (MFMailComposeResult)result error:(NSError*)error
{
// Notifies users about errors associated with the interface
switch (result)
{
case MFMailComposeResultCancelled:
break;
case MFMailComposeResultSaved:
break;
case MFMailComposeResultSent:
break;
case MFMailComposeResultFailed:
break;
default:
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Email" message:@"Sending Failed – Unknown Error "
delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
[alert show];
[alert release];
}
break;
}
[mailComposer dismissModalViewControllerAnimated:NO];
// Force game back to landscape mode. If you don't weird things happen
[[UIApplication sharedApplication] setStatusBarOrientation: CCDeviceOrientationLandscapeLeft animated:NO];
CCDirector *director = [CCDirector sharedDirector];
[director.openGLView setUserInteractionEnabled:YES];
[director startAnimation];
[director resume];
[mailComposer.view.superview removeFromSuperview];
}