WatchOS:获取applicationDidBecomeActive通知

时间:2016-01-06 11:04:55

标签: swift watchkit apple-watch

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

我正在为Apple手表寻找相当于这个iOS代码,因为在UIKit中不再出现UIApplication:

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

任何帮助都会很好

3 个答案:

答案 0 :(得分:2)

看来WatchOS相当于

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

就是

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

只需要用等效的字符串替换emum

答案 1 :(得分: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.
  }
}

答案 2 :(得分:0)

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

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

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