Is there any way to check if iOS app is in background?

后端 未结 8 972
滥情空心
滥情空心 2020-12-04 10:38

I want to check if the app is running in the background.

In:

locationManagerDidUpdateLocation {
    if(app is runing in background){
        do this
         


        
8条回答
  •  余生分开走
    2020-12-04 11:04

    A Swift 4.0 extension to make accessing it a bit easier:

    import UIKit
    
    extension UIApplication {
        var isBackground: Bool {
            return UIApplication.shared.applicationState == .background
        }
    }
    

    To access from within your app:

    let myAppIsInBackground = UIApplication.shared.isBackground
    

    If you are looking for information on the various states (active, inactive and background), you can find the Apple documentation here.

提交回复
热议问题