I have a complication that works on Simulator, but doesn't work on an actual device when I TestFlight it to test on an actual device (and for clarity sake if there is any confusion, I'm not talking about debugging via device, but just testing if it works on a device).
Specifically, on the Watch device:
- I select the Complication on the Watch thru customizing the clock face, which gives me the placeholder text (so far so good because
getPlaceholderTemplateForComplication
works on Simulator too)... - but then the Complication always stays as the placeholder text (not
correct because
getCurrentTimelineEntryForComnplication
works on Simulator)... - even when scrolling thru Time Travel the placeholder text doesn't change but just dims (not correct because
getTimelineEntriesForComplication:afterDate
works on Simulator)...
Info
on iPhone:
game.duel = playoffs[“Duel”] as! String
game.tv = playoffs[“TV”] as! String
game.td = playoffs[“TD”] as! AnyObject
let dictionary = [“Duel” : game.duel, “TV” : game.tv, “TD” : game.td]
let transferComplication = WCSession.defaultSession().transferCurrentComplicationUserInfo(dictionary)
ExtensionDelegate
in WatchKit Extension:
var duelArray = [String]()
var tvArray = [String]()
var tdArray = [NSDate]()
let defaults = NSUserDefaults.standardUserDefaults()
if let duel = userInfo[“Duel”] as? String, let tv = userInfo[“TV”] as? String, let td = userInfo[“TD”] as? String {
duelArray.append(duel)
tvArray.append(tv)
tdArray.append(td as! NSDate)
defaults.setObject(duelArray, forKey: “DuelSaved”)
defaults.setObject(tvArray, forKey: "TVSaved”)
defaults.setObject(tdArray, forKey: "TDSaved”)
}
ComplicationController
in WatchKit Extension:
func getCurrentTimelineEntryForComplication(complication: CLKComplication, withHandler handler: ((CLKComplicationTimelineEntry?) -> Void)) {
switch complication.family {
case .ModularLarge:
let mlTemplate = CLKComplicationTemplateModularLargeStandardBody()
if let currentDuel = defaults.arrayForKey(“DuelSaved”) as? [String] {
let firstDuel = currentDuel[0]
let headerTextProvider = CLKSimpleTextProvider(text: firstDuel)
mlTemplate.headerTextProvider = headerTextProvider
} else {
// …
}
if let currentTV = defaults.arrayForKey(“TVSaved”) as? [String] {
let firstTV = currentTV[0]
let body1TextProvider = CLKSimpleTextProvider(text: firstTV)
mlTemplate.body1TextProvider = body1TextProvider
} else {
// …
}
if let currentTD = defaults.arrayForKey("TDSaved"){
let firstTD = currentTD[0]
let body2TextProvider = CLKTimeTextProvider(date: firstTD as! NSDate)
mlTemplate.body2TextProvider = body2TextProvider
} else {
// …
}
let timelineEntry = CLKComplicationTimelineEntry(date: NSDate(), complicationTemplate: mlTemplate)
handler(timelineEntry)
// …
}
func getTimelineEntriesForComplication(complication: CLKComplication, afterDate date: NSDate, limit: Int, withHandler handler: (([CLKComplicationTimelineEntry]?) -> Void)) {
let headerArray = defaults.arrayForKey(“DuelSaved”)
let body1Array = defaults.arrayForKey("TVSaved")
let body2Array = defaults.arrayForKey("TDSaved")
guard let headers = headerArray, texts = body1Array, dates = body2Array else { return }
var entries = [CLKComplicationTimelineEntry]()
for (index, header) in headers.enumerate() {
let text = texts[index]
let date1 = dates[index]
let headerTextProvider = CLKSimpleTextProvider(text: header as! String, shortText: headerShort as? String)
let body1TextProvider = CLKSimpleTextProvider(text: text as! String)
let timeTextProvider = CLKTimeTextProvider(date: date1 as! NSDate)
let template = CLKComplicationTemplateModularLargeStandardBody()
template.headerTextProvider = headerTextProvider
template.body1TextProvider = body1TextProvider
template.body2TextProvider = timeTextProvider
switch complication.family {
case .ModularLarge:
let timelineEntry = CLKComplicationTimelineEntry(date: date1 as! NSDate, complicationTemplate: template)
entries.append(timelineEntry)
// …
}
func requestedUpdateDidBegin() {
let server=CLKComplicationServer.sharedInstance()
for comp in (server.activeComplications) {
server.reloadTimelineForComplication(comp)
}
}
This is the flow of the data:
transferCurrentComplicationUserInfo
passes data to the Watch ExtensionDelegate
wherein the data is saved in NSUserDefaults
. ComplicationController
then pulls its initial data from NSUserDefaults
.