How to access phone contacts from PhoneGap in DrupalGap? I'm really new to DrupalGap and PhoneGap. I have a question, how can we access the phone contacts in DrupalGap? I want to list the mobile numbers and other contact details in the application. Then select some of the contacts and store in the Drupal database.

The steps I followed:


Installed PhoneGap
Created PhoneGap project
Contacts pulgin installed for the project
Copied the DrupalGap to the www folder of the project
Done the necessary settings changes
Created a custom module to access the contacts from PhoneGap in laptop


I found some code snippet to access the contacts in PhoneGap as follows:

function sync_contacts_get_contacts() {

    // specify contact search criteria
    var options = new ContactFindOptions();

    options.filter = "";      // empty search string returns all contacts
    options.multiple = true;  // return multiple results
    filter = ["displayName"]; // return contact.displayName field


    // find contacts
    navigator.contacts.find(filter, onSuccess, onError, options);
}

function onSuccess(contacts) {
    for (var i=0; i&lt;contacts.length; i++) {
        if (contacts[i].displayName) {  // many contacts don't have displayName
            alert(contacts[i].displayName);
        }
    }
};

// onError: Failed to get the contacts
//
function onError(contactError) {
    alert('onError!');
}


How can I access this in DrupalGap module I created and store in Drual database?

SOLUTION:

Later in my research I realized one of my misunderstanding. Contacts, camera etc. are work only in device. That means "navigator.contacts" cannot test in the web browser. we have to build the .apk file and install that in the device and test.

Instead of writing the contacts getting code, we can use the PhoneGap plugin from this location: https://github.com/dbaq/cordova-plugin-contacts-phone-numbers.

Using the custom service I stored the contacts in Drupal database. Link: http://docs.drupalgap.org/7/Services/Create_a_Custom_Service

To create the .apk file we have to follow the URL: http://docs.drupalgap.org/7/Compiling_a_Mobile_Application/Preparing_PhoneGap/Installing_PhoneGap/index
Here's the approach I would take:

1 - Build a custom DrupalGap module.

2 - Implement hook_install() in the module:

/**
 * Implements hook_install().
 */
function my_module_install() {
  sync_contacts_get_contacts();
}


3 - Create a Custom Service Resource Endpoint

4 - In your onSuccess function is where you would send the contacts JSON data up to your Drupal resource.

What (and how) you store the data in Drupal is up to you and your custom Drupal module(s).