Skip to content

Commit

Permalink
Update to version 1.1 in iOS: now plugin handles all type of attachme…
Browse files Browse the repository at this point in the history
…nt, not only images and pdf
  • Loading branch information
Guido Sabatini committed Nov 30, 2012
1 parent d937cf2 commit abde2d2
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 10 deletions.
3 changes: 3 additions & 0 deletions Android/EmailComposerWithAttachments/EmailComposer.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/**
*
* Phonegap Email composer plugin for Android with multiple attachments handling
*
* Version 1.0
*
* Guido Sabatini 2012
*
*/
Expand Down
2 changes: 2 additions & 0 deletions Android/EmailComposerWithAttachments/readme.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# EmailComposer with attachments handling

- Add the EmailComposer.java to your project (use the correct package, or change it in the file)

- Place the EmailComposer.js file somewhere in your www folder, and include it from your html.
Expand Down
1 change: 1 addition & 0 deletions iOS/EmailComposerWithAttachments/EmailComposer.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//
// EmailComposer.h
//
// Version 1.1
//
// Created by Guido Sabatini in 2012.
//
Expand Down
29 changes: 19 additions & 10 deletions iOS/EmailComposerWithAttachments/EmailComposer.m
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//
// EmailComposer.m
//
//
// Version 1.1
//
// Created by Guido Sabatini in 2012.
//
Expand All @@ -12,11 +13,13 @@
#define RETURN_CODE_EMAIL_NOTSENT 4

#import "EmailComposer.h"
#import <MobileCoreServices/MobileCoreServices.h>

@interface EmailComposer ()

-(void) showEmailComposerWithParameters:(NSDictionary*)parameters;
-(void) returnWithCode:(int)code;
-(NSString *) getMimeTypeFromFileExtension:(NSString *)extension;

@end

Expand Down Expand Up @@ -107,15 +110,8 @@ -(void) showEmailComposerWithParameters:(NSDictionary*)parameters {
if (attachmentPaths) {
for (NSString* path in attachmentPaths) {
@try {
if ([path hasSuffix:@".pdf"]) {
NSData *data = [[NSFileManager defaultManager] contentsAtPath:path];
[mailComposer addAttachmentData:data mimeType:@"application/pdf" fileName:[NSString stringWithFormat:@"attachment%d.pdf", counter]];
} else {
// supposed image
UIImage *image = [UIImage imageWithContentsOfFile:path];
NSData *data = UIImagePNGRepresentation(image);
[mailComposer addAttachmentData:data mimeType:@"image/png" fileName:[NSString stringWithFormat:@"attachment%d.png", counter]];
}
NSData *data = [[NSFileManager defaultManager] contentsAtPath:path];
[mailComposer addAttachmentData:data mimeType:[self getMimeTypeFromFileExtension:[path pathExtension]] fileName:[NSString stringWithFormat:@"attachment%d.%@", counter, [path pathExtension]]];
counter++;
}
@catch (NSException *exception) {
Expand Down Expand Up @@ -170,4 +166,17 @@ -(void) returnWithCode:(int)code {
[self writeJavascript:[NSString stringWithFormat:@"window.plugins.emailComposer._didFinishWithResult(%d);", code]];
}

// Retrieve the mime type from the file extension
-(NSString *) getMimeTypeFromFileExtension:(NSString *)extension {
if (!extension)
return nil;
CFStringRef pathExtension, type;
// Get the UTI from the file's extension
pathExtension = (CFStringRef)extension;
type = UTTypeCreatePreferredIdentifierForTag(kUTTagClassFilenameExtension, pathExtension, NULL);

// Converting UTI to a mime type
return (NSString *)UTTypeCopyPreferredTagWithClass(type, kUTTagClassMIMEType);
}

@end
6 changes: 6 additions & 0 deletions iOS/EmailComposerWithAttachments/readme.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# EmailComposer with attachments handling

**Update: VERSION 1.1**
Now the plugin can handle any type of attachments, not only images or PDFs

**Description**
This is a modification of the EmailComposer iOS plugin made by **Randy McMillan**
In this version of the plugin, you can attach images and PDF files to emails. A little refactoring was made.
It is compliant with Cordova 2.2.0 standard (new CDVInvokedUrlCommand parameter sent to native methods). If you want to use the plugin with an older version of Cordova you must comment the method
Expand Down

0 comments on commit abde2d2

Please sign in to comment.