Contacts

API Reference: Device.Contacts

Contacts is used to access the contact list on the device.

Permissions

To add a contact, you have to guarantee WRITE_CONTACT permission.

To get a contact, you have to guarantee READ_CONTACT permission.

JavaScript
JavaScript
const Page = require("sf-core/ui/page");
const Contacts = require("sf-core/device/contacts");
const Button = require("sf-core/ui/button");
const Application = require("sf-core/application");
const Color = require("sf-core/ui/color");
const Device = require('sf-core/device');
const extend = require("js-base/core/extend");
var Page1 = extend(Page)(
function(_super) {
var page = this;
_super(this, {
onShow: function(params) {
Application.statusBar.visible = false;
this.headerBar.visible = false;
}
});
function getContact(e) {
console.log(e)
var displayName = e.firstName;
var phoneNumber = e.phoneNumbers;
alert("Name : " + displayName + " Phone : " + phoneNumber);
}
var myButton = new Button({
marginTop: 250,
marginRight: 50,
marginLeft: 50,
height: 60,
text: "PICK CONTACT",
backgroundColor: Color.RED,
onPress: function() {
chooseContact();
}
});
this.layout.addChild(myButton);
function chooseContact() {
if(Device.System.OS === "Android") {
var result = Application.android.checkPermission(Application.android.Permissions.READ_CONTACTS);
if (!result) {
var permissionCode = 1001;
Application.android.requestPermissions(permissionCode, Application.android.Permissions.READ_CONTACTS);
}
result = Application.android.checkPermission(Application.android.Permissions.READ_CONTACTS);
if (result) {
Contacts.pickContact({
onSuccess: getContact,
page: page
});
}
} else if(Device.System.OS === "iOS") {
Contacts.pickContact({
onSuccess: getContact,
page: page
});
}
}
}
);
module.exports = Page1;