watchOS 简介Introduction to watchOS

备注

有关最新功能的概述,请查看watchOS 3 简介Check out the Introduction to watchOS 3 for an overview of the latest features.

关于 watchOSAbout watchOS

WatchOS 应用解决方案包含3个项目:A watchOS app solution has 3 projects:

  • 监视扩展-包含 Watch 应用代码的项目。Watch Extension – A project that contains the code for the watch app.
  • 监视应用-包含用户界面情节提要和资源。Watch App – Contains the user-interface storyboard and resources.
  • IOS 父应用–此应用是普通的 iPhone 应用。iOS Parent App – This app is a normal iPhone app. 监视应用和扩展捆绑到 iPhone 应用中,以便交付到用户的手表。The watch app and extension are bundled into the iPhone app for delivery to the user's watch.

在 watchOS 1 应用中,扩展中的代码在 iPhone 上运行– Apple Watch 实际上是外部显示。In watchOS 1 apps, the code in the extension runs on the iPhone – the Apple Watch is effectively an external display. watchOS 2 和3应用完全在 Apple Watch 上运行。watchOS 2 and 3 apps run entirely on the Apple Watch. 下图显示了这种差异:This difference is shown in the diagram below:

无论 watchOS 的版本是哪个版本,Visual Studio for Mac 的 Solution Pad 完整的解决方案将如下所示:Regardless of which version of watchOS is targeted, in Visual Studio for Mac’s Solution Pad a complete solution will look something like this:

WatchOS 解决方案中的父应用是一种常规的 iOS 应用。The Parent App in a watchOS solution is a regular iOS app. 这是解决方案中可在手机上看到的唯一项目。This is the only project in the solution that is visible on the phone. 此应用程序的用例包括教程、管理屏幕和中间层筛选 cacheing 等。但是,用户可以在不打开父应用的情况下安装和运行 watch 应用/扩展,因此,如果需要父应用程序运行一次或管理,则需要对你的手表应用程序/扩展进行编程,以通知用户。Use-cases for this app would include tutorials, administrative screens, and middle-tier filtering, cacheing, etc. However, it is possible for the user to install and run the watch app/extension without ever having opened the parent app, so if you need the Parent app to run for one-time initialization or administration, you need to program your watch app/extension to tell the user that.

虽然父应用程序提供了 watch 应用和扩展,但它们在不同的沙箱中运行。Although the parent app delivers the watch app and extension, they run in different sandboxes.

在 watchOS 1 上,他们可以通过共享应用组或静态函数 WKInterfaceController.OpenParentApplication共享数据,这将触发父应用 AppDelegate 中的 UIApplicationDelegate.HandleWatchKitExtensionRequest 方法(请参阅使用父应用)。On watchOS 1 they can share data via a shared app group or via the static function WKInterfaceController.OpenParentApplication, which will trigger the UIApplicationDelegate.HandleWatchKitExtensionRequest method in your parent app’s AppDelegate (see working with the Parent app).

在 watchOS 2 或更高版本中,使用 WCSession 类将 Watch 连接框架与父应用进行通信。On watchOS 2 or later the Watch Connectivity framework is used to communicate with the parent app, using the WCSession class.

应用程序生命周期Application Lifecycle

在监视扩展中,为每个情节提要场景创建 WKInterfaceController 类的子类。In the watch extension, a subclass of the WKInterfaceController class is created for each Storyboard scene.

这些 WKInterfaceController 类类似于 iOS 编程中的 UIViewController 对象,但对视图没有相同的访问级别。These WKInterfaceController classes are analogous to the UIViewController objects in iOS programming but do not have the same level of access to the view. 例如,不能将控件动态添加到 UI 或重构 UI。For instance, you cannot dynamically add controls to or restructure your UI. 但是,您可以隐藏和显示控件,并在某些控件中更改其大小、透明度和外观选项。You can, however, hide and reveal controls and, with some controls, change their size, transparency, and appearance options.

WKInterfaceController 对象的生命周期涉及以下调用:The lifecycle of a WKInterfaceController object involves the following calls:

  • 唤醒:应在此方法中执行大多数初始化。Awake : You should perform most of your initialization in this method.
  • WillActivate :在向用户显示 Watch 应用之前立即调用。WillActivate : Called shortly before the Watch App appears to the user. 使用此方法可以执行最后一次初始化、启动动画等。Use this method to perform last-moment initialization, start animations, etc.
  • 此时,将显示 "监视" 应用,并且扩展会开始响应用户输入,并根据应用程序逻辑更新监视应用的显示。At this point, the Watch App appears and the Extension begins responding to user input and updating the Watch App’s display per your application logic.
  • DidDeactivate用户消除了 Watch 应用后,将调用此方法。DidDeactivate After the Watch App has been dismissed by the user, this method is called. 此方法返回后,将无法修改用户界面控件,直到下一次调用 WillActivateAfter this method returns, user interface controls cannot be modified until the next time WillActivate is called. 如果与 iPhone 的连接断开,还会调用此方法。This method will also be called if the connection to the iPhone is broken.
  • 停用扩展后,程序将无法访问该扩展。After the extension has been deactivated, it is inaccessible to your program. 将不会调用挂起的异步函数。Pending asynchronous functions will not be called. 手表工具包扩展不能使用后台处理模式。Watch Kit Extensions may not use background processing modes. 如果用户重新激活了该程序,但该应用程序尚未被操作系统终止,则将 WillActivate第一个调用的方法。If the program is reactivated by the user but the app has not been terminated by the operating system, the first method called will be WillActivate.

用户界面的类型Types of User Interface

有三种类型的用户可与手表应用交互。There are three types of interaction the user can have with your watch app. 所有使用 WKInterfaceController的自定义子类别进行编程,因此,之前讨论的生命周期序列会广泛应用(通知是使用 WKUserNotificationController的子类进行编程的,后者本身是 WKInterfaceController的子类):All are programmed using custom sub-classes of WKInterfaceController, so the previously-discussed lifecycle sequence applies universally (notifications are programmed with sub-classes of WKUserNotificationController, which itself is a sub-class of WKInterfaceController):

正常交互Normal Interaction

大多数监视应用/扩展交互将与你编写的 WKInterfaceController 的子类结合在手表应用的界面中。The majority of watch app/extension interaction will be with sub-classes of WKInterfaceController that you write to correspond to scenes in your watch app’s Interface.storyboard. 安装入门文章中对此进行了详细介绍。This is covered in detail in the Installation and Getting Started articles. 下图显示了 "观看工具包目录" 示例的情节提要的一部分。The following image shows a portion of the Watch Kit Catalog sample’s Storyboard. 对于此处显示的每个场景,扩展项目中都有相应的自定义 WKInterfaceControllerLabelDetailControllerButtonDetailControllerSwitchDetailController等)。For each scene showed here, there is a corresponding custom WKInterfaceController (LabelDetailController, ButtonDetailController, SwitchDetailController, etc.) in the extension project.

通知Notifications

通知是 Apple Watch 的主要用例。Notifications are a major use-case for the Apple Watch. 支持本地和远程通知。Both local and remote notifications are supported. 与通知的交互发生在两个阶段中,称为短期和长期。Interaction with notifications occurs in two stages, called Short- and Long-Look.

简短外观显示,并显示 "监视应用" 图标、名称和标题(如 WKInterfaceController.SetTitle所指定)。Short Looks are displayed briefly and show the watch app icon, its name, and the Title (as specified with WKInterfaceController.SetTitle).

长时间的外观将系统提供的窗扇区域和 "解除" 按钮与自定义的基于情节提要的内容合并在一起。The Long Look combines a system-provided sash area and Dismiss button with your custom Storyboard-based content.

WKUserNotificationInterfaceControllerWKInterfaceControllerDidReceiveLocalNotificationDidReceiveRemoteNotification的方法进行扩展。WKUserNotificationInterfaceController extends WKInterfaceController with the methods DidReceiveLocalNotification and DidReceiveRemoteNotification. 重写这些方法以响应通知事件。Override these methods to react to notification events.

有关通知 UI 设计的详细信息,请参阅Apple Watch 人体学接口准则For more information on Notification UI design, refer to the Apple Watch Human Interface Guidelines

屏幕大小Screen Sizes

Apple Watch 有两种表面大小:38mm 和42mm,两者都具有5:4 显示比例和 Retina 显示。The Apple Watch has two face sizes: 38mm and 42mm, both with a 5:4 display ratio, and a Retina display. 可用大小为:Their useable sizes are:

  • 38mm: 136 x 170 逻辑像素(272 x 340 物理像素)38mm: 136 x 170 logical pixels (272 x 340 physical pixels)
  • 42mm: 156 x 195 逻辑像素(312 x 390 物理像素)。42mm: 156 x 195 logical pixels (312 x 390 physical pixels).

使用 WKInterfaceDevice.ScreenBounds 确定监视应用正在运行的显示。Use WKInterfaceDevice.ScreenBounds to determine on which display your Watch App is running.

通常,通过更受限制的38mm 显示来开发文本和布局设计会更容易,然后增加。Generally, it’s easier to develop your text and layout design with the more constrained 38mm display and then scale up. 如果从较大的环境开始,缩小可能会导致不太好的重叠或文本截断。If you start with the larger environment, scaling down may lead to ugly overlap or text truncation.

阅读更多有关使用屏幕大小的信息。Read more about working with screen sizes.

WatchOS 的限制Limitations of watchOS

开发 watchOS 应用时,需要注意一些限制:There are some limitations of watchOS to be aware of when developing watchOS apps:

  • Apple Watch 设备具有有限的存储空间,请在下载大文件之前注意可用空间(例如Apple Watch devices have limited storage - be aware of the available space before downloading large files (eg. 音频或电影文件)。audio or movie files).

  • 许多 watchOS控件在 UIKit 中具有物,但它们是不同的类(WKInterfaceButton 而不是 UIButtonWKInterfaceSwitch UISwitch等),并且具有一组有限的方法与 UIKit 等效项相比。Many watchOS controls have analogues in UIKit, but are different classes (WKInterfaceButton rather than UIButton, WKInterfaceSwitch for UISwitch, etc.) and have a limited set of methods compared to their UIKit equivalents. 此外,watchOS 还提供了一些控件,如 WKInterfaceDate (用于显示日期和时间),UIKit 不具有该功能。Additionally, watchOS has some controls such as WKInterfaceDate (for displaying a date and time) that UIKit does not have.

    • 不能将通知仅路由到手表,也不能将通知路由到仅限 iPhone (用户已通过路由的控件)。You cannot route notifications to the Watch only, or the iPhone only (what kind of control the user has over routing has not been announced by Apple).

一些其他已知限制/常见问题:Some other known limitations / frequently asked questions:

  • Apple 将不允许第三方自定义监视面。Apple will not allow 3rd-party custom watch faces.

  • 允许监视控制连接的电话上的 iTunes 的 Api 是专用的。The APIs that allow the Watch to control iTunes on the connected phone are private.

其他阅读材料Further Reading

查看 Apple 的文档:Check out the documentation from Apple: