创新互联React教程:iOS应用程序状态

AppStateIOS 可以告诉你应用程序是在前台还是在后台,而且状态更新时会通知你。 在处理推送通知时,AppStateIOS 经常被用于判断目标和适当的行为。

创新互联专注于黟县企业网站建设,响应式网站开发,商城网站制作。黟县网站建设公司,为黟县等地区提供建站服务。全流程按需网站开发,专业设计,全程项目跟踪,创新互联专业和态度为您提供的服务

iOS 应用程序状态

  • Active - 应用程序在前台运行

  • Background - 应用程序在后台运行。用户正在使用另一个应用程序或者在主屏幕上。

  • Inactive - 这是一种过渡状态,目前不会在React Native的应用程序上发生。

想要获取更多的信息,见 Apple's documentation

基本用法

为了查看当前的状态,你可以检查 AppStateIOS.currentState,该方法会一直保持最新状态。然而,当 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}
      );
    },

这个例子似乎只能说"当前状态是:活跃的"因为在 active 状态时,应用程序只对用户是可见的,空状态只能是暂时的。

方法

static addEventListener(type: string, handler: Function)

通过监听 change 事件类型和提供处理程序,为应用程序状态变化添加一个处理程序。

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 ; }
      },
    ];

当前名称:创新互联React教程:iOS应用程序状态
转载源于:http://www.hantingmc.com/qtweb/news37/273837.html

网站建设、网络推广公司-创新互联,是专注品牌与效果的网站制作,网络营销seo公司;服务项目有等

广告

声明:本网站发布的内容(图片、视频和文字)以用户投稿、用户转载内容为主,如果涉及侵权请尽快告知,我们将会在第一时间删除。文章观点不代表本网站立场,如需处理请联系客服。电话:028-86922220;邮箱:631063699@qq.com。内容未经允许不得转载,或转载时需注明来源: 创新互联