Skip to content

Instantly share code, notes, and snippets.

@ruandao
Created July 29, 2014 03:07
Show Gist options
  • Select an option

  • Save ruandao/8021b4e6683c5f564300 to your computer and use it in GitHub Desktop.

Select an option

Save ruandao/8021b4e6683c5f564300 to your computer and use it in GitHub Desktop.
mfmessage send message
- (void)showEmail:(NSString*)file {
NSString *emailTitle = @"Great Photo and Doc";
NSString *messageBody = @"Hey, check this out!";
NSArray *toRecipents = [NSArray arrayWithObject:@"support@appcoda.com"];
MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
mc.mailComposeDelegate = self;
[mc setSubject:emailTitle];
[mc setMessageBody:messageBody isHTML:NO];
[mc setToRecipients:toRecipents];
// Determine the file name and extension
NSArray *filepart = [file componentsSeparatedByString:@"."];
NSString *filename = [filepart objectAtIndex:0];
NSString *extension = [filepart objectAtIndex:1];
// Get the resource path and read the file using NSData
NSString *filePath = [[NSBundle mainBundle] pathForResource:filename ofType:extension];
NSData *fileData = [NSData dataWithContentsOfFile:filePath];
// Determine the MIME type
NSString *mimeType;
if ([extension isEqualToString:@"jpg"]) {
mimeType = @"image/jpeg";
} else if ([extension isEqualToString:@"png"]) {
mimeType = @"image/png";
} else if ([extension isEqualToString:@"doc"]) {
mimeType = @"application/msword";
} else if ([extension isEqualToString:@"ppt"]) {
mimeType = @"application/vnd.ms-powerpoint";
} else if ([extension isEqualToString:@"html"]) {
mimeType = @"text/html";
} else if ([extension isEqualToString:@"pdf"]) {
mimeType = @"application/pdf";
}
// Add attachment
[mc addAttachmentData:fileData mimeType:mimeType fileName:filename];
// Present mail view controller on screen
[self presentViewController:mc animated:YES completion:NULL];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment