Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WatchOS: getting applicationDidBecomeActive notifications

I'm making a framework that needs to do stuff when my apple watch is entering background and foreground.

I'm looking for an equivalent of this iOS code for the Apple watch since UIApplication is not present in UIKit anymore :

let notificationCenter = NSNotificationCenter.defaultCenter()
notificationCenter.addObserver(self, selector: "applicationDidEnterBackground", name: UIApplicationDidEnterBackgroundNotification, object: nil)

any help would be nice

like image 308
HaneTV Avatar asked Jan 06 '16 11:01

HaneTV


2 Answers

As of watchOS 7 the following have been added:

WKExtension.applicationDidBecomeActiveNotification
WKExtension.applicationDidEnterBackgroundNotification
WKExtension.applicationDidFinishLaunchingNotification
WKExtension.applicationWillEnterForegroundNotification
WKExtension.applicationWillResignActiveNotification

Source: https://developer.apple.com/documentation/WatchKit/wkextension

like image 93
Jamie McDaniel Avatar answered Nov 30 '22 10:11

Jamie McDaniel


Appears that the WatchOS equivalent of

let notificationCenter = NSNotificationCenter.defaultCenter()
notificationCenter.addObserver(self, selector: "applicationDidEnterBackground", name: UIApplicationDidEnterBackgroundNotification, object: nil)

is simply

let notificationCenter = NSNotificationCenter.defaultCenter()
notificationCenter.addObserver(self, selector: "applicationDidEnterBackground", name: "UIApplicationDidEnterBackgroundNotification", object: nil)

One just need to replace the emum by its string equivalent

like image 37
HaneTV Avatar answered Nov 30 '22 10:11

HaneTV