Skip to content

Instantly share code, notes, and snippets.

@mbehan
Created October 18, 2016 12:20
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mbehan/0530682256fa4d4ac1b28c644550a46d to your computer and use it in GitHub Desktop.
Save mbehan/0530682256fa4d4ac1b28c644550a46d to your computer and use it in GitHub Desktop.
Sample code demonstrating how to determine which complication family was tapped to launch your watchOS app
// 1.
class ComplicationTimeKeeper{
static let shared = ComplicationTimeKeeper()
var utilitarianLarge : Date?
var utilitarianSmall : Date?
var circularSmall : Date?
var modularLarge : Date?
var modularSmall : Date?
}
// 2. in your CLKComplicationDataSource
func getCurrentTimelineEntry(for complication: CLKComplication, withHandler handler: @escaping ((CLKComplicationTimelineEntry?) -> Void)) {
// Call the handler with the current timeline entry
switch complication.family {
case .utilitarianLarge:
let date = Date()
ComplicationTimeKeeper.shared.utilitarianLarge = date
let template = CLKComplicationTemplateUtilitarianLargeFlat()
template.textProvider = CLKSimpleTextProvider(text:"Something")
let timelineEntry = CLKComplicationTimelineEntry(date: date, complicationTemplate: template)
handler(timelineEntry)
default: handler(nil)
}
}
// 3. in your WKExtensionDelegate
func handleUserActivity(_ userInfo: [AnyHashable : Any]?) {
guard let userInfo = userInfo, let timelineDate = userInfo[CLKLaunchedTimelineEntryDateKey] as? Date else{
return
}
if let utilLarge = ComplicationTimeKeeper.shared.utilitarianLarge, timelineDate.compare(utilLarge) == .orderedSame {
WKExtension.shared().rootInterfaceController?.pushController(withName: "SomeController", context: nil)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment