Objective c “性能选择器”;背景刷新状态“;iOS 7上的崩溃

Objective c “性能选择器”;背景刷新状态“;iOS 7上的崩溃,objective-c,ios7,xcode5,Objective C,Ios7,Xcode5,我正在使用Xcode 5/iOS SDK 6.1构建。如果应用程序在iOS 7.x设备上运行,则应检查是否为应用程序设置了“设置->常规->背景AppRefresh”。由于此属性仅在iOS 7上可用,我正在执行以下操作: if([[UIApplication sharedApplication] respondsToSelector:@selector(backgroundRefreshStatus)]) { NSInteger outcome=[[[UIApplication shar

我正在使用Xcode 5/iOS SDK 6.1构建。如果应用程序在iOS 7.x设备上运行,则应检查是否为应用程序设置了“设置->常规->背景AppRefresh”。由于此属性仅在iOS 7上可用,我正在执行以下操作:

if([[UIApplication sharedApplication] respondsToSelector:@selector(backgroundRefreshStatus)])
{
    NSInteger outcome=[[[UIApplication sharedApplication] performSelector:@selector(backgroundRefreshStatus)] integerValue];
    //do something with "outcome"
}

然而。。。应用程序在iOS 7上的“performSelector”行崩溃,这很奇怪,因为它通过了“respondsToSelector”调用??有人知道为什么吗?我还尝试了NSSelectorFromString(@“backgroundRefreshStatus”),结果相同。

您正在使用字符串作为选择器。尝试不使用字符串:

UIApplication *app = [UIApplication sharedApplication];
if([app respondsToSelector:@selector(backgroundRefreshStatus)])
{
    UIBackgroundRefreshStatus outcome = [app performSelector:@selector(backgroundRefreshStatus)];
    // or outcome = [app backgroundRefreshStatus]
}

这里有很多不必要的代码。除非iOS 7之前存在作为私有API的
backgroundRefreshStatus
选择器,否则不需要进行版本检查

您使用的
@selector
也不正确,您不需要使用
performSelector
,只需调用以下方法:

if ([[UIApplication sharedApplication] respondsToSelector:@selector(backgroundRefreshStatus)]) {
    UIBackgroundRefreshStatus refreshStatus = [[UIApplication sharedApplication] backgroundRefreshStatus];
}

您需要id Output=[[UIApplication sharedApplication]性能选择器:@selector(backgroundRefreshStatus)];如果使用ARC,为
id
指定一个整数可能会崩溃。如果要使用iOS 7功能,为什么要使用6.1?谢谢您的评论。我编辑了原文。它仍然在iOS 7设备上崩溃。关于字符串,你是对的。这是我的错,因为在我的代码中它是正确的。我改变了我原来的帖子。您的解决方案的问题是“UIBackgroundRefreshStatus”在iOS 6.1 SDK中不可用,因此无法编译。我尝试的是:NSInteger status=[[UIApplication sharedApplication]performSelector:@selector(backgroundrefreshtstatus)]integerValue];但是没有成功。它不会用iOS 6 SDK编译,因为这里既没有定义“UIBackgroundRefreshStatus”也没有定义“backgroundRefreshStatus”!您应该真正使用iOS 7 SDK进行开发,并通过使用诸如检查接收器是否响应选择器等技术来实现与iOS 6的向后兼容性,而不是使用旧SDK并尝试“向前兼容”。如果您坚持使用较旧的SDK(这对于实现与iOS 6和iOS 7的兼容性不是必需的),那么在这种情况下,您将不得不使用它,因为UIBackgroundRefreshStatus是一种基本类型,非常混乱。