Xcode 11 Beta 不会因为 WatchKit 而构建?

Xcode 11 Beta won't build because of WatchKit?

已完成 Xcode 10。现在处于测试阶段,我无法构建我不断收到此错误:

a "WatchKit" is not available when building for iOS Simulator. Consider using #if !os(iOS) to conditionally import this framework.

我在 WatchKit 扩展中有一个 swift 文件有同样的问题。事实证明,它既是 iOS 应用程序的成员,也是 WatchKit Extension 的成员。我在文件的目标成员资格部分取消选中 iOS 应用程序,以便它只属于 WatchKit 扩展目标。现在项目构建成功。

我们需要使用 "Conditional Imports" 来解决问题。

import WatchKit header 替换为以下代码:

#if !os(iOS)
import WatchKit
#endif

这解决了我的问题并在 iOS 13.

中成功构建

Apple Watch 与 iPhone/iPad 之间的一些通信功能过去是在 WatchKit 框架内实现的。但在某些时候它被转移到 WatchKitConnectivity 框架中。

如果您查看 Target 的 "Build Phase" -> "Link Binary With Libraries" 部分,您将看到 "WatchKit.framework" 状态为 "Optional"。 iOS13+ 变得更 "strict" 所以它不会构建,除非我完全删除 "WatchKit.framework",而是添加 "WatchConnectivity.framework"。

还要确保您的 iPhone/iPad 代码使用 "import WatchConnectivity" 而不是 "import WatchKit"。

Xcode 11 从 iOS SDK 中删除了 WatchKit。来自 release notes:

The WatchKit framework is no longer included in the iOS SDK. If you’re using WatchKit APIs from iOS, you need to remove this use. The WatchKit framework remains available on watchOS. If you’re using WatchKit APIs from iOS to infer availability of features on the paired Apple Watch, include information about your use case when you submit feedback to Feedback Assistant. (49707950)

这包括在 plugin.xml 中引用 WatchKit 的 Cordova 插件:

<framework src="WatchKit.framework" />

以上行将添加 WatchKit 作为 iOS 应用程序目标的框架。您需要删除它并仅将 WatchKit 添加到您应用的 Watch 目标。