2013-08-29 44 views
40

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

回答

69

这是你在找什么。

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."); 
} 
+2

谢谢,这是有益的。然而,你可以评论我在这方面的依赖问题 - - http://stackoverflow.com/questions/19235183/uiapplicationbackgroundrefreshstatusdidchangenotification-usage-without-correspo – Ashok

+0

嗨,被称为thiS方法。在后台它未能呼吁 –

+0

我'米猜测背景appRefresh无关的无声通知是正确的? – Honey

8

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

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

3

更新了斯威夫特3和iOS10:

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