0

当应用程序在后台时,我也需要获取 batteryLevelDidChangeNotification 值。我已经在 applicationDidEnterBackground 中编写了代码,但当电池电量变化时我也没有受到打击。我也在后台模式下检查了后台获取功能。 在此处输入图像描述 这是我的代码

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
       
        UIDevice.current.isBatteryMonitoringEnabled = true
        NotificationCenter.default.addObserver(self, selector: #selector(batteryLevelDidChange), name: UIDevice.batteryLevelDidChangeNotification, object: nil)
       
        return true
    }

@objc func batteryLevelDidChange(_ notification: Notification) {
        print(batteryLevel)
    }
  func applicationDidEnterBackground(_ application: UIApplication) {
      
        UIDevice.current.isBatteryMonitoringEnabled = true
        NotificationCenter.default.addObserver(self, selector: #selector(batteryLevelDidChange), name: UIDevice.batteryLevelDidChangeNotification, object: nil)
        
    }
    
4

0 回答 0