gpt4 book ai didi

objective-c - applicationWillResignActive 通知未在设备上捕获

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:04:55 24 4
gpt4 key购买 nike

我试图在应用程序关闭时将一个简单的字符串保存在一个简单的 .plist 文件中。

这是我的代码:

- (void)viewDidLoad {
NSString *filePath = [self dataFilePath];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath]) {
NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:filePath];
self.kmOld.text = [array objectAtIndex:0];

[array release];
}


[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(applicationWillResignActive:)
name:UIApplicationWillResignActiveNotification
object:NULL];
[super viewDidLoad];

然后按照applicationWillResignActive方法:

 - (void)applicationWillResignActive:(NSNotification *)notification {

NSString *text = [[NSString alloc]initWithFormat:@"%@",kmNew.text];
NSLog(@"%@",text);

if ([text intValue]>0) {
NSArray *array = [[NSArray alloc] initWithObjects:text, nil];

BOOL success = [array writeToFile:[self dataFilePath] atomically:YES];
NSLog(@"%d",success);
[array release];
}
[text release];
}

这在模拟器上运行良好,但它似乎被设备忽略了......哪里有问题?谢谢...

最佳答案

看看http://www.cocoanetics.com/2010/07/understanding-ios-4-backgrounding-and-delegate-messaging/以获得 UIApplication 生命周期的良好概述。根据您的应用程序运行的 iOS 版本以及导致您的应用程序终止/后台运行的操作,您可能没有收听正确的通知,并且可能还需要观察 UIApplicationWillTerminateNotification。

iOS <4

iOS 4+

关于objective-c - applicationWillResignActive 通知未在设备上捕获,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7757025/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com