新聞中心
AppStateIOS 可以告訴你應(yīng)用程序是在前臺還是在后臺,而且狀態(tài)更新時會通知你。 在處理推送通知時,AppStateIOS 經(jīng)常被用于判斷目標(biāo)和適當(dāng)?shù)男袨椤?/p>

創(chuàng)新互聯(lián)專注于黟縣企業(yè)網(wǎng)站建設(shè),響應(yīng)式網(wǎng)站開發(fā),商城網(wǎng)站制作。黟縣網(wǎng)站建設(shè)公司,為黟縣等地區(qū)提供建站服務(wù)。全流程按需網(wǎng)站開發(fā),專業(yè)設(shè)計,全程項目跟蹤,創(chuàng)新互聯(lián)專業(yè)和態(tài)度為您提供的服務(wù)
iOS 應(yīng)用程序狀態(tài)
-
Active - 應(yīng)用程序在前臺運行
-
Background - 應(yīng)用程序在后臺運行。用戶正在使用另一個應(yīng)用程序或者在主屏幕上。
-
Inactive - 這是一種過渡狀態(tài),目前不會在React Native的應(yīng)用程序上發(fā)生。
想要獲取更多的信息,見 Apple's documentation
基本用法
為了查看當(dāng)前的狀態(tài),你可以檢查 AppStateIOS.currentState,該方法會一直保持最新狀態(tài)。然而,當(dāng) AppStateIOS在橋接器上檢索currentState時,在啟動時它將會為空。
getInitialState: function() {
return {
currentAppState: AppStateIOS.currentState,
};
}, componentDidMount: function() {
AppStateIOS.addEventListener('change', this._handleAppStateChange);
}, componentWillUnmount: function() {
AppStateIOS.removeEventListener('change', this._handleAppStateChange);
}, _handleAppStateChange: function(currentAppState) {
this.setState({ currentAppState, });
}, render: function() {
return (
Current state is: {this.state.currentAppState}
);
},這個例子似乎只能說"當(dāng)前狀態(tài)是:活躍的"因為在 active 狀態(tài)時,應(yīng)用程序只對用戶是可見的,空狀態(tài)只能是暫時的。
方法
static addEventListener(type: string, handler: Function)
通過監(jiān)聽 change 事件類型和提供處理程序,為應(yīng)用程序狀態(tài)變化添加一個處理程序。
static removeEventListener(type: string, handler: Function)
通過傳遞 change 事件類型和處理程序,刪除一個處理程序。
例子
Edit on GitHub
'use strict'; var React = require('react-native'); var {
AppStateIOS,
Text,
View
} = React; var AppStateSubscription = React.createClass({
getInitialState() { return {
appState: AppStateIOS.currentState,
previousAppStates: [],
};
},
componentDidMount: function() {
AppStateIOS.addEventListener('change', this._handleAppStateChange);
},
componentWillUnmount: function() {
AppStateIOS.removeEventListener('change', this._handleAppStateChange);
},
_handleAppStateChange: function(appState) { var previousAppStates = this.state.previousAppStates.slice();
previousAppStates.push(this.state.appState); this.setState({
appState,
previousAppStates,
});
},
render() { if (this.props.showCurrentOnly) { return (
{this.state.appState}
);
} return (
{JSON.stringify(this.state.previousAppStates)}
);
}
});
exports.title = 'AppStateIOS';
exports.description = 'iOS app background status';
exports.examples = [
{
title: 'AppStateIOS.currentState',
description: 'Can be null on app initialization',
render() { return {AppStateIOS.currentState} ; }
},
{
title: 'Subscribed AppStateIOS:',
description: 'This changes according to the current state, so you can only ever see it rendered as "active"',
render(): ReactElement { return ; }
},
{
title: 'Previous states:',
render(): ReactElement { return ; }
},
]; 分享標(biāo)題:創(chuàng)新互聯(lián)React教程:iOS應(yīng)用程序狀態(tài)
轉(zhuǎn)載源于:http://m.5511xx.com/article/cojsgcj.html


咨詢
建站咨詢
