diff --git a/iOS/EmailComposer/EmailComposer.h b/iOS/EmailComposer/EmailComposer.h new file mode 100755 index 0000000..b87f4e3 --- /dev/null +++ b/iOS/EmailComposer/EmailComposer.h @@ -0,0 +1,25 @@ +// +// EmailComposer.h +// +// +// Created by Jesse MacFadyen on 10-04-05. +// Copyright 2010 Nitobi. All rights reserved. +// + +#import +#import +#ifdef CORDOVA_FRAMEWORK +#import +#else +#import "CDVPlugin.h" +#endif + + +@interface EmailComposer : CDVPlugin < MFMailComposeViewControllerDelegate > { + + +} + +- (void) showEmailComposer:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options; + +@end \ No newline at end of file diff --git a/iOS/EmailComposer/EmailComposer.js b/iOS/EmailComposer/EmailComposer.js new file mode 100755 index 0000000..30ee408 --- /dev/null +++ b/iOS/EmailComposer/EmailComposer.js @@ -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(); +}); \ No newline at end of file diff --git a/iOS/EmailComposer/EmailComposer.m b/iOS/EmailComposer/EmailComposer.m new file mode 100755 index 0000000..ed775b0 --- /dev/null +++ b/iOS/EmailComposer/EmailComposer.m @@ -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 \ No newline at end of file diff --git a/iOS/EmailComposer/readme.md b/iOS/EmailComposer/readme.md new file mode 100755 index 0000000..c66c3b7 --- /dev/null +++ b/iOS/EmailComposer/readme.md @@ -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]);