How to create apple watchOS5 complication?

4.8k views Asked by At

I've never worked in WatchOS5 and want to develop a horizontal complication (Modular large) for AppleWatch, like "Heart Rate". The idea is that I would display heart rate data in a different way. Right now I want to deploy the complication on development watch.

I have created a new project with a checkbox for "complication" added. I see that this added a complications controller with timeline configuration placeholders.

There is also an storyboard with a bunch of empty screens. I'm not sure as to how much effort I need to put into an apple watch app before I can deploy it. I see this Apple doc, but it does not describe how to layout my complication. Some section seem to have missing links.

  • Can I provide one style of complication only (large horizontal - modular large)
  • Do I need to provide any iPhone app content beyond managing the complication logic, or can I get away without having a view controller?
  • Do I control the appearance of my complication by adding something to the assets folder (it has a bunch of graphic slots)?

Sorry for a complete beginner project, I have not seen a project focusing specifically on the horizontal complication for watch OS 5

2

There are 2 answers

0
Alex Stone On BEST ANSWER

Here's an example by Apple of how to communicate with the apple watch app. You need to painstakingly read the readme about 25 times to get all the app group identifiers changed in that project.

  • Your main phone app assets are not visible to the watch app
  • Your watch storyboard assets go in WatchKit target
  • Your programmatically accessed assets go into the watch extension target

Original answers:

  • Can I provide one style of complication only (large horizontal - modular large) - YES
  • Do I need to provide any iPhone app content beyond managing the complication logic, or can I get away without having a view controller? YES - watch apps have computation limits imposed on them
  • Do I control the appearance of my complication by adding something to the assets folder (it has a bunch of graphic slots)? See below - it's both assets folder and placeholders

Modify the example above to create a placeholder image displayed on the watch (when you are selecting a complication while modifying the screen layout)

func getPlaceholderTemplate(for complication: CLKComplication, withHandler handler: @escaping (CLKComplicationTemplate?) -> Void) {
// Pass the template to ClockKit.
if complication.family == .graphicRectangular {

    // Display a random number string on the body.
    let template = CLKComplicationTemplateGraphicRectangularLargeImage()
    template.textProvider = CLKSimpleTextProvider(text: "---")
    let image = UIImage(named: "imageFromWatchExtensionAssets") ?? UIImage()
    template.imageProvider = CLKFullColorImageProvider(fullColorImage: image)

    // Pass the entry to ClockKit.
    handler(template)
}else {
    handler(nil);
    return
}

}

sending small packets to the watch (will not send images!)

func updateHeartRate(with sample: HKQuantitySample){

    let context: [String: Any] = ["title": "String from phone"]
    do {
        try WCSession.default.updateApplicationContext(context)
    } catch {
        print("Failed to transmit app context")
    }
}

Transferring images and files:

func uploadImage(_ image: UIImage, name: String, title: String = "") {

    let data: Data? = UIImagePNGRepresentation(image)

    do {
        let fileManager = FileManager.default
        let documentDirectory = try fileManager.url(for: .cachesDirectory,
                                                    in: .userDomainMask,
                                                    appropriateFor:nil,
                                                    create:true)
        let fileURL = try FileManager.fileURL("\(name).png")

        if fileManager.fileExists(atPath: fileURL.path) {
            try fileManager.removeItem(at: fileURL)
            try data?.write(to: fileURL, options: Data.WritingOptions.atomic)
        } else {
            try data?.write(to: fileURL, options: Data.WritingOptions.atomic)
        }

        if WCSession.default.activationState != .activated {
            print("session not activated")
        }
        fileTransfer = WCSession.default.transferFile(fileURL, metadata: ["name":name, "title": title])

    }
    catch {
        print(error)
    }
    print("Completed transfer \(name)")
}
0
ARMatt On

You should be able to deploy it immediately, though it won't do anything. Have a look at the wwdc video explaining how to create a complication: video

You can't layout the complication yourself, you can chose from a set of templates that you fill with data. The screens you are seeing are for your watch app, not the complication.

You don't have to support all complication styles.

The complication logic is part of your WatchKit Extension, so technically you don't need anything in the iOS companion app, I'm not sure how much functionality you have to provide to get past the app review though.

Adding your graphics to the asset catalog won't do anything, you have to reference them when configuring the templates.