Skip to content

Commit db93ea6

Browse files
author
Grant Sanders
committed
added plugin for composing, addressing and sending text messages from inside an app.
1 parent 072a8a0 commit db93ea6

File tree

5 files changed

+267
-0
lines changed

5 files changed

+267
-0
lines changed

iPhone/SMSComposer/README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# PhoneGap SMSComposer #
2+
by Grant Sanders
3+
4+
## Adding the Plugin to your project ##
5+
6+
Using this plugin requires [PhoneGap for iPhone](http://github.com/phonegap/phonegap-iphone).
7+
8+
1. Add the SMSComposer.h and SMSComposer.m files to your "Plugins" folder in your PhoneGap project
9+
2. Add the SMSComposer.js files to your "www" folder on disk, and add a reference to the .js file after phonegap.js.
10+
11+
## RELEASE NOTES ##
12+
13+
### 201101112 ###
14+
* Initial release
15+
* Adds SMS text message composition in-app.
16+
* Requires iOS 4.0 or higher.
17+
Attempts to compose SMS text without running 4.0+ fails gracefully with a friendly message.
18+
19+
## EXAMPLE USAGE ##
20+
21+
* All parameters are optional.
22+
window.plugins.smsComposer.showSMSComposer();
23+
24+
25+
* Passing phone number and message.
26+
window.plugins.smsComposer.showSMSComposer('3424221122', 'hello');
27+
28+
* Multiple recipents are separated by comma(s).
29+
window.plugins.smsComposer.showSMSComposer('3424221122,2134463330', 'hello');
30+
31+
32+
* showSMSComposerWithCB takes a callback as its first parameter.
33+
* 0, 1, 2, or 3 will be passed to the callback when the text message has been attempted.
34+
35+
window.plugins.smsComposer.showSMSComposerWithCB(function(result){
36+
37+
if(result == 0)
38+
alert("Cancelled");
39+
else if(result == 1)
40+
alert("Sent");
41+
else if(result == 2)
42+
alert("Failed.");
43+
else if(result == 3)
44+
alert("Not Sent.");
45+
46+
},'3424221122,2134463330', 'hello');
47+
48+
49+
* A fully working example as index.html has been added to this repository.
50+
* It is an example of what your www/index.html could look like.

iPhone/SMSComposer/SMSComposer.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
//
2+
// SMSComposer.h
3+
//
4+
// Created by Grant Sanders on 12/25/2010.
5+
6+
7+
#import <Foundation/Foundation.h>
8+
#import "PhoneGapCommand.h"
9+
@interface SMSComposer : PhoneGapCommand {
10+
}
11+
12+
- (void)showSMSComposer:(NSArray*)arguments withDict:(NSDictionary*)options;
13+
@end

iPhone/SMSComposer/SMSComposer.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
/**
2+
* Clipboard plugin for PhoneGap
3+
* window.plugins.SMSComposer
4+
*
5+
* @constructor
6+
*/
7+
function SMSComposer()
8+
{
9+
this.resultCallback = null;
10+
}
11+
12+
SMSComposer.ComposeResultType =
13+
{
14+
Cancelled:0,
15+
Sent:1,
16+
Failed:2,
17+
NotSent:3
18+
}
19+
20+
SMSComposer.prototype.showSMSComposer = function(toRecipients, body)
21+
{
22+
23+
var args = {};
24+
25+
if(toRecipients)
26+
args.toRecipients = toRecipients;
27+
28+
if(body)
29+
args.body = body;
30+
31+
PhoneGap.exec("SMSComposer.showSMSComposer",args);
32+
}
33+
34+
SMSComposer.prototype.showSMSComposerWithCB = function(cbFunction,toRecipients,body)
35+
{
36+
this.resultCallback = cbFunction;
37+
this.showSMSComposer.apply(this,[toRecipients,body]);
38+
}
39+
40+
SMSComposer.prototype._didFinishWithResult = function(res)
41+
{
42+
this.resultCallback(res);
43+
}
44+
45+
PhoneGap.addConstructor(function() {
46+
47+
if(!window.plugins) {
48+
window.plugins = {};
49+
}
50+
window.plugins.smsComposer = new SMSComposer();
51+
});

iPhone/SMSComposer/SMSComposer.m

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
//
2+
// ClipboardPlugin.m
3+
// Clipboard plugin for PhoneGap
4+
//
5+
// Created by Grant Sanders on 12/25/2010.
6+
//
7+
8+
#import "SMSComposer.h"
9+
#import <MessageUI/MessageUI.h>
10+
#import <MessageUI/MFMessageComposeViewController.h>
11+
12+
@implementation SMSComposer
13+
14+
- (void)showSMSComposer:(NSArray*)arguments withDict:(NSDictionary*)options
15+
{
16+
17+
Class messageClass = (NSClassFromString(@"MFMessageComposeViewController"));
18+
if (messageClass != nil) {
19+
20+
if (![messageClass canSendText]) {
21+
22+
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Notice" message:@"SMS Text not available."
23+
delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
24+
[alert show];
25+
[alert release];
26+
return;
27+
}
28+
29+
} else {
30+
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Notice" message:@"SMS Text not available."
31+
delegate:self cancelButtonTitle:@"OK" otherButtonTitles: nil];
32+
[alert show];
33+
[alert release];
34+
return;
35+
}
36+
37+
38+
NSString* body = [options valueForKey:@"body"];
39+
NSString* toRecipientsString = [options valueForKey:@"toRecipients"];
40+
41+
MFMessageComposeViewController *picker = [[MFMessageComposeViewController alloc] init];
42+
picker.messageComposeDelegate = self;
43+
44+
if(body != nil)
45+
picker.body = [options valueForKey:@"body"];
46+
47+
if(toRecipientsString != nil)
48+
[picker setRecipients:[ toRecipientsString componentsSeparatedByString:@","]];
49+
50+
[[ super appViewController ] presentModalViewController:picker animated:YES];
51+
[picker release];
52+
53+
}
54+
55+
// Dismisses the composition interface when users tap Cancel or Send. Proceeds to update the message field with the result of the operation.
56+
- (void)messageComposeViewController:(MFMessageComposeViewController *)controller didFinishWithResult:(MessageComposeResult)result
57+
{
58+
// Notifies users about errors associated with the interface
59+
int webviewResult = 0;
60+
61+
switch (result)
62+
{
63+
case MessageComposeResultCancelled:
64+
webviewResult = 0;
65+
break;
66+
case MessageComposeResultSent:
67+
webviewResult = 1;
68+
break;
69+
case MessageComposeResultFailed:
70+
webviewResult = 2;
71+
break;
72+
default:
73+
webviewResult = 3;
74+
break;
75+
}
76+
77+
[[ super appViewController ] dismissModalViewControllerAnimated:YES];
78+
79+
NSString* jsString = [[NSString alloc] initWithFormat:@"window.plugins.smsComposer._didFinishWithResult(%d);",webviewResult];
80+
[self writeJavascript:jsString];
81+
[jsString release];
82+
83+
}
84+
85+
@end

iPhone/SMSComposer/index.html

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
2+
<html>
3+
<head>
4+
<meta name="viewport" content="width=default-width; user-scalable=no" />
5+
<meta http-equiv="Content-type" content="text/html; charset=utf-8">
6+
7+
<script type="text/javascript" charset="utf-8" src="phonegap.js"></script>
8+
9+
<script type="text/javascript" charset="utf-8" src="SMSComposer.js"></script>
10+
<script type="text/javascript" charset="utf-8">
11+
12+
function onBodyLoad()
13+
{
14+
document.addEventListener("deviceready",onDeviceReady,false);
15+
}
16+
function onDeviceReady()
17+
{
18+
viewUpdate();
19+
}
20+
</script>
21+
</head>
22+
<body onload="onBodyLoad()">
23+
24+
25+
<input onclick="ComposeSMS();" type="button" value="Compose SMS text with number and body" /><br />
26+
<input onclick="ComposeSMS2();" type="button" value="Compose SMS text with no parameters" /><br />
27+
<input onclick="ComposeSMS3();" type="button" value="Compose SMS text to multiple recipients" /><br />
28+
<input onclick="ComposeSMSWithCallback();" type="button" value="Compose SMS text with callback" />
29+
30+
31+
<script>
32+
33+
var ComposeSMS = function(){
34+
35+
window.plugins.smsComposer.showSMSComposer('3424221122', 'hello');
36+
}
37+
38+
var ComposeSMS2 = function(){
39+
40+
window.plugins.smsComposer.showSMSComposer();
41+
}
42+
43+
var ComposeSMS3 = function(){
44+
45+
window.plugins.smsComposer.showSMSComposer('3424221122,2134463330', 'hello');
46+
}
47+
48+
var ComposeSMSWithCallback = function(){
49+
50+
window.plugins.smsComposer.showSMSComposerWithCB(myCallback,'3424221122,2134463330', 'hello');
51+
}
52+
53+
var myCallback = function(result){
54+
55+
if(result == 0)
56+
alert("Cancelled");
57+
else if(result == 1)
58+
alert("Sent");
59+
else if(result == 2)
60+
alert("Failed.");
61+
else if(result == 3)
62+
alert("Not Sent.");
63+
}
64+
65+
</script>
66+
</body>
67+
</html>
68+

0 commit comments

Comments
 (0)