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.
TypeScript
1
import PageSampleDesign from 'generated/pages/pageSample';
2
import FlexLayout from '@smartface/native/ui/flexlayout';
3
import Application from '@smartface/native/application';
4
import Contacts from '@smartface/native/device/contacts';
5
import Button from '@smartface/native/ui/button';
6
import Device from '@smartface/native/device';
7
import PermissionUtil from '@smartface/extension-utils/lib/permission';
8
9
//You should create new Page from UI-Editor and extend with it.
10
export default class Sample extends PageSampleDesign {
11
12
myButton: Button;
13
14
constructor() {
15
super();
16
// Overrides super.onShow method
17
this.onShow = onShow.bind(this, this.onShow.bind(this));
18
// Overrides super.onLoad method
19
this.onLoad = onLoad.bind(this, this.onLoad.bind(this));
20
21
this.layout.flexDirection = FlexLayout.FlexDirection.COLUMN;
22
this.layout.justifyContent = FlexLayout.JustifyContent.CENTER;
23
this.layout.alignItems = FlexLayout.AlignItems.CENTER;
24
}
25
26
getContact(contact: Contacts.Contact): void {
27
const { firstName, phoneNumbers } = contact;
28
alert("Name : " + firstName + " Phone : " + phoneNumbers);
29
}
30
31
chooseContact(): void {
32
33
//@ts-ignore
34
if (Device.System.OS === "Android") {
35
PermissionUtil.getPermission(Application.Android.Permissions.WRITE_CONTACTS, 'Please go to the settings and grant permission')
36
.then(() => {
37
this.pickContact()
38
})
39
.then((reason) => {
40
console.info('Permission rejected');
41
});
42
//@ts-ignore
43
} else if (Device.System.OS === "iOS") {
44
this.pickContact()
45
}
46
}
47
pickContact() {
48
//@ts-ignore
49
Contacts.pickContact({
50
onSuccess: this.getContact,
51
page: this
52
});
53
}
54
}
55
56
/**
57
* @event onShow
58
* This event is called when a page appears on the screen (everytime).
59
* @param {function} superOnShow super onShow function
60
* @param {Object} parameters passed from Router.go function
61
*/
62
function onShow(superOnShow: () => void) {
63
const { headerBar } = this;
64
superOnShow();
65
Application.statusBar.visible = false;
66
headerBar.visible = false;
67
}
68
69
/**
70
* @event onLoad
71
* This event is called once when page is created.
72
* @param {function} superOnLoad super onLoad function
73
*/
74
function onLoad(superOnLoad: () => void) {
75
superOnLoad();
76
77
this.myButton = new Button({
78
text: "PICK CONTACT",
79
onPress: (): void => {
80
this.chooseContact();
81
}
82
});
83
84
this.layout.addChild(this.myButton, "myButton", ".sf-button", {
85
marginTop: 250,
86
marginRight: 50,
87
marginLeft: 50,
88
height: 60,
89
backgroundColor: "#FF0000"
90
});
91
}
92
Copied!
Last modified 2mo ago
Copy link