Permalink
Browse files

Update to version 1.1 in iOS: now plugin handles all type of attachme…

…nt, not only images and pdf
  • Loading branch information...
1 parent d937cf2 commit abde2d22e0be089ae19458e2182e0658d9f2f240 Guido Sabatini committed Nov 30, 2012
@@ -1,6 +1,9 @@
/**
*
* Phonegap Email composer plugin for Android with multiple attachments handling
+ *
+ * Version 1.0
+ *
* Guido Sabatini 2012
*
*/
@@ -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.
@@ -1,6 +1,7 @@
//
// EmailComposer.h
//
+// Version 1.1
//
// Created by Guido Sabatini in 2012.
//
@@ -1,6 +1,7 @@
//
// EmailComposer.m
-//
+//
+// Version 1.1
//
// Created by Guido Sabatini in 2012.
//
@@ -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
@@ -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) {
@@ -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
@@ -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

0 comments on commit abde2d2

Please sign in to comment.