Skip to content

Commit

Permalink
Add EmailComposer plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
Jan Christiansen committed Jul 4, 2012
1 parent 6a11345 commit 5d4e057
Show file tree
Hide file tree
Showing 9 changed files with 372 additions and 3 deletions.
26 changes: 26 additions & 0 deletions Wikipedia-iOS.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
objects = {

/* Begin PBXBuildFile section */
01CDA1BF15A4620400A754B9 /* EmailComposer.js in Sources */ = {isa = PBXBuildFile; fileRef = 01CDA1BB15A4620400A754B9 /* EmailComposer.js */; };
01CDA1C015A4620400A754B9 /* EmailComposer.m in Sources */ = {isa = PBXBuildFile; fileRef = 01CDA1BC15A4620400A754B9 /* EmailComposer.m */; };
01CDA1C115A4620400A754B9 /* index.html in Resources */ = {isa = PBXBuildFile; fileRef = 01CDA1BD15A4620400A754B9 /* index.html */; };
01CDA1C215A4620400A754B9 /* readme.md in Resources */ = {isa = PBXBuildFile; fileRef = 01CDA1BE15A4620400A754B9 /* readme.md */; };
0719935714CDDBA700D60D9C /* Security.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0719935614CDDBA700D60D9C /* Security.framework */; };
0719935914CDDBB400D60D9C /* MessageUI.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0719935814CDDBB400D60D9C /* MessageUI.framework */; settings = {ATTRIBUTES = (Required, ); }; };
0719952D14CDEEF500D60D9C /* Twitter.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0719952C14CDEEF500D60D9C /* Twitter.framework */; settings = {ATTRIBUTES = (Weak, ); }; };
Expand Down Expand Up @@ -65,6 +69,11 @@
/* End PBXBuildFile section */

/* Begin PBXFileReference section */
01CDA1BA15A4620400A754B9 /* EmailComposer.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = EmailComposer.h; sourceTree = "<group>"; };
01CDA1BB15A4620400A754B9 /* EmailComposer.js */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.javascript; path = EmailComposer.js; sourceTree = "<group>"; };
01CDA1BC15A4620400A754B9 /* EmailComposer.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = EmailComposer.m; sourceTree = "<group>"; };
01CDA1BD15A4620400A754B9 /* index.html */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.html; path = index.html; sourceTree = "<group>"; };
01CDA1BE15A4620400A754B9 /* readme.md */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = readme.md; sourceTree = "<group>"; };
0719935614CDDBA700D60D9C /* Security.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Security.framework; path = System/Library/Frameworks/Security.framework; sourceTree = SDKROOT; };
0719935814CDDBB400D60D9C /* MessageUI.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = MessageUI.framework; path = System/Library/Frameworks/MessageUI.framework; sourceTree = SDKROOT; };
0719952C14CDEEF500D60D9C /* Twitter.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = Twitter.framework; path = System/Library/Frameworks/Twitter.framework; sourceTree = SDKROOT; };
Expand Down Expand Up @@ -183,6 +192,18 @@
/* End PBXFrameworksBuildPhase section */

/* Begin PBXGroup section */
01CDA1B915A4620400A754B9 /* EmailComposer */ = {
isa = PBXGroup;
children = (
01CDA1BA15A4620400A754B9 /* EmailComposer.h */,
01CDA1BB15A4620400A754B9 /* EmailComposer.js */,
01CDA1BC15A4620400A754B9 /* EmailComposer.m */,
01CDA1BD15A4620400A754B9 /* index.html */,
01CDA1BE15A4620400A754B9 /* readme.md */,
);
path = EmailComposer;
sourceTree = "<group>";
};
6108B3FD1512AF0800779B71 /* Twitter */ = {
isa = PBXGroup;
children = (
Expand Down Expand Up @@ -400,6 +421,7 @@
61368D531517F94F00C4F0B8 /* facebook-ios-sdk */,
6108B3FD1512AF0800779B71 /* Twitter */,
D424DED514C0FAC30086FF61 /* ActionSheet */,
01CDA1B915A4620400A754B9 /* EmailComposer */,
);
path = Plugins;
sourceTree = "<group>";
Expand Down Expand Up @@ -477,6 +499,8 @@
D43186C1152E350C00E19905 /* Default-Landscape@2x~ipad.png in Resources */,
D43186C4152E351300E19905 /* Default-Portrait@2x~ipad.png in Resources */,
99CC918E156D89E2007F84D5 /* Cordova.plist in Resources */,
01CDA1C115A4620400A754B9 /* index.html in Resources */,
01CDA1C215A4620400A754B9 /* readme.md in Resources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -522,6 +546,8 @@
61368D791517F94F00C4F0B8 /* SBJsonWriter.m in Sources */,
61368D7D1517F96F00C4F0B8 /* FacebookConnectPlugin.m in Sources */,
99993890156D803E0048DB2B /* CDVDeprecated.m in Sources */,
01CDA1BF15A4620400A754B9 /* EmailComposer.js in Sources */,
01CDA1C015A4620400A754B9 /* EmailComposer.m in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down
8 changes: 5 additions & 3 deletions Wikipedia-iOS/Cordova.plist
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
<key>OpenAllWhitelistURLsInWebView</key>
<false/>
<key>ExternalHosts</key>
<array>
<array>
<string>ws.geonames.net</string>
<string>*.mqcdn.com</string>
<string>*.wikipedia.org</string>
Expand All @@ -30,7 +30,9 @@
</array>
<key>Plugins</key>
<dict>
<key>ActionSheet</key>
<key>EmailComposer</key>
<string>EmailComposer</string>
<key>ActionSheet</key>
<string>ActionSheet</string>
<key>Compass</key>
<string>CDVLocation</string>
Expand Down Expand Up @@ -60,7 +62,7 @@
<string>CDVSplashScreen</string>
<key>Battery</key>
<string>CDVBattery</string>
<key>com.phonegap.twitter</key>
<key>com.phonegap.twitter</key>
<string>TwitterPlugin</string>
<key>org.apache.cordova.facebook.Connect</key>
<string>FacebookConnectPlugin</string>
Expand Down
25 changes: 25 additions & 0 deletions Wikipedia-iOS/Plugins/EmailComposer/EmailComposer.h
Original file line number Diff line number Diff line change
@@ -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
61 changes: 61 additions & 0 deletions Wikipedia-iOS/Plugins/EmailComposer/EmailComposer.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// 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, "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 = {};
}

// shim to work in 1.5 and 1.6
if (!window.Cordova) {
window.Cordova = cordova;
};

window.plugins.emailComposer = new EmailComposer();
});
106 changes: 106 additions & 0 deletions Wikipedia-iOS/Plugins/EmailComposer/EmailComposer.m
Original file line number Diff line number Diff line change
@@ -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
72 changes: 72 additions & 0 deletions Wikipedia-iOS/Plugins/EmailComposer/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
<!DOCTYPE html>
<html>
<head>
<title></title>

<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no;" />
<meta charset="utf-8">


<!-- iPad/iPhone specific css below, add after your main css >
<link rel="stylesheet" media="only screen and (max-device-width: 1024px)" href="ipad.css" type="text/css" />
<link rel="stylesheet" media="only screen and (max-device-width: 480px)" href="iphone.css" type="text/css" />
-->
<!-- If your application is targeting iOS BEFORE 4.0 you MUST put json2.js from http://www.JSON.org/json2.js into your www directory and include it here -->
<script type="text/javascript" charset="utf-8" src="cordova-1.6.0.js"></script>

<script type="text/javascript" charset="utf-8" src="EmailComposer.js"></script>
<script type="text/javascript">


// If you want to prevent dragging, uncomment this section
/*
function preventBehavior(e)
{
e.preventDefault();
};
document.addEventListener("touchmove", preventBehavior, false);
*/

/* If you are supporting your own protocol, the var invokeString will contain any arguments to the app launch.
see http://iphonedevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html
for more details -jm */
/*
function handleOpenURL(url)
{
// TODO: do something with the url passed in.
}
*/

function onBodyLoad()
{
document.addEventListener("deviceready", onDeviceReady, false);
}

/* When this function is called, Cordova has been initialized and is ready to roll */
/* If you are supporting your own protocol, the var invokeString will contain any arguments to the app launch.
see http://iphonedevelopertips.com/cocoa/launching-your-own-application-via-a-custom-url-scheme.html
for more details -jm */
function onDeviceReady()
{
// do your thing!
navigator.notification.alert("Cordova is working")

var args;
cordova.exec(null, null, "EmailComposer", "showEmailComposer", [args]);



}

</script>
</head>
<body onload="onBodyLoad()">
<h1>Hey, it's Cordova!</h1>
<p>Don't know how to get started? Check out our <em><a target="_blank" href="http://docs.phonegap.com/en/edge/guide_getting-started_ios_index.md.html#Getting%20Started%20with%20iOS">Getting Started Guide</a></em>
<br />
<ol>
<li>Check your console log for any white-list rejection errors.</li>
<li>Add your allowed <strong>hosts</strong> in Cordova.plist/ExternalHosts (wildcards OK, don't enter the URL scheme)</li>
</ol>
</body>
</html>
15 changes: 15 additions & 0 deletions Wikipedia-iOS/Plugins/EmailComposer/readme.md
Original file line number Diff line number Diff line change
@@ -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 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, "EmailComposer", "showEmailComposer", [args]);
Loading

0 comments on commit 5d4e057

Please sign in to comment.