如何使用Cordova将手机号码保存到联系人

时间:2016-06-22 11:48:24

标签: cordova handlebars.js cordova-plugins android-contacts

我有一个目录显示一些信息,如姓名和相关的电话号码,点击它将进入电话呼叫页面(本机)。如何使用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": "jking@fakemail.com", "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": "nishanshah50@gmail.com", "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();

}

1 个答案:

答案 0 :(得分:2)

您应该查看联系人插件

的文档

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

基本上,首先创建然后保存它。

更新

使用以下功能更新createContact,确保根据需要编辑标记的行

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);
    }

}