Email Composer (iOS) with support to attachments

This commit is contained in:
Guido Sabatini
2012-11-21 17:13:02 +01:00
parent 2cc98a6747
commit bc40c51159
4 changed files with 301 additions and 0 deletions

View File

@@ -0,0 +1,25 @@
//
// EmailComposer.h
//
//
// Created by Jesse MacFadyen on 10-04-05.
// Copyright 2010 Nitobi. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <MessageUI/MFMailComposeViewController.h>
#import <Cordova/CDVPlugin.h>
@interface EmailComposer : CDVPlugin <MFMailComposeViewControllerDelegate> {
}
// UNCOMMENT THIS METHOD if you want to use the plugin with versions of cordova < 2.2.0
//- (void) showEmailComposer:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
// COMMENT THIS METHOD if you want to use the plugin with versions of cordova < 2.2.0
- (void) showEmailComposer:(CDVInvokedUrlCommand*)command;
@end

View File

@@ -0,0 +1,60 @@
// window.plugins.emailComposer
function EmailComposer() {
this.resultCallback = null; // Function
}
EmailComposer.ComposeResultType = {
Cancelled:0,
Saved:1,
Sent:2,
Failed:3,
NotSent:4
}
// showEmailComposer : all args optional
EmailComposer.prototype.showEmailComposer = function(subject,body,toRecipients,ccRecipients,bccRecipients,bIsHTML,attachments) {
var args = {};
if(toRecipients)
args.toRecipients = toRecipients;
if(ccRecipients)
args.ccRecipients = ccRecipients;
if(bccRecipients)
args.bccRecipients = bccRecipients;
if(subject)
args.subject = subject;
if(body)
args.body = body;
if(bIsHTML)
args.bIsHTML = bIsHTML;
if(attachments)
args.attachments = attachments;
cordova.exec(null, null, "EmailComposer", "showEmailComposer", [args]);
}
EmailComposer.prototype.showEmailComposerWithCallback = function(callback, subject, body, toRecipients, ccRecipients, bccRecipients, isHTML, attachments) {
this.resultCallback = callback;
this.showEmailComposer.apply(this,[subject,body,toRecipients,ccRecipients,bccRecipients,isHTML,attachments]);
}
EmailComposer.prototype._didFinishWithResult = function(res) {
this.resultCallback(res);
}
cordova.addConstructor(function() {
if(!window.plugins)
{
window.plugins = {};
}
// shim to work in 1.5 and 1.6
if (!window.Cordova) {
window.Cordova = cordova;
};
window.plugins.emailComposer = new EmailComposer();
});

View File

@@ -0,0 +1,174 @@
//
// EmailComposer.m
//
//
// Created by Jesse MacFadyen on 10-04-05.
// Copyright 2010 Nitobi. All rights reserved.
//
#define RETURN_CODE_EMAIL_CANCELLED 0
#define RETURN_CODE_EMAIL_SAVED 1
#define RETURN_CODE_EMAIL_SENT 2
#define RETURN_CODE_EMAIL_FAILED 3
#define RETURN_CODE_EMAIL_NOTSENT 4
#import "EmailComposer.h"
@interface EmailComposer ()
-(void) showEmailComposerWithParameters:(NSDictionary*)parameters;
-(void) returnWithCode:(int)code;
@end
@implementation EmailComposer
// UNCOMMENT THIS METHOD if you want to use the plugin with versions of cordova < 2.2.0
//- (void) showEmailComposer:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options {
// NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:
// [options valueForKey:@"toRecipients"], @"toRecipients",
// [options valueForKey:@"ccRecipients"], @"ccRecipients",
// [options valueForKey:@"bccRecipients"], @"bccRecipients",
// [options valueForKey:@"subject"], @"subject",
// [options valueForKey:@"body"], @"body",
// [options valueForKey:@"bIsHTML"], @"bIsHTML",
// [options valueForKey:@"attachments"], @"attachments",
// nil];
// [self showEmailComposerWithParameters:parameters];
//}
// COMMENT THIS METHOD if you want to use the plugin with versions of cordova < 2.2.0
- (void) showEmailComposer:(CDVInvokedUrlCommand*)command {
NSDictionary *parameters = [command.arguments objectAtIndex:0];
[self showEmailComposerWithParameters:parameters];
}
-(void) showEmailComposerWithParameters:(NSDictionary*)parameters {
MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
mailComposer.mailComposeDelegate = self;
// set subject
@try {
NSString* subject = [parameters objectForKey:@"subject"];
if (subject) {
[mailComposer setSubject:subject];
}
}
@catch (NSException *exception) {
NSLog(@"EmailComposer - Cannot set subject; error: %@", exception);
}
// set body
@try {
NSString* body = [parameters objectForKey:@"body"];
BOOL isHTML = [[parameters objectForKey:@"bIsHTML"] boolValue];
if(body) {
[mailComposer setMessageBody:body isHTML:isHTML];
}
}
@catch (NSException *exception) {
NSLog(@"EmailComposer - Cannot set body; error: %@", exception);
}
// Set recipients
@try {
NSArray* toRecipientsArray = [parameters objectForKey:@"toRecipients"];
if(toRecipientsArray) {
[mailComposer setToRecipients:toRecipientsArray];
}
}
@catch (NSException *exception) {
NSLog(@"EmailComposer - Cannot set TO recipients; error: %@", exception);
}
@try {
NSArray* ccRecipientsArray = [parameters objectForKey:@"ccRecipients"];
if(ccRecipientsArray) {
[mailComposer setCcRecipients:ccRecipientsArray];
}
}
@catch (NSException *exception) {
NSLog(@"EmailComposer - Cannot set CC recipients; error: %@", exception);
}
@try {
NSArray* bccRecipientsArray = [parameters objectForKey:@"bccRecipients"];
if(bccRecipientsArray) {
[mailComposer setBccRecipients:bccRecipientsArray];
}
}
@catch (NSException *exception) {
NSLog(@"EmailComposer - Cannot set BCC recipients; error: %@", exception);
}
@try {
int counter = 1;
NSArray *attachmentPaths = [parameters objectForKey:@"attachments"];
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]];
}
counter++;
}
@catch (NSException *exception) {
DLog(@"Cannot attach file at path %@; error: %@", path, exception);
}
}
}
}
@catch (NSException *exception) {
NSLog(@"EmailComposer - Cannot set attachments; error: %@", exception);
}
if (mailComposer != nil) {
[self.viewController presentModalViewController:mailComposer animated:YES];
} else {
[self returnWithCode:RETURN_CODE_EMAIL_NOTSENT];
}
[mailComposer release];
}
// Dismisses the email composition interface when users tap Cancel or Send.
// Proceeds to update the message field with the result of the operation.
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
// Notifies users about errors associated with the interface
int webviewResult = 0;
switch (result) {
case MFMailComposeResultCancelled:
webviewResult = RETURN_CODE_EMAIL_CANCELLED;
break;
case MFMailComposeResultSaved:
webviewResult = RETURN_CODE_EMAIL_SAVED;
break;
case MFMailComposeResultSent:
webviewResult =RETURN_CODE_EMAIL_SENT;
break;
case MFMailComposeResultFailed:
webviewResult = RETURN_CODE_EMAIL_FAILED;
break;
default:
webviewResult = RETURN_CODE_EMAIL_NOTSENT;
break;
}
[controller dismissModalViewControllerAnimated:YES];
[self returnWithCode:webviewResult];
}
// Call the callback with the specified code
-(void) returnWithCode:(int)code {
[self writeJavascript:[NSString stringWithFormat:@"window.plugins.emailComposer._didFinishWithResult(%d);", code]];
}
@end

View File

@@ -0,0 +1,42 @@
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
showEmailComposer:(CDVInvokedUrlCommand*)command;
and uncomment the method
showEmailComposer:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
both in EmailComposer.h and EmailComposer.m files
**IMPORTANT:** You will need to add MessageUI.framework to your project if it is not already included.
**IMPORTANT:** by now, you can attach only PDF and IMAGES (the latter will be convertend in PNG format)
- Add the EmailComposer.h EmailComposer.m files to your Plugins Folder.
- Place the EmailComposer.js file somewhere in your www folder, and include it from your html.
- Add to Cordova.plist Plugins: key **EmailComposer** value **EmailComposer**
Callable interface:
window.plugins.emailComposer.showEmailComposerWithCallback(<callback>,<subject>,<body>,<toRecipients>,<ccRecipients>,<bccRecipients>,<isHtml>,<attachments>);
or
window.plugins.emailComposer.showEmailComposer(<subject>,<body>,<toRecipients>,<ccRecipients>,<bccRecipients>,<isHtml>,<attachments>);
**Parameters:**
- <callback>: a js function that will receive return parameter from the plugin
- <subject>: a string representing the subject of the email; can be null
- <body>: a string representing the email body (could be HTML code, in this case set <isHtml> to **true**); can be null
- <toRecipients>: a js array containing all the email addresses for TO field; can be null/empty
- <ccRecipients>: a js array containing all the email addresses for CC field; can be null/empty
- <bccRecipients>: a js array containing all the email addresses for BCC field; can be null/empty
- <isHtml>: a bool value indicating if the body is HTML or plain text
- <attachments>: a js array containing all full paths to the files you want to attach; can be null/empty
**Example**
window.plugins.emailComposer.showEmailComposerWithCallback(function(result){console.log(result);},"Look at this photo","Take a look at <b>this<b/>:",["example@email.com", "johndoe@email.org"],[],[],true,["_complete_path/image.jpg"]);
**Return values**
- 0: email composition cancelled (cancel button pressed and draft not saved)
- 1: email saved (cancel button pressed but draft saved)
- 2: email sent
- 3: send failed
- 4: email not sent (something wrong happened)