Contacts

API Reference: Device.Contacts

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

TypeScript code blocks include examples of how to implement, override and components within the theme. You can create page with the UI Editor to make your page compatible with theming and then you can implement themable components programmatically. Once the page is created with the UI Editor, it generates a class under scripts/generated/pages. You can then extend that class with the following TypeScript classes.

Permissions

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

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

TypeScript
JavaScript
TypeScript
import PageSampleDesign from 'generated/pages/pageSample';
import FlexLayout = require('sf-core/ui/flexlayout');
import Application = require('sf-core/application');
import Contacts = require("sf-core/device/contacts");
import Button = require("sf-core/ui/button");
import Device = require('sf-core/device');
import PermissionUtil = require('sf-extension-utils/lib/permission');
//You should create new Page from UI-Editor and extend with it.
export default class Sample extends PageSampleDesign {
myButton: Button;
constructor() {
super();
// Overrides super.onShow method
this.onShow = onShow.bind(this, this.onShow.bind(this));
// Overrides super.onLoad method
this.onLoad = onLoad.bind(this, this.onLoad.bind(this));
this.layout.flexDirection = FlexLayout.FlexDirection.COLUMN;
this.layout.justifyContent = FlexLayout.JustifyContent.CENTER;
this.layout.alignItems = FlexLayout.AlignItems.CENTER;
}
getContact(contact: Contacts.Contact): void {
const { firstName, phoneNumbers } = contact;
alert("Name : " + firstName + " Phone : " + phoneNumbers);
}
chooseContact(): void {
//@ts-ignore
if (Device.System.OS === "Android") {
PermissionUtil.getPermission(Application.Android.Permissions.WRITE_CONTACTS, 'Please go to the settings and grant permission')
.then(() => {
this.pickContact()
})
.then((reason) => {
console.info('Permission rejected');
});
//@ts-ignore
} else if (Device.System.OS === "iOS") {
this.pickContact()
}
}
pickContact() {
//@ts-ignore
Contacts.pickContact({
onSuccess: this.getContact,
page: this
});
}
}
/**
* @event onShow
* This event is called when a page appears on the screen (everytime).
* @param {function} superOnShow super onShow function
* @param {Object} parameters passed from Router.go function
*/
function onShow(superOnShow: () => void) {
const { headerBar } = this;
superOnShow();
Application.statusBar.visible = false;
headerBar.visible = false;
}
/**
* @event onLoad
* This event is called once when page is created.
* @param {function} superOnLoad super onLoad function
*/
function onLoad(superOnLoad: () => void) {
superOnLoad();
this.myButton = new Button({
text: "PICK CONTACT",
onPress: (): void => {
this.chooseContact();
}
});
this.layout.addChild(this.myButton, "myButton", ".sf-button", {
marginTop: 250,
marginRight: 50,
marginLeft: 50,
height: 60,
backgroundColor: "#FF0000"
});
}
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;