Created
October 18, 2016 12:20
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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