更新记录
1.0.0(2022-09-01)
1.提供读取通讯录信息功能,功能纯净且api丰富,基本能满足所有需求,只需要一个权限,无隐私合规相关烦恼。
平台兼容性
Android | Android CPU类型 | iOS |
---|---|---|
适用版本区间:4.4 - 12.0 | armeabi-v7a:支持,arm64-v8a:支持,x86:支持 | × |
原生插件通用使用流程:
- 购买插件,选择该插件绑定的项目。
- 在HBuilderX里找到项目,在manifest的app原生插件配置中勾选模块,如需要填写参数则参考插件作者的文档添加。
- 根据插件作者的提供的文档开发代码,在代码中引用插件,调用插件功能。
- 打包自定义基座,选择插件,得到自定义基座,然后运行时选择自定义基座,进行log输出测试。
- 开发完毕后正式云打包
付费原生插件目前不支持离线打包。
Android 离线打包原生插件另见文档 https://nativesupport.dcloud.net.cn/NativePlugin/offline_package/android
iOS 离线打包原生插件另见文档 https://nativesupport.dcloud.net.cn/NativePlugin/offline_package/ios
注意事项:使用HBuilderX2.7.14以下版本,如果同一插件且同一appid下购买并绑定了多个包名,提交云打包界面提示包名绑定不一致时,需要在HBuilderX项目中manifest.json->“App原生插件配置”->”云端插件“列表中删除该插件重新选择
用法举例
-
获取所有通讯录信息
getAll方法是一个数据库密集型过程,可能需要很长时间才能完成,具体取决于联系人列表的大小。 因此,建议您在必要的时候才访问getAll方法,并缓存结果以供将来使用。
import Contacts from 'react-native-contacts'; Contacts.getAll((contacts) => { // contacts returned })
-
请求读取通讯录权限
import Contacts from 'react-native-contacts'; Contacts.requestPermission((permissionResult)=>{ if (permissionResult == 'authorized') { Contacts.getAll((contacts)=>{ console.log(JSON.stringfy(contacts[0])) }) } })
-
检查是否有通讯录读取权限
Contacts.checkPermission((result)=>{ // result: true/false if (result) { } else { // 无读取权限,则请求权限 Contacts.requestPermission() } })
API
- getAll: Promise<Contact[]> - returns all contacts as an array of objects
- getAllWithoutPhotos - same as getAll on Android,
- getContactById(contactId): Promise - returns contact with defined contactId (or null if it doesn't exist)
- getCount(): Promise - returns the number of contacts
- getPhotoForId(contactId): Promise - returns a URI (or null) for a contacts photo
- addContact(contact): Promise - adds a contact to the AddressBook.
- openContactForm(contact) - create a new contact and display in contactsUI.
- openExistingContact(contact) - open existing contact (edit mode), where contact is an object with a valid recordID
- viewExistingContact(contact) - open existing contact (view mode), where contact is an object with a valid recordID
- editExistingContact(contact): Promise - add numbers to the contact, where the contact is an object with a valid recordID and an array of phoneNumbers
- updateContact(contact): Promise - where contact is an object with a valid recordID
- deleteContact(contact) - where contact is an object with a valid recordID
- getContactsMatchingString(string): Promise<Contact[]> - where string is any string to match a name (first, middle, family) to
- getContactsByPhoneNumber(string): Promise<Contact[]> - where string is a phone number to match to.
- getContactsByEmailAddress(string): Promise<Contact[]> - where string is an email address to match to.
- checkPermission(): Promise - checks permission to access Contacts
- requestPermission(): Promise - request permission to access Contacts
- writePhotoToPath() - writes the contact photo to a given path
Contact结构示例
{
recordID: '6b2237ee0df85980',
backTitle: '',
company: '',
emailAddresses: [{
label: 'work',
email: 'carl-jung@example.com',
}],
familyName: 'Jung',
givenName: 'Carl',
middleName: '',
jobTitle: '',
phoneNumbers: [{
label: 'mobile',
number: '(555) 555-5555',
}],
hasThumbnail: true,
thumbnailPath: 'content://com.android.contacts/display_photo/3',
postalAddresses: [{
label: 'home',
formattedAddress: '',
street: '123 Fake Street',
pobox: '',
neighborhood: '',
city: 'Sample City',
region: 'CA',
state: 'CA',
postCode: '90210',
country: 'USA',
}],
prefix: 'MR',
suffix: '',
department: '',
birthday: {'year': 1988, 'month': 1, 'day': 1 },
imAddresses: [
{ username: '0123456789', service: 'ICQ'},
{ username: 'johndoe123', service: 'Facebook'}
],
isStarred: false,
}