从iOS 7开始,苹果的多任务API允许应用程序在三种新的后台模式下运行:后台获取、远程通知内容和后台传输服务。同时,苹果也允许iOS用户控制所有应用程序是否允许在后台运行,或者单个应用程序是否可以在后台运行(设置>通用>后台应用程序刷新)。我的应用程序是否有一种编程方式可以检测用户是否禁用了我的应用程序在后台刷新的能力?
从iOS 7开始,苹果的多任务API允许应用程序在三种新的后台模式下运行:后台获取、远程通知内容和后台传输服务。同时,苹果也允许iOS用户控制所有应用程序是否允许在后台运行,或者单个应用程序是否可以在后台运行(设置>通用>后台应用程序刷新)。我的应用程序是否有一种编程方式可以检测用户是否禁用了我的应用程序在后台刷新的能力?
这就是您要找的内容。
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.");
}
已更新至Swift 3和iOS10:
switch UIApplication.shared.backgroundRefreshStatus {
case .available:
print("Refresh available")
case .denied:
print("Refresh denied")
case .restricted:
print("Refresh restricted")
}