Saturday, 8 August 2015

Making the contact list available in your app

Using nativescript, you can access the apis on your device. Today I will be posting screenshots and details on how you can do it on Android. IOS will be much the same, there are many posts found on google on how to do this.

Eventually I hope to post all the code on Github.

The app I am working on has the option for the user to email or sms booklets of exercises to their clients. Added a button next to the email address which the user can tap to access the phone's contacts.

Using a pageview in modal dialog mode, you can display all the contacts with email addresses, the user can do filter searchs and tap on the email address they wish to use. This then updates the email screen.

Similarly for the sms tab, added a button next to the phone number.

The user can enter a phone number from scratch (including the country code).

Alternatively they can pick a phone number straight from their contacts list.

The code for getting the android contacts is as below. I hope to be able to clean it up a bit in the future. I had some crash issues getting this working, it was a bit of a experimental exercise.

Edit : Forgot to mention, you also need to add
<uses-permission android:name="android.permission.READ_CONTACTS"> to your AndroidManifest file.

import application = require("application");

export interface IContact {
    id: string;
    name: string;
    phoneNumber: Array<IContactData>;
    email: Array<IContactData>;

export interface IContactData {
    data: string;
    dataType: string;

// converts number to english equivalent

function translateType(dataType: number) {
    var dataTypeTranslation = "??: ";
    switch (dataType) {
        case 1:
            dataTypeTranslation = "Home: ";
        case 3:
            dataTypeTranslation = "Work: ";
        case 2:
            dataTypeTranslation = "Mobile: ";

    return dataTypeTranslation;

// returns list of contacts - android only, IOS to be added

export function getAndroidContacts(): Array<IContact> {

    var contacts: Array<IContact> = [];
    var contentResolver =;

    // get All email contacts and convert to javascript object
    // note if you do this in the same way as phone numbers, the program crashes without giving a useful error code

    var emailObject: Object = {};
    var emailCursor = contentResolver.query(android.provider.ContactsContract.CommonDataKinds.Email.CONTENT_URI,
        ["data1", "data2", "name_raw_contact_id"], null, null, null);

    if (emailCursor.getCount() > 0) {
        while (emailCursor.moveToNext()) {

            var id = emailCursor.getString(emailCursor.getColumnIndex("name_raw_contact_id"));
            var email = emailCursor.getString(emailCursor.getColumnIndex("data1"));
            var emailType = emailCursor.getInt(emailCursor.getColumnIndex("data2"));

            if (email != "undefined" && email != null) {
                var key = "E" + id;
                if (typeof emailObject[key] == "undefined") {
                    emailObject[key] = [];
                emailObject[key].push({ data: email, dataType: translateType(emailType) });

    // get contacts

    var cursor = contentResolver.query(android.provider.ContactsContract.Contacts.CONTENT_URI, ["name_raw_contact_id", "display_name", "has_phone_number"], null, null, null);
    if (cursor.getCount() > 0) {
        while (cursor.moveToNext()) {
            var contact: IContact =
                    id: cursor.getString(cursor.getColumnIndex("name_raw_contact_id")),
                    name: cursor.getString(cursor.getColumnIndex("display_name")),
                    phoneNumber: [],
                    email: []

            // get corresponding phone numbers

            if (parseInt(cursor.getString(cursor.getColumnIndex("has_phone_number")), 10) > 0) {
                var phoneCursor = contentResolver.query(android.provider.ContactsContract.CommonDataKinds.Phone.CONTENT_URI, ["data1", "data2"],
                    "contact_id = ?", [], null);
                if (phoneCursor.getCount() > 0) {
                    while (phoneCursor.moveToNext()) {
                        var phone = phoneCursor.getString(phoneCursor.getColumnIndex("data1"));
                        var phoneType = phoneCursor.getInt(phoneCursor.getColumnIndex("data2"));

                        if (phone != "undefined" && phone != null) {
                            contact.phoneNumber.push({ data: phone, dataType: translateType(phoneType) });

            // get email

            var key = "E" +;
            if (typeof emailObject[key] != "undefined") {
       = emailObject[key];

            // only return in it has an email or phone
            if (contact.phoneNumber.length > 0 || > 0) {

    return contacts;


No comments:

Post a Comment