swift - How to display image in watchOS complication -


i'm setting complications watchos 2 app.

i want offer 3 different types of complications:

  • utilitarian small
  • modular smallist item
  • circular small

all these complication types should display app icon image. in clockkit class, have implemented 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 appropriate way of realising idea, i'd know code displaying image complication. know how achieve this?

at first recommend watch apple's session complications unless haven't seen yet.

you need implement 3 following non-optional methods of clkcomplicationdatasource in complicationcontroller @ 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 optional. far see implemented second one. implementations of remaining 2 following in 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 specify data source class in complication configuration $(product_module_name).complicationcontroller , check appropriate checkboxes. enter image description here

that's minimal complication configuration in case.


Comments