HaneTV
HaneTV

Reputation: 926

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

Upvotes: 7

Views: 3098

Answers (3)

Jamie McDaniel
Jamie McDaniel

Reputation: 1808

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

Upvotes: 10

HaneTV
HaneTV

Reputation: 926

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

Upvotes: 3

tskulbru
tskulbru

Reputation: 2346

Closest you get is applicationDidBecomeActive and applicationWillResignActive

class ExtensionDelegate: NSObject, WKExtensionDelegate {

  func applicationDidFinishLaunching() {
      // Perform any final initialization of your application.
  }

  func applicationDidBecomeActive() {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.    
  }

  func applicationWillResignActive() {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, etc.
  }
}

Upvotes: 0

Related Questions