gibt es eine Möglichkeit, den Status meiner Anwendung zu erfahren, wenn sie sich im Hintergrundmodus oder im Vordergrund befindet. Vielen Dank
[UIApplication sharedApplication].applicationState
gibt den aktuellen Status der Anwendungen zurück.
oder wenn Sie per Benachrichtigung darauf zugreifen möchten, siehe UIApplicationDidBecomeActiveNotification
wir müssen gerne anrufen
Swift 3 und höher
let state = UIApplication.shared.applicationState
if state == .background || state == .inactive{
// background
}else if state == .active {
// foreground
}
andere Option :
switch UIApplication.shared.applicationState{
case .background,.inactive :
// backgound
break
case .active :
// foreground
break
default:
break
}
Ziel C
UIApplicationState state = [[UIApplication sharedApplication] applicationState];
if (state == UIApplicationStateBackground || state == UIApplicationStateInactive)
{
// background
}else if (state == UIApplicationStateActive)
{
// foreground
}
Swift 3
let state: UIApplicationState = UIApplication.shared.applicationState
if state == .background {
// background
}
else if state == .active {
// foreground
}
Swift 4
let state = UIApplication.shared.applicationState
if state == .background {
print("App in Background")
}else if state == .active {
print("App in Foreground or Active")
}
sie können einen Booleschen Wert hinzufügen, wenn die Anwendung im Hintergrund oder im Vordergrund eingegeben wird. Sie haben diese Informationen, indem Sie den App-Delegaten verwenden.
Gemäß der Apple-Dokumentation können Sie möglicherweise auch die Eigenschaft mainWindow Ihrer Anwendung oder die Eigenschaft active status der App verwenden.
Diskussion Der Wert in dieser Eigenschaft ist null, wenn das Storyboard oder die Nib-Datei der App noch nicht vollständig geladen wurde. Es kann auch gleich null sein, wenn die App inaktiv oder verborgen ist.
Wenn jemand es in Swift 3.0 will
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
}
für Swift 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
}