swiftapple-watchapple-watch-complicationwatchos-5

How to create apple watchOS5 complication?


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.

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


Solution

  • 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.

    Original answers:

    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)")
    }