Apple Watch开发

一:概述

Apple Watch是苹果在2015年3月9日发布的一款智能手表,Apple Watch的好处是用户可以很方便的查看重要信息,而不用将iphone从裤袋里掏出来。

苹果发布的iOS SDK 8.2加入了WatchKit框架以支持搭载着Watch OS系统的Apple Watch。

开发者需要在现有的iOS应用基础上开发WatchKit app应用,Apple Watch目前还不支持独立的WatchKit app应用(据苹果官方文档说,2015年晚些时候可能会支持原生应用)


二:创建WatchKit app

要创建一个在Apple Watch上运行的第三方WatchKit app,需要创建2个独立的bundle:

1.WatchKit app

2.WatchKit extension

这2个bundle打包在iOS app内部。

当你在iPhone上安装一个适配了WatchKit app的iOS app的时候,如果iOS系统检测到了一个配对的Apple Watch,就会提示用户安装WatchKit app。


三:配置Xcode

你必须通过一个现有的iOS app来创建WatchKit app,在你的iOS app project添加WatchKit app方法如下(你的Xcode版本必须为6.2或以上)

1.打开你的iOS app project

2.选择File>New>Target,会出现下面的界面


左侧选择Apple Watch,右侧选择WatchKit app,点击Next,出现下面的界面


根据需要勾选Include Notificaiton Scence和Include Glance Sence,点击Fish之后,出现下面的Alert


点击Activate自动为WatchKit app创建scheme,完成之后代码结构如下图所示


你的工程主要由3部分组成:iOS app,Watchkit extension,WatchKit app

1.iOS app和Watchkit extension在iPhone中运行,WatchKit app在Apple Watch中运行

2.WatchKit app包含storyboard界面文件和资源文件,Watchkit extension包含界面文件管理,用户交互响应等逻辑代码


WatchKit app的target结构如下图



WatchKit app由三种界面组成

1.AppleWatch

2.Notification

3.Glance

storyboard界面预览如下图


选择不同的scheme来编译调试不同的界面,如下图所示


如果调试的时候,没有显示Apple Watch模拟器,勾选下图所示的选项



四:WatchKit app体系结构

因为WatchKit app的界面(在Apple Watch上)和逻辑代码(在iPhone上)是分开的,所以Apple Watch和iPhone需要通过WatchKit通讯,流程图如下



WatchKit app生命周期

你的WatchKit app主要有下面3种方法启动

1.用户在Apple Watch的Home界面点击你的WatchKit app进入Apple Watch界面

2.轻扫进入你的WatchKit app的Glance界面

3.Apple Watch收到Notification进入WatchKit app的Notification界面

启动WatchKit app的流程图如下


在WatchKit app中,每一个视图控制器都是WKInterfaceController类的实例,类似于iOS app中的UIViewController

WKInterfaceController的生命周期方法

1.init(类似于iOS app中的init)

2.awakeWithContext(类似于iOS app中的ViewDidLoad)

3.willActivate(类似于iOS app中的viewWillAppear)

4.didDeactivate(类似于iOS app中的viewWillDisappear)

WKInterfaceController的生命周期流程图如下



五:UI要件

WatchKit app支持的UI控件有,Label,group,image,separator,button,switch,slider,menu,table,date,map,timer

支持的布局方式是Group布局,类似于Android的LinearLayout布局,如下图所示



在运行的时候,WKInterfaceController能够对UI控件所做的操作有

1.设置或者更新data values

2.修改可视化外观

3.修改大小

4.修改透明度

5.显示或隐藏

运行的时候,你不能添加或删除UI控件或者更改UI控件的排列顺序,但是你可以隐藏它


六:导航方式

WatchKit app支持2种导航方式

1.Page based

视图之间是并列的关系,按页分布,每一页是一个视图控制器,用户通过滑动屏幕切换到不同的页面

2.Hierarchical

视图之间是层级的关系,类似于iOS app的navigationController

用pushControllerWithName:context:方法push视图控制器


WatchKit app支持模态视图视图控制器,类似于iOS app的modelViewController

用presentControllerWithName:context:弹出模态视图控制器


七:Glance

1.Glance不支持交互式的UI控件

2.点击Glance,自动跳转到Apple Watch界面

3.Glance界面生成之后,会自动生成2个Group控件,你只能在这2个Group里面添加你的UI控件


八:Notification

当用户查看一个Notification的时候,系统会显示Short-Look Interface,Short-Look Interface的界面由系统提供,不能自定义,如下图所示


如果用户持续查看这个通知,系统会自动把Short-Look Interface转化成Long-Look Interface

1.System-provided sash
在storyboard文件中配置,可以修改背景色和文字颜色
2.App content
在storyboard文件中配置(Static interface的静态Alert Label内容从Notification的payload中获取)
3.App-defined actions
在iOS app的didFinishLaunchingWithOptions中,通过category配置,代码片段如下

因为iOS模拟器没有权限访问iOS app中注册的Actions,所以模拟器的actions要在系统自动生成的PushNotificationPayload.apns中配置,如下图所示

1.body属性
Static interface的静态Alert Label显示的类容
2.title属性
Short-Look Interface中的Title string显示的内容
3.category属性
NotificationPayload中的category
didFinishLaunchingWithOptions中注册的category
storyboard中的category
是一一对应的
4.WatchKit Simulator Actions
模拟器中注册的Actions
5.customKey
自定义key

PushNotificationPayload.apns是NotificationPayload测试文件,可以生成多个,在Notificaiton Scheme中选择,如下图所示



Long-Look Interface又分为Static interface和dynamic interface,如下图所示



Static interface和dynamic interface的区别

1.系统会自动为Static interface生成一个静态Alert Label,它的Text内容系统自动从Notification的payload中的body属性获取

dynamic interface无静态Alert Label

2.Static interface不可以自定义类,storyboard的类属性编辑器不可编辑,如下图所示


所以Static interface上的UI控件的显示内容不能动态改变

dynamic interface可以自定义类,UI控件的显示内容可动态改变


显示Static interface的情况

1.没有dynamic interface

2.Apple Watch不够电量显示dynamic interface

3.dynamic interface中的didReceiveRemoteNotification方法未实现,或者实现了但是completionHandler的参数为

 WKUserNotificationInterfaceTypeDefault

其他情况下显示dynamic interface

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值