Skip to content

Commit 19fe6f9

Browse files
committed
Merge pull request phonegap#901 from guidosabatini/master
New iOS EmailComposer plugin
2 parents ee4eda3 + bc40c51 commit 19fe6f9

File tree

4 files changed

+301
-0
lines changed

4 files changed

+301
-0
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//
2+
// EmailComposer.h
3+
//
4+
//
5+
// Created by Jesse MacFadyen on 10-04-05.
6+
// Copyright 2010 Nitobi. All rights reserved.
7+
//
8+
9+
#import <Foundation/Foundation.h>
10+
#import <MessageUI/MFMailComposeViewController.h>
11+
#import <Cordova/CDVPlugin.h>
12+
13+
14+
@interface EmailComposer : CDVPlugin <MFMailComposeViewControllerDelegate> {
15+
16+
17+
}
18+
19+
// UNCOMMENT THIS METHOD if you want to use the plugin with versions of cordova < 2.2.0
20+
//- (void) showEmailComposer:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
21+
22+
// COMMENT THIS METHOD if you want to use the plugin with versions of cordova < 2.2.0
23+
- (void) showEmailComposer:(CDVInvokedUrlCommand*)command;
24+
25+
@end
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// window.plugins.emailComposer
2+
3+
function EmailComposer() {
4+
this.resultCallback = null; // Function
5+
}
6+
7+
EmailComposer.ComposeResultType = {
8+
Cancelled:0,
9+
Saved:1,
10+
Sent:2,
11+
Failed:3,
12+
NotSent:4
13+
}
14+
15+
16+
17+
// showEmailComposer : all args optional
18+
19+
EmailComposer.prototype.showEmailComposer = function(subject,body,toRecipients,ccRecipients,bccRecipients,bIsHTML,attachments) {
20+
var args = {};
21+
if(toRecipients)
22+
args.toRecipients = toRecipients;
23+
if(ccRecipients)
24+
args.ccRecipients = ccRecipients;
25+
if(bccRecipients)
26+
args.bccRecipients = bccRecipients;
27+
if(subject)
28+
args.subject = subject;
29+
if(body)
30+
args.body = body;
31+
if(bIsHTML)
32+
args.bIsHTML = bIsHTML;
33+
if(attachments)
34+
args.attachments = attachments;
35+
36+
cordova.exec(null, null, "EmailComposer", "showEmailComposer", [args]);
37+
}
38+
39+
EmailComposer.prototype.showEmailComposerWithCallback = function(callback, subject, body, toRecipients, ccRecipients, bccRecipients, isHTML, attachments) {
40+
this.resultCallback = callback;
41+
this.showEmailComposer.apply(this,[subject,body,toRecipients,ccRecipients,bccRecipients,isHTML,attachments]);
42+
}
43+
44+
EmailComposer.prototype._didFinishWithResult = function(res) {
45+
this.resultCallback(res);
46+
}
47+
48+
cordova.addConstructor(function() {
49+
if(!window.plugins)
50+
{
51+
window.plugins = {};
52+
}
53+
54+
// shim to work in 1.5 and 1.6
55+
if (!window.Cordova) {
56+
window.Cordova = cordova;
57+
};
58+
59+
window.plugins.emailComposer = new EmailComposer();
60+
});
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
//
2+
// EmailComposer.m
3+
//
4+
//
5+
// Created by Jesse MacFadyen on 10-04-05.
6+
// Copyright 2010 Nitobi. All rights reserved.
7+
//
8+
9+
#define RETURN_CODE_EMAIL_CANCELLED 0
10+
#define RETURN_CODE_EMAIL_SAVED 1
11+
#define RETURN_CODE_EMAIL_SENT 2
12+
#define RETURN_CODE_EMAIL_FAILED 3
13+
#define RETURN_CODE_EMAIL_NOTSENT 4
14+
15+
#import "EmailComposer.h"
16+
17+
@interface EmailComposer ()
18+
19+
-(void) showEmailComposerWithParameters:(NSDictionary*)parameters;
20+
-(void) returnWithCode:(int)code;
21+
22+
@end
23+
24+
@implementation EmailComposer
25+
26+
// UNCOMMENT THIS METHOD if you want to use the plugin with versions of cordova < 2.2.0
27+
//- (void) showEmailComposer:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options {
28+
// NSDictionary *parameters = [NSDictionary dictionaryWithObjectsAndKeys:
29+
// [options valueForKey:@"toRecipients"], @"toRecipients",
30+
// [options valueForKey:@"ccRecipients"], @"ccRecipients",
31+
// [options valueForKey:@"bccRecipients"], @"bccRecipients",
32+
// [options valueForKey:@"subject"], @"subject",
33+
// [options valueForKey:@"body"], @"body",
34+
// [options valueForKey:@"bIsHTML"], @"bIsHTML",
35+
// [options valueForKey:@"attachments"], @"attachments",
36+
// nil];
37+
// [self showEmailComposerWithParameters:parameters];
38+
//}
39+
40+
// COMMENT THIS METHOD if you want to use the plugin with versions of cordova < 2.2.0
41+
- (void) showEmailComposer:(CDVInvokedUrlCommand*)command {
42+
NSDictionary *parameters = [command.arguments objectAtIndex:0];
43+
[self showEmailComposerWithParameters:parameters];
44+
}
45+
46+
-(void) showEmailComposerWithParameters:(NSDictionary*)parameters {
47+
48+
MFMailComposeViewController *mailComposer = [[MFMailComposeViewController alloc] init];
49+
mailComposer.mailComposeDelegate = self;
50+
51+
// set subject
52+
@try {
53+
NSString* subject = [parameters objectForKey:@"subject"];
54+
if (subject) {
55+
[mailComposer setSubject:subject];
56+
}
57+
}
58+
@catch (NSException *exception) {
59+
NSLog(@"EmailComposer - Cannot set subject; error: %@", exception);
60+
}
61+
62+
// set body
63+
@try {
64+
NSString* body = [parameters objectForKey:@"body"];
65+
BOOL isHTML = [[parameters objectForKey:@"bIsHTML"] boolValue];
66+
if(body) {
67+
[mailComposer setMessageBody:body isHTML:isHTML];
68+
}
69+
}
70+
@catch (NSException *exception) {
71+
NSLog(@"EmailComposer - Cannot set body; error: %@", exception);
72+
}
73+
74+
// Set recipients
75+
@try {
76+
NSArray* toRecipientsArray = [parameters objectForKey:@"toRecipients"];
77+
if(toRecipientsArray) {
78+
[mailComposer setToRecipients:toRecipientsArray];
79+
}
80+
}
81+
@catch (NSException *exception) {
82+
NSLog(@"EmailComposer - Cannot set TO recipients; error: %@", exception);
83+
}
84+
85+
@try {
86+
NSArray* ccRecipientsArray = [parameters objectForKey:@"ccRecipients"];
87+
if(ccRecipientsArray) {
88+
[mailComposer setCcRecipients:ccRecipientsArray];
89+
}
90+
}
91+
@catch (NSException *exception) {
92+
NSLog(@"EmailComposer - Cannot set CC recipients; error: %@", exception);
93+
}
94+
95+
@try {
96+
NSArray* bccRecipientsArray = [parameters objectForKey:@"bccRecipients"];
97+
if(bccRecipientsArray) {
98+
[mailComposer setBccRecipients:bccRecipientsArray];
99+
}
100+
}
101+
@catch (NSException *exception) {
102+
NSLog(@"EmailComposer - Cannot set BCC recipients; error: %@", exception);
103+
}
104+
105+
@try {
106+
int counter = 1;
107+
NSArray *attachmentPaths = [parameters objectForKey:@"attachments"];
108+
if (attachmentPaths) {
109+
for (NSString* path in attachmentPaths) {
110+
@try {
111+
if ([path hasSuffix:@".pdf"]) {
112+
NSData *data = [[NSFileManager defaultManager] contentsAtPath:path];
113+
[mailComposer addAttachmentData:data mimeType:@"application/pdf" fileName:[NSString stringWithFormat:@"attachment%d.pdf", counter]];
114+
} else {
115+
// supposed image
116+
UIImage *image = [UIImage imageWithContentsOfFile:path];
117+
NSData *data = UIImagePNGRepresentation(image);
118+
[mailComposer addAttachmentData:data mimeType:@"image/png" fileName:[NSString stringWithFormat:@"attachment%d.png", counter]];
119+
}
120+
counter++;
121+
}
122+
@catch (NSException *exception) {
123+
DLog(@"Cannot attach file at path %@; error: %@", path, exception);
124+
}
125+
}
126+
}
127+
}
128+
@catch (NSException *exception) {
129+
NSLog(@"EmailComposer - Cannot set attachments; error: %@", exception);
130+
}
131+
132+
if (mailComposer != nil) {
133+
[self.viewController presentModalViewController:mailComposer animated:YES];
134+
} else {
135+
[self returnWithCode:RETURN_CODE_EMAIL_NOTSENT];
136+
}
137+
[mailComposer release];
138+
}
139+
140+
141+
// Dismisses the email composition interface when users tap Cancel or Send.
142+
// Proceeds to update the message field with the result of the operation.
143+
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error {
144+
// Notifies users about errors associated with the interface
145+
int webviewResult = 0;
146+
147+
switch (result) {
148+
case MFMailComposeResultCancelled:
149+
webviewResult = RETURN_CODE_EMAIL_CANCELLED;
150+
break;
151+
case MFMailComposeResultSaved:
152+
webviewResult = RETURN_CODE_EMAIL_SAVED;
153+
break;
154+
case MFMailComposeResultSent:
155+
webviewResult =RETURN_CODE_EMAIL_SENT;
156+
break;
157+
case MFMailComposeResultFailed:
158+
webviewResult = RETURN_CODE_EMAIL_FAILED;
159+
break;
160+
default:
161+
webviewResult = RETURN_CODE_EMAIL_NOTSENT;
162+
break;
163+
}
164+
165+
[controller dismissModalViewControllerAnimated:YES];
166+
[self returnWithCode:webviewResult];
167+
}
168+
169+
// Call the callback with the specified code
170+
-(void) returnWithCode:(int)code {
171+
[self writeJavascript:[NSString stringWithFormat:@"window.plugins.emailComposer._didFinishWithResult(%d);", code]];
172+
}
173+
174+
@end
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
This is a modification of the EmailComposer iOS plugin made by **Randy McMillan**
2+
In this version of the plugin, you can attach images and PDF files to emails. A little refactoring was made.
3+
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
4+
showEmailComposer:(CDVInvokedUrlCommand*)command;
5+
and uncomment the method
6+
showEmailComposer:(NSMutableArray*)arguments withDict:(NSMutableDictionary*)options;
7+
both in EmailComposer.h and EmailComposer.m files
8+
9+
**IMPORTANT:** You will need to add MessageUI.framework to your project if it is not already included.
10+
**IMPORTANT:** by now, you can attach only PDF and IMAGES (the latter will be convertend in PNG format)
11+
12+
- Add the EmailComposer.h EmailComposer.m files to your Plugins Folder.
13+
14+
- Place the EmailComposer.js file somewhere in your www folder, and include it from your html.
15+
16+
- Add to Cordova.plist Plugins: key **EmailComposer** value **EmailComposer**
17+
18+
Callable interface:
19+
window.plugins.emailComposer.showEmailComposerWithCallback(<callback>,<subject>,<body>,<toRecipients>,<ccRecipients>,<bccRecipients>,<isHtml>,<attachments>);
20+
21+
or
22+
window.plugins.emailComposer.showEmailComposer(<subject>,<body>,<toRecipients>,<ccRecipients>,<bccRecipients>,<isHtml>,<attachments>);
23+
24+
**Parameters:**
25+
- <callback>: a js function that will receive return parameter from the plugin
26+
- <subject>: a string representing the subject of the email; can be null
27+
- <body>: a string representing the email body (could be HTML code, in this case set <isHtml> to **true**); can be null
28+
- <toRecipients>: a js array containing all the email addresses for TO field; can be null/empty
29+
- <ccRecipients>: a js array containing all the email addresses for CC field; can be null/empty
30+
- <bccRecipients>: a js array containing all the email addresses for BCC field; can be null/empty
31+
- <isHtml>: a bool value indicating if the body is HTML or plain text
32+
- <attachments>: a js array containing all full paths to the files you want to attach; can be null/empty
33+
34+
**Example**
35+
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"]);
36+
37+
**Return values**
38+
- 0: email composition cancelled (cancel button pressed and draft not saved)
39+
- 1: email saved (cancel button pressed but draft saved)
40+
- 2: email sent
41+
- 3: send failed
42+
- 4: email not sent (something wrong happened)

0 commit comments

Comments
 (0)