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

46

从iOS 7开始,苹果的多任务API允许应用程序在三种新的后台模式下运行:后台获取、远程通知内容和后台传输服务。同时,苹果也允许iOS用户控制所有应用程序是否允许在后台运行,或者单个应用程序是否可以在后台运行(设置>通用>后台应用程序刷新)。我的应用程序是否有一种编程方式可以检测用户是否禁用了我的应用程序在后台刷新的能力?


苹果公司的家长控制页面包括启用/禁用限制,特别是后台应用程序刷新:https://support.apple.com/zh-cn/HT201304。有一个指南比在iPhone设置迷宫中徘徊要好。 - Eliot Gillum
3个回答

80

这就是您要找的内容。

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
谢谢,这很有用。不过,您能否就我的相关从属问题发表评论 - https://dev59.com/QHfZa4cB1Zd3GeqPU7XK - Ashok
嗨,我们该在哪里调用这个方法?在后台它无法被调用。 - Gopal Raju
1
我猜测后台应用程序刷新与静默通知没有任何关系,是这样吗? - mfaani

9

已更新至Swift 3和iOS10:

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

8
请检查 UIApplication 的 backgroundRefreshStatus 属性。以下内容摘自 Apple 文档。
该属性反映应用程序是否可以被启动到后台,以处理后台行为,例如处理后台位置更新和执行后台获取。如果您的应用程序依赖于在后台启动以执行任务,则可以使用此属性的值来确定是否可能并警告用户如果不能。如果此属性的值设置为 UIBackgroundRefreshStatusRestricted,请不要警告用户;受限制的用户没有启用应用程序的多任务处理能力。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接