在swift中检测应用程序是在后台还是前台 [英] Detect if the application in background or foreground in swift

查看:683
本文介绍了在swift中检测应用程序是在后台还是前台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有没有办法知道我的应用程序的状态,如果它处于后台模式或前台。谢谢

is there any way to know the state of my application if it is in background mode or in foreground . Thanks

推荐答案

[UIApplication sharedApplication] .applicationState 将返回当前状态应用程序,

[UIApplication sharedApplication].applicationState will return current state of applications,


  • UIApplicationStateActive

  • UIApplicationStateInactive

  • UIApplicationStateBackground

我们需要打电话

let state: UIApplicationState = UIApplication.sharedApplication().applicationState // or use  let state =  UIApplication.sharedApplication().applicationState

if state == .Background {

// background
}
else if state == .Active {

// foreground
}

swift 3及以上

let state = UIApplication.shared.applicationState

    if state == .background {

        // background
    }
    else if state == .active {

        // foreground
    }

这篇关于在swift中检测应用程序是在后台还是前台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆