2

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:

  1. Installed PhoneGap
  2. Created PhoneGap project
  3. Contacts pulgin installed for the project
  4. Copied the DrupalGap to the www folder of the project
  5. Done the necessary settings changes
  6. 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<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

0

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).

  • Thank you very much. I will update my question with the solution I did. – San Jan 5 '16 at 12:04
  • You're welcome. If the answer is useful, please mark it as such. – tyler.frankenstein Jan 5 '16 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.