react-native-contact 安卓已测试,
1. 下载模块 npm install react-native-contacts --save
2.安卓配置:
a.在android/settings.gradle
b.在android/app/build.gradle
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />
<uses-permission android:name="android.permission.WRITE_CONTACTS" />
-keep class com.rt2zz.reactnativecontacts.** {*;}
-keepclassmembers class com.rt2zz.reactnativecontacts.** {*;}

b.

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 安卓已测试,的更多相关文章
- React Native解决安卓图片被挤压
Bug如下图所示: iOS显示正常,而安卓出现图片被挤压上去. 最后的解决方法: 比如你的 图片 是 750 x 513 那么 你设置样式的时候 width 为 屏幕的宽 ,高度为 屏幕的 宽 / ( ...
- React Native区分安卓/iOS平台
import { Platform, } from 'react-native'; alert(JSON.stringify(Platform)): android手机弹出:{"OS&quo ...
- React Native运行安卓报错解决记录
1>Error:Configuration with name ‘default’ not found. 解决链接: http://blog.csdn.net/u011240877/articl ...
- 使用React Native来撰写跨平台的App
React Native 是一个 JavaScript 的框架,用来撰写实时的.可原生呈现 iOS 和 Android 的应用.其是基于 React的,而 React 是 Facebook 的用于构建 ...
- 一个资深iOS开发者对于React Native的看法
一个资深iOS开发者对于React Native的看法 当我第一次尝试ReactNative的时候,我觉得这只是网页开发者涉足原生移动应用领域的歪门邪道. 我认为一个js开发者可以使用javasc ...
- [转] 一个资深iOS开发者对于React Native的看法
当我第一次尝试ReactNative的时候,我觉得这只是网页开发者涉足原生移动应用领域的歪门邪道. 我认为一个js开发者可以使用javascript来构建iPhone应用确实是一件很酷的事情,但是我很 ...
- 《React Native 精解与实战》书籍连载「React Native 底层原理」
此文是我的出版书籍<React Native 精解与实战>连载分享,此书由机械工业出版社出版,书中详解了 React Native 框架底层原理.React Native 组件布局.组件与 ...
- 选择 React Native 的理由
转载:选择 React Native 的理由 从开始知道 React Native 到现在已经过了5个月,真实的试用也经历了三个月的时间.阅读文档开始,了解是什么,到简单的理解为什么,都是在聆听不同的 ...
- react native 中的ReadableMap和WritableMap的使用
react native跟安卓原生交互的数据类型中,有两个比较陌生的类型,ReadableMap和WritableMap. ReadableMap和WritableMap,顾名思义,反正是map. W ...
随机推荐
- 【栈】日志分析(BSOJ2981)
Description M海运公司最近要对旗下仓库的货物进出情况进行统计.目前他们所拥有的唯一记录就是一个记录集装箱进出情况的日志.该日志记录了两类操作:第一类操作为集装箱入库操作,以及该次入库的集装 ...
- SPOJ - GSS1 —— 线段树 (结点信息合并)
题目链接:https://vjudge.net/problem/SPOJ-GSS1 GSS1 - Can you answer these queries I #tree You are given ...
- POJ1185 炮兵阵地 —— 状压DP
题目链接:http://poj.org/problem?id=1185 炮兵阵地 Time Limit: 2000MS Memory Limit: 65536K Total Submissions ...
- curl的安装与使用
linux 下的curl扩展安装,记录一下(发现网上好多抄袭的也不检测一下能不能用) 1.下载curl安装包: https://curl.haxx.se/download.html 2.解压: 如 t ...
- 深入理解JVM - Java内存区域与内存溢出异常 - 第二章
一 运行时数据区域 JVM在执行Java程序的过程中会把它管理的内存划分为若干个不同的数据区域.这些区域都有各自的用途,以及创建和销毁的时间. 程序计数器 程序计数器(Program Counter ...
- python无法安装cv2的解决办法
问题:在windows命令窗口输入pip install cv2后出现:Could not find a version that satisfies the requirement cv2... 解 ...
- 分享知识-快乐自己:Oracle 创建序列 及 使用序列
1.创建序列语法: create sequence 序列名 [可选参数] 序列名常定义为‘seq_XXX’的形式,创建序列不能使用replace 可选参数说明: increment by: 序列每次增 ...
- kubectl工具管理应用生命周期
######kubectl管理工具###### [root@k8s-master dashboard]# kubectl get pod NAME READY STATUS RESTARTS AGE ...
- ACM学习历程—HDU1717 小数化分数2(gcd)
Description Ray 在数学课上听老师说,任何小数都能表示成分数的形式,他开始了化了起来,很快他就完成了,但他又想到一个问题,如何把一个循环小数化成分数呢? 请你写一个程序不但可以将普通小数 ...
- ACM学习历程——HDU5137 How Many Maos Does the Guanxi Worth(14广州10题)(单源最短路)
Problem Description "Guanxi" is a very important word in Chinese. It kind of means &quo ...