NSNotificationCenter addObserver in Swift

后端 未结 14 1993
难免孤独
难免孤独 2020-11-22 05:23

How do you add an observer in Swift to the default notification center? I\'m trying to port this line of code that sends a notification when the battery level changes.

14条回答
  •  一向
    一向 (楼主)
    2020-11-22 06:12

    In swift 3, Xcode 8.2:- checking battery state level

    //Add observer
    NotificationCenter.default.addObserver(self, selector: #selector(batteryStateDidChange), name: NSNotification.Name.UIDeviceBatteryStateDidChange, object: nil)
    
    
     //Fired when battery level changes
    
     func batteryStateDidChange(notification: NSNotification){
            //perform manipulation here
        }
    

提交回复
热议问题