Implement MFMailComposeViewControllerDelegate
@interface EMailSending : CCLayer <MFMailComposeViewControllerDelegate> {
...
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
...
}
Add Child View Controller
emailViewController = [[UIViewController alloc] init];
[[CCDirector sharedDirector] addChildViewController:emailViewController];
Create Mail Compose View Controller
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
Present Modal View Controller
[emailViewController presentModalViewController:picker animated:YES];
Dismiss Modal View Controller
[controller dismissModalViewControllerAnimated:NO];
Create Render Texture and Take Snapshot for this Layer
CCRenderTexture *renderTexture = [CCRenderTexture renderTextureWithWidth:windowSize.width height:windowSize.height];
[renderTexture begin];
[self visit];
[renderTexture end];
[renderTexture saveToFile:file format:kCCImageFormatPNG];
Compose EMail with Attachment
picker.mailComposeDelegate = self;
[picker setSubject:@"This is test subject"];
[picker setMessageBody:@"This is test message body" isHTML:YES];
...
NSString *file = @"screenShot.png";
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *screenShotPath = [documentsDirectory stringByAppendingPathComponent:file];
NSData *data = [NSData dataWithContentsOfFile:screenShotPath];
[picker addAttachmentData:data mimeType:@"image/png" fileName:@"attachment .png"];
Save Snapshot to Photo Album
NSData *data = [NSData dataWithContentsOfFile:screenShotPath];
UIImage *image = [UIImage imageWithData:data];
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
Sample code for EMailSending.h
#import "cocos2d.h"
#import "MessageUI/MessageUI.h"
@interface EMailSending : CCLayer <MFMailComposeViewControllerDelegate> {
UIViewController *emailViewController;
CCMenu *menu;
CCSprite *sprite;
}
+(CCScene *)scene;
@end
Sample code for EMailSending.m
#import "EMailSending.h"
@implementation EMailSending
+(CCScene *)scene {
CCScene *scene = [CCScene node];
EMailSending *layer = [EMailSending node];
[scene addChild:layer];
return scene;
}
-(id)init {
if (self = [super init]) {
CCMenuItem *emailItem = [CCMenuItemFont itemWithString:@"Email" target:self selector:@selector(emailCallback)];
menu = [CCMenu menuWithItems:emailItem, nil];
menu.position = ccp(50, 50);
[self addChild:menu];
sprite = [CCSprite spriteWithFile:@"mySprite.png"];
sprite.position = ccp(200, 200);
[self addChild:sprite];
emailViewController = [[UIViewController alloc] init];
[[CCDirector sharedDirector] addChildViewController:emailViewController];
}
return self;
}
-(void)emailCallback {
[[CCDirector sharedDirector] pause];
[[CCDirector sharedDirector] stopAnimation];
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
[picker setSubject:@"This is test subject"];
[picker setMessageBody:@"This is test message body" isHTML:YES];
NSString *file = @"screenShot.png";
[CCDirector sharedDirector].nextDeltaTimeZero = YES;
CGSize windowSize = [CCDirector sharedDirector].winSize;
CCRenderTexture *renderTexture = [CCRenderTexture renderTextureWithWidth:windowSize.width height:windowSize.height];
[renderTexture begin];
[self visit];
[renderTexture end];
[renderTexture saveToFile:file format:kCCImageFormatPNG];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *screenShotPath = [documentsDirectory stringByAppendingPathComponent:file];
NSData *data = [NSData dataWithContentsOfFile:screenShotPath];
UIImage *image = [UIImage imageWithData:data];
UIImageWriteToSavedPhotosAlbum(image, nil, nil, nil);
[picker addAttachmentData:data mimeType:@"image/png" fileName:@"attachment .png"];
[emailViewController presentModalViewController:picker animated:YES];
}
-(void)mailComposeController:(MFMailComposeViewController *)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error {
[[CCDirector sharedDirector] resume];
[[CCDirector sharedDirector] startAnimation];
[controller dismissModalViewControllerAnimated:NO];
}
@end