ios 快速检测应用程序是在后台还是前台
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/38971994/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Detect if the application in background or foreground in swift
提问by Mohammed Aboelwafa
is there any way to know the state of my application if it is in background mode or in foreground . Thanks
有没有办法知道我的应用程序的状态,如果它处于后台模式或前台。谢谢
回答by Anbu.Karthik
[UIApplication sharedApplication].applicationState
will return current state of applications such as:
[UIApplication sharedApplication].applicationState
将返回应用程序的当前状态,例如:
- UIApplicationStateActive
- UIApplicationStateInactive
- UIApplicationStateBackground
- UIApplicationStateActive
- UIApplicationStateInactive
- UIApplicationStateBackground
or if you want to access via notification see UIApplicationDidBecomeActiveNotification
或者如果您想通过通知访问,请参阅UIApplicationDidBecomeActiveNotification
Swift 3+
斯威夫特 3+
let state = UIApplication.shared.applicationState
if state == .background || state == .inactive {
// background
} else if state == .active {
// foreground
}
switch UIApplication.shared.applicationState {
case .background, .inactive:
// background
case .active:
// foreground
default:
break
}
Objective C
目标 C
UIApplicationState state = [[UIApplication sharedApplication] applicationState];
if (state == UIApplicationStateBackground || state == UIApplicationStateInactive) {
// background
} else if (state == UIApplicationStateActive) {
// foreground
}
回答by dimo hamdy
Swift 3
斯威夫特 3
let state: UIApplicationState = UIApplication.shared.applicationState
if state == .background {
// background
}
else if state == .active {
// foreground
}
回答by Ashu
Swift 4
斯威夫特 4
let state = UIApplication.shared.applicationState
if state == .background {
print("App in Background")
}else if state == .active {
print("App in Foreground or Active")
}
回答by Hamid Shahsavari
If somebody wants it in swift 3.0
如果有人想要 swift 3.0
switch application.applicationState {
case .active:
//app is currently active, can update badges count here
break
case .inactive:
//app is transitioning from background to foreground (user taps notification), do what you need when user taps here
break
case .background:
//app is in background, if content-available key of your notification is set to 1, poll to your backend to retrieve data and update your interface here
break
default:
break
}
for swift 4
快速 4
switch UIApplication.shared.applicationState {
case .active:
//app is currently active, can update badges count here
break
case .inactive:
//app is transitioning from background to foreground (user taps notification), do what you need when user taps here
break
case .background:
//app is in background, if content-available key of your notification is set to 1, poll to your backend to retrieve data and update your interface here
break
default:
break
}
回答by Atlandu
you can add a boolean when the application enter in background or enter in foreground. You have this information by using the App delegate.
当应用程序进入后台或进入前台时,您可以添加一个布尔值。您可以使用 App 委托获得此信息。
According the Apple documentation, maybe you can also use the mainWindow property of your Application or the active status property of the app.
根据 Apple 文档,也许您还可以使用应用程序的 mainWindow 属性或应用程序的活动状态属性。
Discussion The value in this property is nil when the app's storyboard or nib file has not yet finished loading. It might also be nil when the app is inactive or hidden.
讨论 当应用程序的故事板或 nib 文件尚未完成加载时,此属性中的值为 nil。当应用程序处于非活动状态或隐藏状态时,它也可能为零。