有没有办法检查iOS应用程序是否在后台?
我想检查应用程序是否在后台运行 在:有没有办法检查iOS应用程序是否在后台?,ios,objective-c,uiapplicationdelegate,application-lifecycle,ios-background-mode,Ios,Objective C,Uiapplicationdelegate,Application Lifecycle,Ios Background Mode,我想检查应用程序是否在后台运行 在: 应用程序委托获取指示状态转换的回调。你可以根据这个来追踪它 UIApplication中的属性还返回当前状态 [[UIApplication sharedApplication] applicationState] 这可能会帮助你解决问题 请参阅下面的评论-inactive是一个相当特殊的情况,可能意味着该应用程序正在被推向前台。这可能意味着“背景”,也可能不意味着“背景”,这取决于您的目标…如果您希望接收回调而不是“询问”应用程序状态,请在AppDele
应用程序委托获取指示状态转换的回调。你可以根据这个来追踪它 UIApplication中的属性还返回当前状态
[[UIApplication sharedApplication] applicationState]
这可能会帮助你解决问题
请参阅下面的评论-inactive是一个相当特殊的情况,可能意味着该应用程序正在被推向前台。这可能意味着“背景”,也可能不意味着“背景”,这取决于您的目标…如果您希望接收回调而不是“询问”应用程序状态,请在
AppDelegate
中使用这两种方法:
- (void)applicationDidBecomeActive:(UIApplication *)application {
NSLog(@"app is actvie now");
}
- (void)applicationWillResignActive:(UIApplication *)application {
NSLog(@"app is not actvie now");
}
Swift版本:
let state = UIApplication.shared.applicationState
if state == .Background {
print("App in Background")
}
Swift 3
let state = UIApplication.shared.applicationState
if state == .background {
print("App in Background")
}
斯威夫特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
}
Swift 4.0扩展,使访问更容易:
import UIKit
extension UIApplication {
var isBackground: Bool {
return UIApplication.shared.applicationState == .background
}
}
要从应用程序中访问,请执行以下操作:
let myAppIsInBackground = UIApplication.shared.isBackground
如果您正在查找各种状态的信息(active
、inactive
和background
),您可以找到。Swift 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
}
你说的是
locationManager:didUpdateToLocation:fromLocation:
方法吗?谢谢-为了更清楚,它是[[UIApplication sharedApplication]应用程序状态]==UIApplicationState背景。我想[[UIApplication sharedApplication]应用程序状态]!=UIApplicationStateActive
更好,由于UIApplicationStateInactive几乎等同于处于后台…此处详细说明了状态:我发现,如果出于后台获取目的而启用应用程序,则不会调用转换回调。需要注意的是,applicationWillEnterForeground在ApplicationIDBecomeActive之前被调用。因此,从ApplicationWillEnter前台中检查applicationState将返回UIApplicationStateBackground。这让我有点扫兴。因此,我使用了上述解决方案的组合,通过在ApplicationIDBecomeActive中检查applicationState,而不是(错误地)检查applicationState在applicationWillEnterForeground中检查applicationState for UIApplicationStateBackground。我有同样的解决方案,它适用于需要另一个线程/队列的状态信息的情况。在iOS 14中似乎没有做任何事情,这些方法不会为我调用。好吧,我在2015年写了这篇文章,可能是在iOS 8上测试过的…很好,很有用的答案。从docs:UIApplicationStateInactive工作-应用程序正在前台运行,但未接收事件。这可能是由于中断或应用程序正在转换到后台或从后台转换而发生的。虽然此代码可能会回答问题,提供有关此代码为什么和/或如何回答此问题的其他上下文可提高其长期价值。由于没有人为swift 4编写帮助swift 4人的正文,因此代码将检查您是否在后台使用该应用程序!假设你在后台收到通知,你想做一些事情,比如在后台收到通知时执行一些操作,那么内部代码将执行,反之亦然?我可以检查==.inactive | |==.active以查看它是否在前台(而不是在后台线程上打开)?
let myAppIsInBackground = UIApplication.shared.isBackground
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
}