Git Product home page Git Product logo

nativescript-phone's Introduction

npm npm

NativeScript Phone

NativeScript plugin to use the device phone and SMS features for Android and iOS

Native Info

Installation

Install the plugin using the NativeScript CLI tooling

tns plugin add nativescript-phone

Video Tutorial

egghead plugin lesson @ https://egghead.io/lessons/javascript-using-the-device-phone-and-sms-with-nativescript

Android

To dial the phone without user interaction on Android your app must request permission to dial. The following must be in your app's AndroidManifest.xml.

<uses-permission android:name="android.permission.CALL_PHONE" />

IOS

You must add the following line to your project's Info.plist

<key>LSApplicationQueriesSchemes</key>
<array>
   <string>tel</string>
   <string>telprompt</string>
</array>

Usage

To use the phone module you must first require() it from your project's node_modules directory:

var phone = require('nativescript-phone');

After you have a reference to the module you can then call the available methods.

Methods

dial: initiate a phone call

Parameters
  • telNum: Phone number to dial.
  • prompt: Boolean to enable OS specific confirmation before dialing.

For example, the code below dials the number without showing the device specific confirmation prompt:

// my-page.js
var phone = require('nativescript-phone');
phone.dial('212-555-1234', false);

sms: open the OS specific SMS app

Parameters
  • 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)

// 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

// my-page.js
var phone = require('nativescript-phone');
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);
  }
);

requestCallPermission: Request Android Call_Phone Permission

Parameters

  • explanation: The explanation text if the user denies permission twice (nullable). If you attempt to use dial("122929912", false) to not prompt on android 6.0, nothing will happen unless permission has been approved before. When this method is executed, a check for permissions happens, and a promise is returned. If the user refuse it, you can handle it via the catch method of promise. If it accepts you can dial in the thenmethod. You should so "wrap" your dial method inside of the requestCallPermission() method (see following example).

TypeScript example

import * as TNSPhone from 'nativescript-phone';

/// Dial a phone number.
public callHome() {
   const phoneNumber = '415-123-4567';
   TNSPhone.requestCallPermission('You should accept the permission to be able to make a direct phone call.')
      .then(() => TNSPhone.dial(phoneNumber, false))
      .catch(() => TNSPhone.dial(phoneNumber, true));
}

// Text a number (or multiple numbers)
public messageParents() {
    TNSPhone.sms(['212-555-1234', '212-555-0987'], "Text till your fingers bleed")
        .then((args) => {
            console.log(JSON.stringify(args));
        }, (err) => {
            console.log('Error: ' + err);
        })
}

nativescript-phone's People

Contributors

bradmartin avatar bthurlow avatar dlcole avatar domgaud avatar kielsoft avatar mathieutu avatar msc-miguel avatar msywensky avatar rajivnarayana avatar tgpetrov avatar witi83 avatar yezarela avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.