8

I'm currently setting up complications for my watchOS 2 app.

I want to offer three different types of complications:

  • Utilitarian small
  • Modular smallist item
  • Circular small

All these complication types should simply display my app icon as an image. In my ClockKit class, I have implemented the following method:

func getCurrentTimelineEntryForComplication(complication: CLKComplication, withHandler handler: (CLKComplicationTimelineEntry?) -> Void) {

    if complication.family == .CircularSmall {

        let template = CLKComplicationTemplateCircularSmallRingImage()
        template.imageProvider = CLKImageProvider(onePieceImage: UIImage(named: "app_icon")!)
        let timelineEntry = CLKComplicationTimelineEntry(date: NSDate(), complicationTemplate: template)
        handler(timelineEntry)

    } else if complication.family == .UtilitarianSmall{

        let template = CLKComplicationTemplateUtilitarianSmallRingImage()
        template.imageProvider = CLKImageProvider(onePieceImage: UIImage(named: "app_icon")!)
        let timelineEntry = CLKComplicationTimelineEntry(date: NSDate(), complicationTemplate: template)
        handler(timelineEntry)

    } else if complication.family == .ModularSmall {

        let template = CLKComplicationTemplateModularSmallRingImage()
        template.imageProvider = CLKImageProvider(onePieceImage: UIImage(named: "app_icon")!)
        let timelineEntry = CLKComplicationTimelineEntry(date: NSDate(), complicationTemplate: template)
        handler(timelineEntry)

    } else {

        handler(nil)

    }

}

I'm not sure that's the appropriate way of realising my idea, so I'd like to know the code for just displaying an image as my complication. Does anybody know how I could achieve this?

fredpi
  • 6,546
  • 3
  • 34
  • 57

1 Answers1

14

At first I strongly recommend you to watch an Apple's session about Complications unless you haven't seen it yet.

You need to implement 3 following non-optional methods of CLKComplicationDataSource in your ComplicationController at least:

public func getSupportedTimeTravelDirectionsForComplication(complication: CLKComplication, withHandler handler: (CLKComplicationTimeTravelDirections) -> Void)
public func getCurrentTimelineEntryForComplication(complication: CLKComplication, withHandler handler: (CLKComplicationTimelineEntry?) -> Void)
public func getPlaceholderTemplateForComplication(complication: CLKComplication, withHandler handler: (CLKComplicationTemplate?) -> Void)

All other methods are optional. As far as you see you implemented only the second one. Implementations of remaining two could be the following in your context:

class ComplicationController: NSObject, CLKComplicationDataSource {

    func getSupportedTimeTravelDirectionsForComplication(complication: CLKComplication, withHandler handler: (CLKComplicationTimeTravelDirections) -> Void) { 
        // Turn off time travelling
        handler([CLKComplicationTimeTravelDirections.None])
    }

    func getPlaceholderTemplateForComplication(complication: CLKComplication, withHandler handler: (CLKComplicationTemplate?) -> Void) {
        var template: CLKComplicationTemplate?

        switch complication.family {
            case .CircularSmall:
                template = CLKComplicationTemplateCircularSmallRingImage()
                template.imageProvider = CLKImageProvider(onePieceImage: UIImage(named: "app_icon")!)
            case .UtilitarianSmall:
                template = CLKComplicationTemplateUtilitarianSmallRingImage()
                template.imageProvider = CLKImageProvider(onePieceImage: UIImage(named: "app_icon")!)
            case .ModularSmall:
                template = CLKComplicationTemplateModularSmallRingImage()
                template.imageProvider = CLKImageProvider(onePieceImage: UIImage(named: "app_icon")!)
            case .ModularLarge:
                template = nil
            case .UtilitarianLarge:
                template = nil
        }

        handler(template)
    }

}

And don't forget to specify your Data Source Class in Complication Configuration as $(PRODUCT_MODULE_NAME).ComplicationController and check appropriate checkboxes. enter image description here

That's the minimal complication configuration in your case.

Dmytro Hutsuliak
  • 1,677
  • 4
  • 19
  • 34
  • Thanks for your detailed answer - perhaps I didn't clarify that I have already implemented these methods, otherwise I couldn't build the app. I'll have a look onto the WWDC session video you suggested. – fredpi Sep 25 '15 at 19:01
  • @pi1000 - Updated the answer with a configuration screenshot. I think you need to enumerate all things you've done including settings that I've attached. – Dmytro Hutsuliak Sep 25 '15 at 19:10
  • Yeah thanks, but still some misunderstanding. I see the complication as a rounded circle, but I do not want it to have such a template and want to know whether I can use my app icon as the image. – fredpi Sep 25 '15 at 19:12
  • 3
    I finally got what you want :) No, you cannot use your application logo in complications as is - you need to adapt one using only one colour with transparency (alpha-channel). Just listen attentively the session video starting from 12:10 otherwise you will get just a colour-flooded icon (yep - that rounded circle is your application logo). – Dmytro Hutsuliak Sep 25 '15 at 19:27
  • Ok, thanks for your help and patience - I will definitely give it a try. – fredpi Sep 25 '15 at 19:29
  • 1
    DO NOT NEED $(PRODUCT_MODULE_NAME).ComplicationController, just use ComplicationController in configuration. The Complications Group is the name of the folder in which the various sizes images are, so if it's "Complication" use this, if there is no folder use the "none" as above. The name of the image must be referenced as "FolderName/ImageName" like "Complication/Modular" etc., BUT be careful that during import XCode uses the original "Modular" etc. names not the file names!! You can rename it, but as I see the messy Complication stuffs, hectic api changes, my advice is leaving it as it is! – BootMaker Jun 27 '18 at 05:46