Skip to content

Commit

Permalink
Merge pull request #4 from bthurlow/master
Browse files Browse the repository at this point in the history
Improved SMS support
  • Loading branch information
msywensky committed Mar 11, 2016
2 parents f90ec0d + 759f59e commit 9f12ef4
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 35 deletions.
34 changes: 30 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ To dial the phone without user interaction on Android your app must request perm
To use the phone module you must first `require()` it from your project's `node_modules` directory:

```js
var phone = require( "nativescript-phone/phone" );
var phone = require( "nativescript-phone" );
```

After you have a reference to the module you can then call the available methods.
Expand All @@ -43,13 +43,39 @@ phone.dial("212-555-1234",false);
```
#### sms: open the OS specific SMS app
##### Parameters
* smsNum: SMS number to use.
* messageText: String to send. - Not supported on iOS at this time.
* smsNum: SMS number or numbers to use.
* messageText: String to send.

For example, the code below opens the sms app for the provided number:

Send to one number (provided for backwards compatibility)

```js
// my-page.js
var phone = require( "nativescript-phone" );
phone.sms("212-555-1234","My Message") //New Method for single number is phone.sms(["212-555-1234"],"My Message")
.then(function(args){
/// args.reponse: "success", "cancelled", "failed"
console.log(JSON.stringify(args));
},
function(err){
console.log("Error: " + err);
}
);
```

Send to multiple numbers

```js
// my-page.js
var phone = require( "nativescript-phone" );
phone.sms("212-555-1234","My message");
phone.sms(["212-555-1234","212-555-1245"],"My Message")
.then(function(args){
/// args.reponse: "success", "cancelled", "failed"
console.log(JSON.stringify(args));
},
function(err){
console.log("Error: " + err);
}
);
```
14 changes: 11 additions & 3 deletions demo/app/main-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,18 @@ function callNumber() {
}

function textNumber() {
var number = vm.get("number");
phone.sms(number,"testing");
var number = vm.get("number");
phone.sms([number],"testing")
.then(function(args){
/// args.reponse: "success", "cancelled", "failed"
console.log(JSON.stringify(args));
},
function(err){
console.log("Error: " + err);
}
);
}

exports.pageLoaded = pageLoaded;
exports.callNumber = callNumber;
exports.textNumber = textNumber;
exports.textNumber = textNumber;
57 changes: 45 additions & 12 deletions index.android.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,51 @@ function dial(telNum,prompt) {
}

function sms(smsNum, messageText) {
try {
var intent = new android.content.Intent(android.content.Intent.ACTION_VIEW);
intent.setData(android.net.Uri.parse("sms:" + smsNum));
intent.putExtra("sms_body", messageText);
application.android.foregroundActivity.startActivity(intent);
return true;

} catch(ex) {
//alert("Unable to open sms");
//console.log("phone.sms failed: " + ex);
return false;
}
return new Promise(function (resolve, reject){
if(!Array.isArray(smsNum)){
smsNum = [smsNum];
}

try {
var SEND_SMS = 1001;
var intent = new android.content.Intent(android.content.Intent.ACTION_VIEW);
intent.putExtra("address", smsNum.join(";"));
intent.putExtra("sms_body", messageText);
intent.setType("vnd.android-dir/mms-sms");

var previousResult = application.android.onActivityResult;
application.android.onActivityResult = function(requestCode, resultCode, data) {
switch (requestCode) {
case SEND_SMS:
application.android.onActivityResult = previousResult;
if (resultCode === android.app.Activity.RESULT_OK){
return resolve({
response:"success"
});
}
else if (resultCode === android.app.Activity.RESULT_CANCELED){
return resolve({
response:"cancelled"
});
}
else {
return resolve({
response:"failed"
});
}
break;
default:
if (typeof previousResult === 'function') {
previousResult(requestCode, resultCode, data);
}
break;
}
};
application.android.foregroundActivity.startActivityForResult(intent, SEND_SMS);
} catch(ex) {
reject(ex.toString());
}
});
}

exports.dial = dial;
Expand Down
70 changes: 56 additions & 14 deletions index.ios.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,38 @@
var frameModule = require("ui/frame");

var CustomMFMessageComposeViewControllerDelegate = NSObject.extend({
initWithResolveReject: function(resolve, reject) {
var self = this.super.init();
if(self) {
this.resolve = resolve;
this.reject = reject;
}
return self;
},
messageComposeViewControllerDidFinishWithResult: function(controller, result){
controller.dismissModalViewControllerAnimated(true);

if(result === MessageComposeResultCancelled){
this.resolve({
response:"cancelled"
});
}
else if(result === MessageComposeResultSent){
this.resolve({
response:"success"
});
}
else{
this.resolve({
response:"failed"
});
}
CFRelease(controller.messageComposeDelegate);
}
}, {
name: "CustomMFMessageComposeViewControllerDelegate",
protocols: [MFMessageComposeViewControllerDelegate]
});

function dial(telNum,prompt) {
var sURL = "tel://";
Expand All @@ -20,20 +55,27 @@ function dial(telNum,prompt) {
}

function sms(smsNum, messageText) {
var sURL = "sms:" + smsNum;

var url = NSURL.URLWithString(sURL);
var a = UIApplication.sharedApplication();

if (a.canOpenURL(url)) {
a.openURL(url);
return true;
} else {
//alert("Unable to open sms");
//console.log("phone.sms failed");
return false;
}

return new Promise(function (resolve, reject){
if(!Array.isArray(smsNum)){
smsNum = [smsNum];
}

var page = frameModule.topmost().ios.controller;
var controller = MFMessageComposeViewController.alloc().init();
var delegate = CustomMFMessageComposeViewControllerDelegate.alloc().initWithResolveReject(resolve, reject);

CFRetain(delegate);
controller.messageComposeDelegate = delegate;

if(MFMessageComposeViewController.canSendText()){
controller.body = messageText;
controller.recipients = smsNum;
page.presentModalViewControllerAnimated(controller, true);
}
else{
reject("Cannot Send SMS!");
}
});
}

exports.dial = dial;
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "nativescript-phone",
"version": "1.0.0",
"version": "1.1.0",
"description": "A phone NativeScript module for Android and iOS",
"main": "index.js",
"repository": {
Expand All @@ -25,4 +25,4 @@
"ios": "1.0.0"
}
}
}
}

0 comments on commit 9f12ef4

Please sign in to comment.