React Native 一些API详解

一、Platform 获取平台属性

javascript 复制代码
// 获取平台
console.log(Platform.OS)
// 获取版本
console.log(Platform.Version)
// {"Brand": "google", "Fingerprint": "google/sdk_gphone64_x86_64/emu64xa:16/BP22.250325.006/13344233:user/release-keys", "Manufacturer": "Google", "Model": "sdk_gphone64_x86_64", "Release": "16", "Serial": "unknown", "ServerHost": "localhost:8081", "Version": 36, "isTesting": false, "reactNativeVersion": {"major": 0, "minor": 70, "patch": 6, "prerelease": null}, "uiMode": "normal"}
console.log(Platform.constants)

// 是否是pad
console.log(Platform.isPad);
// 是否是tv
console.log(Platform.isTV);

// 不同平台用不同样式
const styles = StyleSheet.create({
	box: {
		width: '100%',
        height: '100%',
		...Platform.select({ // 返回的是对象
			android: {xx:xx},
			ios: {xx:xx},
			default: {xx:xx},
		})
	}
})

二、Linking

javascript 复制代码
// 判断链接能否打开
Linking.canOpenURL('xx')
// 打开链接
Linking.openURL('xx') 
// 跳应用设置页面
Linking.openSettings()
// 获取跳转过来的url
Linking.getInitialURL()

三、PixelRatio

javascript 复制代码
// 获取屏幕像素密度
PixelRatio.get()
// 获取安卓字体缩放比例
PixelRatio.getFontScale()
// 获取尺寸的物理像素 = 尺寸* PixelRatio.get()
PixelRatio.getPixelSizeForLayoutSize(尺寸)

四、PermissionsAndroid 安装权限

javascript 复制代码
// 检查权限
PermissionsAndroid.check(xxxx).then(result => {
	// result: true/false
    if (!result) {
    	// 申请权限
        PermissionsAndroid.request(xxxx).then(status => {
        	// status: granted 允许   /   denied 拒绝
            // do sth.
        });
    }
});


// 申请多个权限
 PermissionsAndroid.requestMultiple([xxxx,xxxx])
相关推荐
Highcharts.js1 天前
Highcharts 集成到 React 或 Vue 项目中时,如何高效地使用 TypeScript技巧?
vue.js·人工智能·react.js·typescript·前端框架·highcharts
hhhjhl2 天前
React Native 三方库「react-native-sha256」的鸿蒙化适配指南
react native·华为·harmonyos
2601_949970692 天前
React Native 三方库「react-native-version-check」的鸿蒙化适配指南
react native·华为·harmonyos
hhhjhl2 天前
React Native 三方库「react-native-proximity」的鸿蒙化适配指南
react native·华为·harmonyos
2601_949970692 天前
React Native 三方库「react-native-media-notification」的鸿蒙化适配指南
react native·华为·harmonyos
liuchangng2 天前
React 19 + Vite 6 实战:从教材 PDF 到 866 词间隔重复单词 App
前端·react.js·pdf·vite·开源项目
GitCode官方2 天前
从大语言模型到具身智能:百度百舸 LoongForge 入驻 AtomGit
人工智能·react native·百度·harmonyos·atomgit
鸿蒙开发2 天前
Three.js 首帧卡顿排查:compileAsync 之后,为什么还会停一下?
react.js·three.js
小李科技3 天前
【性能优化】事件委托:目标定位与 React/Vue 实战
前端·vue.js·react.js·面试·性能优化·前端框架
csj503 天前
前端基础之《React(16)—Modal组件封装》
前端·react.js