Search code examples
xcodeswiftswift-playgroundapple-watch-complicationclockkit

Why can't I import ClockKit and use CLKComplicationDataSource in an Xcode Playground?


I can import WatchKit and WatchConnectivity and use various delegates like WCSessionDelegate, without any problem.

But when I try to import ClockKit or add CLKComplicationDataSource, Xcode throws errors like "no module exists."

To check this, I created a watch app project and examined ComplicationController.swift. It does have import ClockKit in the source.

Why can't we test this API in an Xcode Playground? Doesn't that defeat the purpose of having a Playground?


Solution

  • No you can't, for the following reasons.

    • The playground is running on an iOS simulator, not a watchOS simulator. What you want to use or test isn't available in iOS.

    • The ClockKit framework is more of a collection of objects related to a watch face complication. It wouldn't make sense for it to be available in the playground since the playground itself wouldn't have any complications.

    • You can't really test a complication in the playground, since no interactive complication server is running in the playground that would call your data source methods.

      Complications run in the background, and are managed by the complication server. When your complication is active (enabled) on the watch face, the system wakes your extension in the background, instantiates an instance of your data source, then obtains the necessary data it needs for that complication:

      You do not instantiate your data source class explicitly. After defining your class, specify the class name in the General tab of the project settings for your WatchKit extension. When the system needs data, it instantiates your class and initializes it by calling its init method. Once initialized, it calls the corresponding protocol methods to gather any needed data.

    You could submit a feature request asking Apple to let you interactively test your complication controller in the playground.