ios 有没有办法检查iOS应用程序是否在后台?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5835806/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-30 19:45:49  来源:igfitidea点击:

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

iosobjective-cuiapplicationdelegateapplication-lifecycleios-background-mode

提问by bobby grenier

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

我想检查应用程序是否在后台运行。

In:

在:

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

回答by DavidN

App delegate gets callbacks indicating state transitions. You can track it based on that.

应用程序委托获取指示状态转换的回调。您可以基于此进行跟踪。

Also the applicationStateproperty in UIApplication returns the current state.

UIApplication 中的applicationState属性也返回当前状态。

[[UIApplication sharedApplication] applicationState]

回答by Aswathy Bose

UIApplicationState state = [[UIApplication sharedApplication] applicationState];
if (state == UIApplicationStateBackground || state == UIApplicationStateInactive)
{
   //Do checking here.
}

This may help you in solving your problem.

这可能会帮助您解决问题。

See comment below - inactive is a fairly special case, and can mean that the app is in the process of being launched into the foreground. That may or may not mean "background" to you depending on your goal...

请参阅下面的评论 - 不活动是一个相当特殊的情况,可能意味着应用程序正在被启动到前台。根据您的目标,这对您来说可能意味着也可能不意味着“背景”......

回答by Meet

Swift 3

斯威夫特 3

    let state = UIApplication.shared.applicationState
    if state == .background {
        print("App in Background")
    }

回答by ioopl

Swift version :

斯威夫特版:

let state = UIApplication.sharedApplication().applicationState
if state == .Background {
    print("App in Background")
}

回答by klimat

If you prefer to receive callbacks instead of "ask" about the application state, use these two methods in your AppDelegate:

如果您更喜欢接收回调而不是“询问”应用程序状态,请在您的AppDelegate.

- (void)applicationDidBecomeActive:(UIApplication *)application {
    NSLog(@"app is actvie now");
}


- (void)applicationWillResignActive:(UIApplication *)application {
    NSLog(@"app is not actvie now");
}

回答by Shakeel Ahmed

swift 5

迅捷 5

let state = UIApplication.shared.applicationState
    if state == .background {
        print("App in Background")
        //MARK: - if you want to perform come action when app in background this will execute 
        //Handel you code here
    }
    else if state == .foreground{
        //MARK: - if you want to perform come action when app in foreground this will execute 
        //Handel you code here
    }

回答by Raj Kumar

Swift 4+

斯威夫特 4+

let appstate = UIApplication.shared.applicationState
        switch appstate {
        case .active:
            print("the app is in active state")
        case .background:
            print("the app is in background state")
        case .inactive:
            print("the app is in inactive state")
        default:
            print("the default state")
            break
        }

回答by CodeBender

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

一个 Swift 4.0 扩展,使访问更容易:

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, inactiveand background), you can find the Apple documentation here.

如果您正在寻找有关各种状态(activeinactivebackground)的信息,可以在此处找到Apple 文档