Email Composer Cordova 1.5 support

This commit is contained in:
Randy McMillan
2012-03-08 22:03:12 -05:00
parent 9ab71295e5
commit 0d9011d616
4 changed files with 201 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>
#ifdef CORDOVA_FRAMEWORK
#import <Cordova/CDVPlugin.h>
#else
#import "CDVPlugin.h"
#endif
@interface EmailComposer : CDVPlugin < MFMailComposeViewControllerDelegate > {
}
- (void) showEmailComposer:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
@end

View File

@@ -0,0 +1,55 @@
// 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) {
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;
Cordova.exec(null, null, "org.apache.cordova.emailComposer", "showEmailComposer", [args]);
}
// this will be forever known as the orch-func -jm
EmailComposer.prototype.showEmailComposerWithCB = function(cbFunction,subject,body,toRecipients,ccRecipients,bccRecipients,bIsHTML) {
this.resultCallback = cbFunction;
this.showEmailComposer.apply(this,[subject,body,toRecipients,ccRecipients,bccRecipients,bIsHTML]);
}
EmailComposer.prototype._didFinishWithResult = function(res) {
this.resultCallback(res);
}
Cordova.addConstructor(function() {
if(!window.plugins)
{
window.plugins = {};
}
window.plugins.emailComposer = new EmailComposer();
});

106
iOS/EmailComposer/EmailComposer.m Executable file
View File

@@ -0,0 +1,106 @@
//
// EmailComposer.m
//
//
// Created by Jesse MacFadyen on 10-04-05.
// Copyright 2010 Nitobi. All rights reserved.
//
#import "EmailComposer.h"
@implementation EmailComposer
- (void) showEmailComposer:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options
{
// NSUInteger argc = [arguments count];
NSString* toRecipientsString = [options valueForKey:@"toRecipients"];
NSString* ccRecipientsString = [options valueForKey:@"ccRecipients"];
NSString* bccRecipientsString = [options valueForKey:@"bccRecipients"];
NSString* subject = [options valueForKey:@"subject"];
NSString* body = [options valueForKey:@"body"];
NSString* isHTML = [options valueForKey:@"bIsHTML"];
MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
picker.mailComposeDelegate = self;
// Set subject
if(subject != nil)
[picker setSubject:subject];
// set body
if(body != nil)
{
if(isHTML != nil && [isHTML boolValue])
{
[picker setMessageBody:body isHTML:YES];
}
else
{
[picker setMessageBody:body isHTML:NO];
}
}
// Set recipients
if(toRecipientsString != nil)
{
[picker setToRecipients:[ toRecipientsString componentsSeparatedByString:@","]];
}
if(ccRecipientsString != nil)
{
[picker setCcRecipients:[ ccRecipientsString componentsSeparatedByString:@","]];
}
if(bccRecipientsString != nil)
{
[picker setBccRecipients:[ bccRecipientsString componentsSeparatedByString:@","]];
}
// Attach an image to the email
// NSString *path = [[NSBundle mainBundle] pathForResource:@"rainy" ofType:@"png"];
// NSData *myData = [NSData dataWithContentsOfFile:path];
// [picker addAttachmentData:myData mimeType:@"image/png" fileName:@"rainy"];
if (picker != nil) {
[self.viewController presentModalViewController:picker animated:YES];
}
[picker 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 = 0;
break;
case MFMailComposeResultSaved:
webviewResult = 1;
break;
case MFMailComposeResultSent:
webviewResult =2;
break;
case MFMailComposeResultFailed:
webviewResult = 3;
break;
default:
webviewResult = 4;
break;
}
[self.viewController dismissModalViewControllerAnimated:YES];
NSString* jsString = [[NSString alloc] initWithFormat:@"window.plugins.emailComposer._didFinishWithResult(%d);",webviewResult];
[self writeJavascript:jsString];
[jsString release];
}
@end

15
iOS/EmailComposer/readme.md Executable file
View File

@@ -0,0 +1,15 @@
Added Cordova 1.5 support March 2012 - @RandyMcMillan
• You will need to add MessageUI.framework to your project if it is not already included.
• Just add the EmailComposer.h EmailComposer.m files to your Plugins Folder.
• Place the EmailComposer.js file in your app root, and include it from your html.
• Add to Cordova.plist Plugins: key org.apache.cordova.emailComposer value EmailComposer
• This is intended to also demonstrate how to pass arguments to native code using the options/map object.
• Please review the js file to understand the interface you can call, and reply with any questions.
Cordova.exec(null, null, "org.apache.cordova.emailComposer", "showEmailComposer", [args]);