无论是Apple app或者Apple Watch app 掌握整个应用的生命周期尤为重要.
下图是watch app整个生命周期图



watchOS生命周期中在代码中是怎么实现的呢?
当用户在Home 界面点击app, watchOS 就把它启动,WatchOS先找到app的Info.plist 文件中key为WKExtensionDelegateClassName所对应的文件。如图三中所对应的文件为ExtensionDelegate.swfit。此时app进入前台模式。如图一app进入Foreground模式.
图一中A.B.C在ExtensionDelegate.swfit所对应的方法。
- A. The applicationDidFinishLaunching method is called.-->app启动时候调用.
- B. The applicationDidBecomeActive or applicationWillResignActive method is called.-->app从闲置状态到活动状态.
- C. The applicationWillEnterForeground or applicationDidEnterBackground method is called.-->app进入后台模式.
WKInterfaceController 生命周期

如图四所示:
app启动后WatchOS会自动加载WatchKit 中Interface.storyboard中的Initial Controller 调完之后会初始化UI然后会调用WKInterfaceController中生命周期
- (void)awake(withContext context: Any?);
- (void)willActivate();
- (void)didDeactivate();
- awake(withContext context: Any?):类似UIViewcontroller中的viewDidLoad:方法,用来配置interface controller。初始化相关的放在这里。
- willActivate:界面将要显示给用户时会被调用,类似UIViewcontroller中viewWillAppear:方法。这个方法主要用来对视图进行一些小的调整,初始化还是要放在init和awake(withContext context: Any?):里
- didDeactivate:用来清空界面,程序进入不活动状态。可以用它来终止Timer或者stop动画。在这个方法不能再对界面进行操作。
想随时了解我的动态,欢迎大家关注我的个人公众号蚁农之家

网友评论