React Native聯(lián)系人組件

本文原創(chuàng)首發(fā)于公眾號(hào):ReactNative開發(fā)圈,轉(zhuǎn)載需注明出處。

React Native通訊錄聯(lián)系人組件,名叫:react-native-contacts,可以用來增加、讀取、修改、刪除、搜索手機(jī)通訊錄中的聯(lián)系人信息,兼容IOS和安卓雙平臺(tái)。

安裝

npm install react-native-contacts
react-native link react-native-contacts

iOS權(quán)限配置

需要增加讀取聯(lián)系人的權(quán)限,在Info.plist中增加一個(gè)key:"Privacy - Contacts Usage Description”。


image.png

Android權(quán)限配置

在android/app/src/main/AndroidManifest.xml中增加以下權(quán)限:

<uses-permission android:name="android.permission.READ_PROFILE" />
  <uses-permission android:name="android.permission.READ_CONTACTS" />
  <uses-permission android:name="android.permission.WRITE_CONTACTS" />

支持的方法

image.png

示例

增加聯(lián)系人

var newPerson = {
  emailAddresses: [{
    label: "work",
    email: "mrniet@example.com",
  }],
  familyName: "Nietzsche",
  givenName: "Friedrich",
}

Contacts.addContact(newPerson, (err) => { /*...*/ })

更新和刪除聯(lián)系人

Contacts.getAll( (err, contacts) => {
  //update the first record
  let someRecord = contacts[0]
  someRecord.emailAddresses.push({
    label: "junk",
    email: "mrniet+junkmail@test.com",
  })
  Contacts.updateContact(someRecord, (err) => { /*...*/ })

  //delete the second record
  Contacts.deleteContact(contacts[1], (err) => { /*...*/ })
})

獲取所有聯(lián)系人

var Contacts = require('react-native-contacts')

Contacts.getAll((err, contacts) => {
  if(err === 'denied'){
    // error
  } else {
    // contacts returned in []
  }
})

如果聯(lián)系人比較多的話,getAll方法會(huì)比較慢,作者建議先獲取好所有聯(lián)系人,存儲(chǔ)在本地?cái)?shù)據(jù)庫中。在需要用的時(shí)候,直接讀取本地?cái)?shù)據(jù)庫,這樣速度比較快。

搜索聯(lián)系人

var Contacts = require('react-native-contacts')

Contacts.getContactsMatchingString("filter", (err, contacts) => {
  if(err === 'denied'){
    // x.x
  } else {
    // Contains only contacts matching "filter"
    console.log(contacts)
  }
})

組件地址

詳細(xì)的源碼和使用說明請(qǐng)?jiān)L問GitHub:https://github.com/rt2zz/react-native-contacts

舉手之勞關(guān)注我的微信公眾號(hào):ReactNative開發(fā)圈

image.png

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容