From 86aa6f547ec6d46ab1446eab5cc7b63df9784c20 Mon Sep 17 00:00:00 2001 From: Brian Thurlow Date: Mon, 7 Mar 2016 12:07:43 -0600 Subject: [PATCH 1/9] Implemented iOS MFMessageComposeViewController Supports multiple recipients. Supports Message Body. --- index.ios.js | 70 +++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 56 insertions(+), 14 deletions(-) diff --git a/index.ios.js b/index.ios.js index 580d41e..ef67229 100644 --- a/index.ios.js +++ b/index.ios.js @@ -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://"; @@ -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(!smsNum instanceof Array){ + reject("Numbers are not in an array!"); + } + + 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; From eed1977442ffa0647f3416f7fb496901e0c18379 Mon Sep 17 00:00:00 2001 From: Brian Thurlow Date: Tue, 8 Mar 2016 09:29:30 -0600 Subject: [PATCH 2/9] Increment Version --- package.json | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 4c5a131..2e3e739 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -25,4 +25,4 @@ "ios": "1.0.0" } } -} \ No newline at end of file +} From cd48266dea91a45cf37244c80703ccb3003b713f Mon Sep 17 00:00:00 2001 From: Brian Thurlow Date: Tue, 8 Mar 2016 09:30:45 -0600 Subject: [PATCH 3/9] Update index.android.js Support multiple recipients. --- index.android.js | 31 +++++++++++++++++++------------ 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/index.android.js b/index.android.js index ab97934..a3a8300 100644 --- a/index.android.js +++ b/index.android.js @@ -23,18 +23,25 @@ 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(!smsNum instanceof Array){ + reject("Numbers are not in an array!"); + } + + try { + 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"); + application.android.foregroundActivity.startActivity(intent); + resolve({ + response:"success" + }); + + } catch(ex) { + reject(ex); + } + }); } exports.dial = dial; From 989033b10a5398ed89ad60d98190d141e1a2836a Mon Sep 17 00:00:00 2001 From: Brian Thurlow Date: Tue, 8 Mar 2016 10:12:06 -0600 Subject: [PATCH 4/9] Update index.android.js Added additional response values to match iOS version. --- index.android.js | 36 +++++++++++++++++++++++++++++++----- 1 file changed, 31 insertions(+), 5 deletions(-) diff --git a/index.android.js b/index.android.js index a3a8300..f51efc4 100644 --- a/index.android.js +++ b/index.android.js @@ -29,15 +29,41 @@ function sms(smsNum, messageText) { } 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"); - application.android.foregroundActivity.startActivity(intent); - resolve({ - response:"success" - }); - + + 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); } From 512b352955cfa13f40c942ee409378a6cff20176 Mon Sep 17 00:00:00 2001 From: Brian Thurlow Date: Tue, 8 Mar 2016 10:13:05 -0600 Subject: [PATCH 5/9] Update README.md --- README.md | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 4c7271a..ffc8de0 100644 --- a/README.md +++ b/README.md @@ -43,13 +43,21 @@ 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 numbers to use. +* messageText: String to send. For example, the code below opens the sms app for the provided number: ```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); + } +); ``` From 7229b07c4e806afdbd508b620e25e72099944aea Mon Sep 17 00:00:00 2001 From: Brian Thurlow Date: Tue, 8 Mar 2016 10:14:59 -0600 Subject: [PATCH 6/9] Update Demo App --- demo/app/main-page.js | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/demo/app/main-page.js b/demo/app/main-page.js index c01193a..154ecef 100644 --- a/demo/app/main-page.js +++ b/demo/app/main-page.js @@ -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; \ No newline at end of file +exports.textNumber = textNumber; From 8c203be3553507a7060c6d7c43521bf3ae20914f Mon Sep 17 00:00:00 2001 From: Brian Thurlow Date: Fri, 11 Mar 2016 09:26:05 -0600 Subject: [PATCH 7/9] Update README.md --- README.md | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index ffc8de0..c3f875a 100644 --- a/README.md +++ b/README.md @@ -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. @@ -43,11 +43,29 @@ phone.dial("212-555-1234",false); ``` #### sms: open the OS specific SMS app ##### Parameters -* smsNum: SMS numbers to use. +* 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" ); From 7da17640d227b73b3caf00671a6105b7fc50d3c7 Mon Sep 17 00:00:00 2001 From: Brian Thurlow Date: Fri, 11 Mar 2016 09:28:58 -0600 Subject: [PATCH 8/9] Update index.android.js Add support for previous sms method. --- index.android.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/index.android.js b/index.android.js index f51efc4..eda8bf6 100644 --- a/index.android.js +++ b/index.android.js @@ -24,8 +24,8 @@ function dial(telNum,prompt) { function sms(smsNum, messageText) { return new Promise(function (resolve, reject){ - if(!smsNum instanceof Array){ - reject("Numbers are not in an array!"); + if(!Array.isArray(smsNum)){ + smsNum = [smsNum]; } try { @@ -65,7 +65,7 @@ function sms(smsNum, messageText) { }; application.android.foregroundActivity.startActivityForResult(intent, SEND_SMS); } catch(ex) { - reject(ex); + reject(ex.toString()); } }); } From 66dc688f028ce4131c368e1231a8ec996314567b Mon Sep 17 00:00:00 2001 From: Brian Thurlow Date: Fri, 11 Mar 2016 09:29:22 -0600 Subject: [PATCH 9/9] Update index.ios.js Add support for previous sms method. --- index.ios.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.ios.js b/index.ios.js index ef67229..c87fc51 100644 --- a/index.ios.js +++ b/index.ios.js @@ -56,8 +56,8 @@ function dial(telNum,prompt) { function sms(smsNum, messageText) { return new Promise(function (resolve, reject){ - if(!smsNum instanceof Array){ - reject("Numbers are not in an array!"); + if(!Array.isArray(smsNum)){ + smsNum = [smsNum]; } var page = frameModule.topmost().ios.controller;