【问题标题】:ios applicationwillenterbackground not being calledios applicationwillenterbackground 未被调用
【发布时间】:2012-03-23 11:38:44
【问题描述】:

当我退出或最小化我的应用程序时,我试图保存我的数据,但似乎从未调用过 applicationwillenterbackground。我需要做一些具体的事情吗?

我已经在 detailview.h 类中设置了<UIApplicationDelegate>,但仍然没有运气。

顺便说一句,如果这是相关的,我正在使用 Master-DetailView 模板。

【问题讨论】:

    标签: ios background


    【解决方案1】:

    使用-applicationDidEnterBackground: 保存用户数据。这也应该在-applicationWillTerminate: 中完成,我猜如果电池没电了,它可能会被调用。有关更多信息,请参阅此问题:ApplicationWillTerminate in iOS 4.0

    对于我的应用,我对这个问题进行了广泛研究,发现除了实现上述两种方法外,我还必须检查设备是否支持多任务处理,否则数据会被保存两次(例如在 iPhone 3G 上)。所以这就是我的代码部分的样子:

    - (void) applicationDidEnterBackground:(UIApplication*)application
    {
        // This is called on all iOS 4 devices, even when they don't support multi-tasking.
        // But we want to avoid saving data twice, so we check.
        if ([[UIDevice currentDevice] respondsToSelector:@selector(isMultitaskingSupported)])
            [model serializeEntries];
    }
    
    - (void) applicationWillTerminate:(UIApplication*)application
    {
        // This is always called on devices that don't support multi-tasking,
        // but also in low-memory conditions and when the app has to be quit for some
        // other reason.
        [model serializeEntries];
    }
    

    【讨论】:

    • 我确实调用了这些,但都没有执行。我是否需要像在 textview 中那样设置委托,即 [self.textView setDelegate:self]?
    • 是否有可能您没有在应用程序委托中定义这些方法?只有一个实现 UIApplicationDelegate 协议的类才能获得这些消息。这个类还必须定义一个-application:didFinishLaunchingWithOptions,并且通常有一个以AppDelegate 结尾的名称。这有帮助吗?
    猜你喜欢
    • 1970-01-01
    • 2013-12-05
    • 2015-05-22
    • 2014-12-14
    • 2013-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多