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

Syd*_*ney 45 settings background objective-c multitasking ios7

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

dub*_*ike 77

这就是你要找的东西.

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.");
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,这很有用.但是,你能否就这方面的依赖性问题发表评论 - http://stackoverflow.com/questions/19235183/uiapplicationbackgroundrefreshstatusdidchangenotification-usage-without-correspo (2认同)

小智 8

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

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


And*_*w K 7

针对Swift 3和iOS10进行了更新:

switch UIApplication.shared.backgroundRefreshStatus {
case .available:
    print("Refresh available")
case .denied:
    print("Refresh denied")
case .restricted:
    print("Refresh restricted")
}
Run Code Online (Sandbox Code Playgroud)