简体   繁体   中英

Cordova contact plugin not working

After calling this function I am getting the following error:

"TypeError: Cannot read property 'pickContact' of undefined"

$scope.pickContact = function() {
    navigator.contacts.pickContact(function(contact) {
        if(contact) {
            $scope.requestData.guestName = contact.displayName;
            if(contact.phoneNumbers && contact.phoneNumbers.length > 0) {
                $scope.requestData.phoneNo = contact.phoneNumbers[0].value;
            } else {
                $scope.requestData.phoneNo = null;
            }
            $scope.$apply();
        } else {
            $ionicPopup.alert({
                title: 'Error!',
                template: 'Unable to get contact details'
            });
        }
    }, function(err) {
        console.log('Error: ' + err);
        $ionicPopup.alert({
            title: 'Error!',
            template: 'Unable to get contact details'
        });
    });
};

Use the $cordovaContacts plugin for get contacts and inject the dependency in your controller.

This plugin is available only on devices, not in the browser please do test on device.

For this plugin first you need to install ngCordova , this will support you for many more plugins and implementations.

Install plugin using following command,

cordova plugin add cordova-plugin-contacts

Example :

        .controller('MyCtrl', function($scope, $cordovaContacts, $ionicPlatform) {
          $scope.addContact = function() {
            $cordovaContacts.save($scope.contactForm).then(function(result) {
              // Contact saved
            }, function(err) {
              // Contact error
            });
          };

          $scope.getAllContacts = function() {
            $cordovaContacts.find().then(function(allContacts) { //omitting parameter to .find() causes all contacts to be returned
              $scope.contacts = allContacts;
            }
          };
          $scope.findContactsBySearchTerm = function (searchTerm) {
            var opts = {                                           //search options
              filter : searchTerm,                                 // 'Bob'
              multiple: true,                                      // Yes, return any contact that matches criteria
              fields:  [ 'displayName', 'name' ]                   // These are the fields to search for 'bob'.
              desiredFields: [id];    //return fields.
            };

            if ($ionicPlatform.isAndroid()) {
              opts.hasPhoneNumber = true;         //hasPhoneNumber only works for android.
            };

            $cordovaContacts.find(opts).then(function (contactsFound) {
              $scope.contacts = contactsFound;
            };
          }

          $scope.pickContactUsingNativeUI = function () {
            $cordovaContacts.pickContact().then(function (contactPicked) {
              $scope.contact = contactPicked;
            }
          }

        });

Hope this will help you !!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM