在iOS 7中检测后台应用程序刷新的用户设置

从iOS 7开始,Apple的多任务API允许应用程序以三种新的后台模式运行:后台提取,远程通知内容和后台传输服务。 苹果还为iOS用户提供了控制是否允许所有应用程序在后台运行,或者是否可以在后台运行单个应用程序(设置>常规>后台应用程序刷新)的function。 有没有办法让我的应用程序以编程方式检测用户是否禁用了我的应用程序在后台刷新的能力?

这是你正在寻找的。

if ([[UIApplication sharedApplication] backgroundRefreshStatus] == UIBackgroundRefreshStatusAvailable) { NSLog(@"Background updates are available for the app."); }else if([[UIApplication sharedApplication] backgroundRefreshStatus] == UIBackgroundRefreshStatusDenied) { NSLog(@"The user explicitly disabled background behavior for this app or for the whole system."); }else if([[UIApplication sharedApplication] backgroundRefreshStatus] == UIBackgroundRefreshStatusRestricted) { NSLog(@"Background updates are unavailable and the user cannot enable them again. For example, this status can occur when parental controls are in effect for the current user."); } 

检查UIApplication的backgroundRefreshStatus属性。 以下是苹果文档引用。

此属性反映应用程序是否可以启动到后台来处理后台行为,如处理后台位置更新和执行后台提取。 如果您的应用程序依赖于启动到后台执行任务,则可以使用此属性的值来确定是否可以执行此操作,如果不是,则可以警告用户。 如果此属性的值设置为UIBackgroundRefreshStatusRestricted,则不要警告用户; 受限用户无法为应用程序启用多任务处理。

更新了Swift 3和iOS10:

 switch UIApplication.shared.backgroundRefreshStatus { case .available: print("Refresh available") case .denied: print("Refresh denied") case .restricted: print("Refresh restricted") }