React Native - 使用Platform API进行平台检测
作者:hangge | 2017-08-07 08:10
在 React Native 开发中,有时会需要在代码中检测当前是哪个系统平台(Android,还是 iOS),然后当前代码根据运行的平台走不同的分支代码。
这个我们可以通过 React Native 提供的 Platform API 来判断,下面是一个简单的样例:
import React, { Component } from 'react';
import {
AppRegistry,
StyleSheet,
Platform,
} from 'react-native';
class App extends Component {
//构造函数
constructor(props) {
super(props);
if(Platform.OS === "android") {
//Android平台需要运行的代码
alert("当前是Android系统。");
}else{
//iOS平台需要运行的代码
alert("当前是iOS系统。");
}
}
render() {
}
}
AppRegistry.registerComponent('HelloWorld', () => App);
运行结果如下:
全部评论(0)