WatchOS:获取 applicationDidBecomeActive 通知

Han*_*eTV 5 swift apple-watch watchkit

我正在制作一个框架,当我的苹果手表进入背景和前景时需要做一些事情。

我正在为 Apple Watch 寻找与此 iOS 代码相当的代码,因为 UIApplication 不再存在于 UIKit 中:

let notificationCenter = NSNotificationCenter.defaultCenter()
notificationCenter.addObserver(self, selector: "applicationDidEnterBackground", name: UIApplicationDidEnterBackgroundNotification, object: nil)
Run Code Online (Sandbox Code Playgroud)

你能帮忙的话,我会很高兴

Jam*_*iel 5

从 watchOS 7 开始,添加了以下内容:

WKExtension.applicationDidBecomeActiveNotification
WKExtension.applicationDidEnterBackgroundNotification
WKExtension.applicationDidFinishLaunchingNotification
WKExtension.applicationWillEnterForegroundNotification
WKExtension.applicationWillResignActiveNotification
Run Code Online (Sandbox Code Playgroud)

来源:https : //developer.apple.com/documentation/WatchKit/wkextension


tsk*_*bru 0

最接近的applicationDidBecomeActiveapplicationWillResignActive

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.
  }
}
Run Code Online (Sandbox Code Playgroud)