1. 下载模块    npm install react-native-contacts --save

2.安卓配置:

  a.在android/settings.gradle

    include ':react-native-contacts'
    project(':react-native-contacts').projectDir = new File(rootProject.projectDir, '../node_modules/react-native-contacts/android')

  b.在android/app/build.gradle  

    dependencies {
        ...
        implementation project(':react-native-contacts')
    }
 
  c.注册模块 (in MainApplication.java)
    

     import com.rt2zz.reactnativecontacts.ReactNativeContacts; // <--- import 
 
     public class MainActivity extends ReactActivity {
        ......
 
        @Override
        protected List<ReactPackage> getPackages() {
            return Arrays.<ReactPackage>asList(
                    new MainReactPackage(),
                    new ReactNativeContacts()); // <------ add this 
        }
      ......
    }
 
  d.在AndroidManifest.xml中设置权限
  
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
 
  e.如果设置了Proguard,就在proguard-rules.pro中添加
-keep class com.rt2zz.reactnativecontacts.** {*;}
-keepclassmembers class com.rt2zz.reactnativecontacts.** {*;}
 
 

3.ios配置
  a. 右击Libraries,选择Add Files to “” --> 文件的node-modules ---> react-native-contact ---> ios --> RCTContacts.xcodeproj,成功后会显示在Libraries中
  

  b.

 c.在Info.plist中. 添加 Privacy - Contacts Usage Description

 
 
附上一段参考的代码,简单的获取本地,已测试:
  

/**
* Sample React Native App
* https://github.com/facebook/react-native
*
* @format
* @flow
*/ import React, {Component} from 'react';
import {TouchableOpacity, PermissionsAndroid, Platform, SectionList, StyleSheet, Text, View, ScrollView, Image, PixelRatio} from 'react-native';
import {Actions, Router, Scene,} from 'react-native-router-flux';
import Contacts from 'react-native-contacts';
import { Container, Header, Content, List, ListItem, } from 'native-base'; export default class Contact extends Component {
constructor(props) {
super (props);
this.state = {
contactData: [], // 通讯录列表
}
}
componentDidMount() {
// this.requestCONTACTS()
} requestCONTACTS = () => {
let self = this;
if (Platform.OS === 'android') {
PermissionsAndroid.check(PermissionsAndroid.PERMISSIONS.READ_CONTACTS).then(res => {
if (!res || res !== 'granted') {
console.log('我走进11')
PermissionsAndroid.request(PermissionsAndroid.PERMISSIONS.READ_CONTACTS, {
'title': '申请读取通讯录权限',
'message': '获取通讯录权限,' +
'
})
.then(res => {
console.log('我走进22') console.log(res, '数组吗')
if (res !== 'granted') {
console.log('我走进33')
Alert.alert('访问通讯录权限没打开', '请在iPhone的“设置-隐私”选项中,允许访问您的通讯录')
}
else {
console.log('我走进44')
self.onButtonPressed()
};
});
}
else {
console.log('我走进55')
self.onButtonPressed()
};
}); } else {
console.log('我走进66,ios')
console.log(Contacts, 'Contacts')
if (Contacts) {
Contacts.checkPermission((err, permission) => {
console.log('我走进77,ios')
if (err) throw err;
// Contacts.PERMISSION_AUTHORIZED || Contacts.PERMISSION_UNDEFINED || Contacts.PERMISSION_DENIED
if (permission === 'undefined') {
Contacts.requestPermission((err, permission) => {
console.log('我走进88,ios')
if (err) throw err;
if (permission === 'authorized') {
console.log('我走进99,ios')
// 同意!
self.onButtonPressed()
}
if (permission === 'denied') {
console.log('我走进100,ios')
// 拒绝
Alert.alert('访问通讯录权限没打开', '请在iPhone的“设置-隐私”选项中,允许访问您的通讯录')
}
})
}
if (permission === 'authorized') {
console.log('我走进110,ios')
// 同意!
self.onButtonPressed()
}
if (permission === 'denied') {
console.log('我走进120,ios')
// 已经拒绝
Alert.alert('访问通讯录权限没打开', '请在iPhone的“设置-隐私”选项中,允许访问您的通讯录')
}
})
}
}
} // 获取通讯录列表
onButtonPressed() {
let self = this;
Contacts.getAll((err, contacts) => {
console.log('我点击了')
if (err) throw err;
this.setState({
contactData: contacts
})
console.log(contacts instanceof Array);//通讯录列表
})
} // 修改通讯录信息
updateContact(contacts){
let someRecord = contacts
someRecord.phoneNumbers.push({
label: "mobile",
number: "12345678901",
})
someRecord.givenName = '李四'
Contacts.updateContact(someRecord, (err) => {
if (err) throw err;
// record updated
})
} // 添加通讯录信息
updateContact(contacts){
let someRecord = contacts
someRecord.phoneNumbers.push({
label: "mobile",
number: "12345678901",
})
someRecord.givenName = '李四'
Contacts.updateContact(someRecord, (err) => {
if (err) throw err;
// record updated
})
} // 删除通讯录
deleteContact(contacts){
//delete the second record
Contacts.deleteContact(contacts, (err, recordId) => {
if (err) throw err;
// contact deleted
})
} // 拨打电话
onDialingAction = (telephone) => {
let url = 'tel: ' + telephone
Linking.canOpenURL(url).then(supported => {
if (!supported) {
Toast.show('您的系统不支持打电话!')
} else {
return Linking.openURL(url);
}
}).catch(err => { });
} // 发送短信
onSendMessage = (telephone) => {
let url = 'smsto: ' + telephone
Linking.canOpenURL(url).then(supported => {
if (!supported) {
Toast.show('您的系统不支持发送短信!')
} else {
return Linking.openURL(url);
}
}).catch(err => { });
} sel = (val) => {
for (let i = 0; i < val.phoneNumbers.length; i++) {
if (val.phoneNumbers[i].label !== "other") {
alert(val.phoneNumbers[i].number, '这个是联系方式') }
} }
render() {
let {contactData} = this.state;
// console.log(contactData.splice(1, 10))
console.log( contactData.splice(1, 10))
return (
<View style={styles.container}>
<Text onPress={this.requestCONTACTS}>11</Text>
<View style={{height: '80%'}}>
<Content>
{/*<List>*/}
{
contactData.length > 0 ? contactData.splice(1, 10).map((val, i) => {
return (
<List key={i}>
<ListItem itemDivider>
<Text>A</Text>
</ListItem>
<ListItem>
<Text onPress={() => this.sel(val)}>{val.familyName}</Text>
</ListItem>
</List>
) }) : <Text>通讯录为空</Text>
}
</Content>
</View>
</View>
);
}
} const styles = StyleSheet.create({
container: {
// flex: 1,
// backgroundColor: 'red',
padding: 20,
marginTop: 20,
},
flex: {
flexDirection: 'row',
alignItems: 'center',
}, });
 
 
 
 

react-native-contact 安卓已测试,的更多相关文章

  1. React Native解决安卓图片被挤压

    Bug如下图所示: iOS显示正常,而安卓出现图片被挤压上去. 最后的解决方法: 比如你的 图片 是 750 x 513 那么 你设置样式的时候 width 为 屏幕的宽 ,高度为 屏幕的 宽 / ( ...

  2. React Native区分安卓/iOS平台

    import { Platform, } from 'react-native'; alert(JSON.stringify(Platform)): android手机弹出:{"OS&quo ...

  3. React Native运行安卓报错解决记录

    1>Error:Configuration with name ‘default’ not found. 解决链接: http://blog.csdn.net/u011240877/articl ...

  4. 使用React Native来撰写跨平台的App

    React Native 是一个 JavaScript 的框架,用来撰写实时的.可原生呈现 iOS 和 Android 的应用.其是基于 React的,而 React 是 Facebook 的用于构建 ...

  5. 一个资深iOS开发者对于React Native的看法

    一个资深iOS开发者对于React Native的看法 当我第一次尝试ReactNative的时候,我觉得这只是网页开发者涉足原生移动应用领域的歪门邪道.   我认为一个js开发者可以使用javasc ...

  6. [转] 一个资深iOS开发者对于React Native的看法

    当我第一次尝试ReactNative的时候,我觉得这只是网页开发者涉足原生移动应用领域的歪门邪道. 我认为一个js开发者可以使用javascript来构建iPhone应用确实是一件很酷的事情,但是我很 ...

  7. 《React Native 精解与实战》书籍连载「React Native 底层原理」

    此文是我的出版书籍<React Native 精解与实战>连载分享,此书由机械工业出版社出版,书中详解了 React Native 框架底层原理.React Native 组件布局.组件与 ...

  8. 选择 React Native 的理由

    转载:选择 React Native 的理由 从开始知道 React Native 到现在已经过了5个月,真实的试用也经历了三个月的时间.阅读文档开始,了解是什么,到简单的理解为什么,都是在聆听不同的 ...

  9. react native 中的ReadableMap和WritableMap的使用

    react native跟安卓原生交互的数据类型中,有两个比较陌生的类型,ReadableMap和WritableMap. ReadableMap和WritableMap,顾名思义,反正是map. W ...

随机推荐

  1. importlib模块 反射字符串的对象

    通过 importlib模块 反射字符串的对象 Python 3.5.2 (default, Nov 17 2016, 17:05:23) [GCC 5.4.0 20160609] on linux ...

  2. CodeForces - 540C Ice Cave —— BFS

    题目链接:https://vjudge.net/contest/226823#problem/C You play a computer game. Your character stands on ...

  3. linux系统 标准目录及其内容

      路径名 操作系统 内容 /bin 所有 最核心的操作系统命令 /boot LS 内核和加载内核所需的文件 /dev 所有 伪终端,磁盘,打印机等的设备项 /etc 所有 关键的启动文件和配置文件 ...

  4. 让django完成翻译,迁移数据库模型

    声明:此Django分类下的教程是追梦人物所有,地址http://www.jianshu.com/u/f0c09f959299,本人写在此只是为了巩固复习使用 上篇我们完成了数据库模型的代码,但是还只 ...

  5. Java常用四大线程池用法以及ThreadPoolExecutor详解

    为什么用线程池? 1.创建/销毁线程伴随着系统开销,过于频繁的创建/销毁线程,会很大程度上影响处-理效率 2.线程并发数量过多,抢占系统资源从而导致阻塞 3.对线程进行一些简单的管理 在Java中,线 ...

  6. struts2框架xml验证

    struts2验证分为3步: 1.获取需要验证的信息,使用同名属性,提供getter,setter方法.然后框架使用反射将值自动注入. 2.对信息进行验证,成功失败作出对应的选择. xml验证和手动验 ...

  7. linux 多线程编程-读写者问题

    #include <cstdio> #include <pthread.h> #include <unistd.h> ]; int i,j; pthread_rwl ...

  8. zero to one (3)

    工具使用 AWVS Acunetix Web Vulnerability Scanner(简称AWVS)是一款知名的Web网络漏洞扫描工具,它通过网络爬虫测试你的网站安全,检测流行安全漏洞. 功能及特 ...

  9. QT(2)项目文件介绍

    一.项目创建 二.文件说明 三.QT模块

  10. JAVA操作cassandra数据库

    如果是maven项目,在pom.xml里加入依赖.不是的话下载相应的jar包放到lib目录下.这里驱动包的版本要和你cassandra的大版本一致.我这里cassandra的版本是最新的3.9,驱动是 ...