ios - AppDelegate applicationDidEnterBackground 在模拟器上工作但不在设备上工作

标签 ios swift appdelegate parse-server

我试图跟踪用户何时在线或离线。我试图通过每当打开应用程序以及每当使用以下代码关闭应用程序时更新解析服务器中的用户对象来实现此目的。

 func applicationWillEnterForeground(_ application: UIApplication) {

        let username : String? = UserDefaults.standard.string(forKey: "username")
        if username != nil {
            print("online")
            let object = PFUser.current()
            object!["online"] = "yes"
            object?.saveInBackground()

        }

    }

    func applicationDidEnterBackground(_ application: UIApplication) {

        let username : String? = UserDefaults.standard.string(forKey: "username")
        if username != nil {
            print("Offline")
            let object = PFUser.current()
            object!["online"] = "no"
            object?.saveInBackground()

        }
    }

当我在 Mac 上使用模拟器进行测试时,它似乎工作得很好。然而,当我在我的实际 iPhone 上测试它时,它会在应用程序打开时更新,但在关闭时不会更新。 (打印总是在设备和模拟器上打印,因此该函数被调用)

经过一些研究后,我发现我可能在 info.plist 中需要这个应用程序不在后台运行所以我添加了它并将其设置为否。但它仍然无法正常工作。

最佳答案

为什么不使用另一种应用程序状态方法,例如 func applicationDidEnterBackground(_ application: UIApplication) {}

请记住,applicationWillResignActive(:) 的调用早于 applicationDidEnterBackground(:)

关于ios - AppDelegate applicationDidEnterBackground 在模拟器上工作但不在设备上工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51641482/

相关文章:

ios - 只更新一次位置

ios - objective C block 到 swift block

ios - 在 Stackview 中设置项目的 ID

ios - 从 AppDelegate Swift 4 更新 tableView 行

swift - 在 appDelegate 中设置 UITabBarItem 徽章值

multithreading - CLLocationmanager从后台线程更新

ios - 未调用 targetContentOffsetForProposedContentOffset

ios - 使用 Parse 进行查询

ios - (iOS) 当应用程序不再运行时取消所有本地通知

ios - URLSessionDownloadDelegate didFinishDownloadingTo 未在后台调用