简体   繁体   English

iOS3 中 applicationWillEnterForeground 上的 EXEC_BAD_ACCESS 但不是 iOS4

[英]EXEC_BAD_ACCESS on applicationWillEnterForeground in iOS3 but not iOS4

I have my application working great on my iOS 4.3.3 iPhone 3GS.我的应用程序在我的 iOS 4.3.3 iPhone 3GS 上运行良好。 When I test the app on a 3.1.3 iPhone 3G, the program crashes right after the splash image is shown.当我在3.1.3 iPhone 3G 上测试应用程序时,程序在显示初始图像后立即崩溃。 The debugger points to the last command of my root view controller's awakeFromNib :调试器指向我的根视图控制器awakeFromNib的最后一个命令:

- (void)awakeFromNib
{
    NSLog(@"awakeFromNib");
    NSLog(@"applicationWillEnterForeground listened");
    [[NSNotificationCenter defaultCenter] addObserver:self 
                                             selector:@selector(applicationWillEnterForeground:)
                                                 name:UIApplicationWillEnterForegroundNotification
                                               object: nil];
}

. .

2011-08-09 15:56:24.585 AppName[4401:207] awakeFromNib
2011-08-09 15:56:24.602 AppName[4401:207] applicationWillEnterForeground listened
Program received signal:  “EXC_BAD_ACCESS”.

Is there something special about iOS 3's awaking/sleeping that I should know that would help me work around this problem? iOS 3 的唤醒/睡眠有什么特别之处,我应该知道这将帮助我解决这个问题吗?

From the iOS Developer library:来自 iOS 开发人员库:

UIApplicationWillEnterForegroundNotification

Posted shortly before an application leaves the background state on its way to becoming the active application.在应用程序离开后台 state 成为活动应用程序之前不久发布。 The object of the notification is the UIApplication object.通知的 object 是 UIApplication object。 There is no userInfo dictionary.没有 userInfo 字典。

Availability可用性

Available in iOS 4.0 and later .在 iOS 4.0 及更高版本中可用

This probably causes the EXEC_BAD_ACCESS.这可能会导致 EXEC_BAD_ACCESS。 Does it crash if you remove that line of code?如果删除那行代码,它会崩溃吗?

The problem is that the identifier UIApplicationWillEnterForegroundNotification is pointing to an externally defined string that only exists on iOS 4 or later.问题在于标识符UIApplicationWillEnterForegroundNotification指向一个外部定义的字符串,该字符串仅存在于 iOS 4 或更高版本上。 On iOS 3 and earlier, it will evaluate to nil;在 iOS 3 及更早版本上,它将评估为零; thus, you are passing in nil for a notification name, which is why adding the observer is crashing.因此,您将 nil 传递给通知名称,这就是添加观察者崩溃的原因。

You can fix this in two ways.您可以通过两种方式解决此问题。 You could directly use the string value of the notification's name in your code:您可以在代码中直接使用通知名称的字符串值:

[[NSNotificationCenter defaultCenter]
 addObserver:self 
 selector:@selector(applicationWillEnterForeground:)
 name:@"UIApplicationWillEnterForeground" // might not be correct
 object:nil];

I'm not sure if that's what it is, you'll have to check the docs or use NSLog to be exactly sure of it.我不确定是否是这样,您必须检查文档或使用 NSLog 才能完全确定。

A better option is to check the value of the identifier first, and only add a listener if it is supported:更好的选择是首先检查标识符的值,并且仅在支持的情况下添加侦听器:

if (UIApplicationWillEnterForegroundNotification) {
    [[NSNotificationCenter defaultCenter]
     addObserver:self 
     selector:@selector(applicationWillEnterForeground:)
     name:UIApplicationWillEnterForegroundNotification
     object:nil];
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM