以下内容主要用来自己学习总结,可以讨论,不喜勿喷
参考官网文档地址:Apple Watch Programming Guide
注意:WatchKit 需要 iOS8.2SDK及以上的SDK
为你的iOS项目添加WatchKit App,你必须已经有了一个iOS app才能创建WatchKit app。
因为app的结构如下图:
即iOS app包含了iOS代码和源文件以及WatchKit Extension,而WatchKit Extension包含了WatchKit代码和源文件以及WatchKit App。
为你的iOS项目添加WatchKit App:
- 在Xcode中打开一个iOS项目
- 选择File>New>Target,然后选择Apple Watch
- 选择WatchKit App,然后点击Next
- 选择Include Notification Scene或Include Glance Scene
- 点击Finish
WatchKit app ,WatchKit extension 和 iOS app 的ID必须统一。
建立,运行和调试程序
包含glance或custom notification的交互页面,必须配置额外的主题来测试这些交互页面。使用glance主题在模拟器里调试你的glance交互页面,以及使用notification主题来测试你的动态或静态的notification交互页面。
为glances和notifications配置自定义主题:
- 选择已存在的WatchKit app主题,就是这个WatchKit app
- 从主题菜单,选择Edit Scheme
- 复制WatchKit app并重命名(不知道干嘛用的)
- 选择Run
- Watch Interface选择合适的选项
- close
如果选择notification interfaces,会用一个JSON文件来描述(以上步骤不知道干嘛用的)
WatchKit App 结构
WatchKit app和WatchKit extension通过联合作业来实现app的UI交互。当用户选择你的app是,你的WatchKit app从storyboard中选择合适的场景。选择好场景后,WatchKit通知配套的iPhone来加载你的WatchKit extension并且创建需要完成这个场景的对象。最终将选择好的场景显示在你的Apple Watch上。示意图如下:
InterfaceController:每个场景都是通过一个InterfaceController对象完成的,这个对象是WKInterfaceController类的实例。InterfaceController类似于iOS中的ViewController,但是ViewController完成展现在屏幕上的内容和响应用户在屏幕上的操作,而InterfaceController并没有完成交互页面,这些页面都是通过WatchKit完成的。