Search code examples
cordovahandlebars.jscordova-pluginsandroid-contacts

How can I save Mobile Numbers to contacts using Cordova


I have Directory displaying some information like Name and associated phone numbers when click on that it will go to phone call page(Native). How can I save that name and phone number to contacts using cordova

Homview.js

var HomeView = function (service) {

    var employeeListView;

    this.initialize = function() {
        this.$el = $('<div/>');
        this.$el.on('keyup', '.search-key', this.findByName);
        employeeListView = new EmployeeListView();
        this.render();
    };

    this.render = function() {
        this.$el.html(this.template());
        $('.content', this.$el).html(employeeListView.$el);
        return this;
    };

    this.findByName = function() {
        service.findByName($('.search-key').val()).done(function(employees) {
            employeeListView.setEmployees(employees);
        });
    };

    this.initialize();
}

EmployeeService.js

var EmployeeService = function() {

    this.initialize = function() {
        // No Initialization required
        var deferred = $.Deferred();
        deferred.resolve();
        return deferred.promise();
    }

    this.findById = function(id) {
        var deferred = $.Deferred();
        var employee = null;
        var l = employees.length;
        for (var i=0; i < l; i++) {
            if (employees[i].id === id) {
                employee = employees[i];
                break;
            }
        }
        deferred.resolve(employee);
        return deferred.promise();
    }

    this.findByName = function(searchKey) {
        var deferred = $.Deferred();
        var results = employees.filter(function(element) {
            var fullName = element.firstName + " " + element.lastName;
            return fullName.toLowerCase().indexOf(searchKey.toLowerCase()) > -1;
        });
        deferred.resolve(results);
        return deferred.promise();
    }
    function onSuccess(contacts) {
    alert('Found ' + contacts.length + ' contacts.');
};


function createContact(contacts) {
   contacts = navigator.contacts.create();
   contacts.save(contactSuccess, contactError);

   function contactSuccess() {
      alert("Contact is saved!")
   }

   function contactError(message) {
      alert('Failed because: ' + message);
   }

}
function onError(contactError) {
    alert('onError!');
};



    var employees = [
        {"id": 1, "firstName": "Naveen", "lastName": "Bannikoppa", "managerId": 0, "managerName": "", "title": "Mobile:7411859736", "department": "Corporate", "cellPhone": "617-000-0001", "officePhone": "7411859736", "email": "[email protected]", "city": "Boston, MA", "pic": "James_King.jpg", "twitterId": "@fakejking", "blog": "http://coenraets.org"},
        {"id": 2, "firstName": "Nishan", "lastName": "Shah", "managerId": 1, "managerName": "Amit vijay", "title": "Software Engineer", "department": "Software Delivery", "cellPhone": "8867793489", "officePhone": "7411859736", "email": "[email protected]", "city": "Bengaluru, KA", "pic": "Nishan_Shah.jpg", "twitterId": "@nishanshah", "blog": "http://ionidea.com"}

    ];

}

EmployeeListVew.js

var EmployeeListView = function () {

    var employees;

    this.initialize = function() {
        this.$el = $('<div/>');
        this.render();
    };

    this.setEmployees = function(list) {
        employees = list;
        this.render();
    }

    this.render = function() {
        this.$el.html(this.template(employees));
        return this;
    };

    this.initialize();

}

EmployeeView.js

var EmployeeView = function(employee) {

    this.initialize = function() {
        this.$el = $('<div/>');
    };

    this.render = function() {
        this.$el.html(this.template(employee));
        return this;
    };

    this.initialize();

}

Solution

  • You should review the documentation of contacts plugin

    https://github.com/apache/cordova-plugin-contacts#save-example

    Basically, first create then save it.

    Update

    Update you createContact with below function make sure you edit the marked lines with your needs

    function createContact(contacts) {
        var _contacts = navigator.contacts.create();
        // Create and assign phone numbers
        _contacts.phoneNumbers = [
            new ContactField('mobile', contacts.something.phone_number) // Edit here
        ];
        // Create and assing contact name
        var name = new ContactName();
        name.givenName = 'Can'; // Edit here
        name.familyName = 'Tecim'; // Edit here
        _concacts.name = name;
    
        // Set the display name
        _contacts.displayName = _contacts.nickname = 'Can Tecim'; // Edit here
        _contacts.save(contactSuccess, contactError);
    
        function contactSuccess() {
            alert("Contact is saved!")
        }
    
        function contactError(message) {
            alert('Failed because: ' + message);
        }
    
    }