Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- - (IBAction)btnUpdateComplication:(id)sender {
- //create a dictionary with value of UITextField
- NSDictionary *userInfo = @{@"data" : txtSend.text};
- if(WCSession.isSupported){
- WCSession *session = [WCSession defaultSession];
- session.delegate = self;
- [session activateSession];
- if(session.reachable){
- if(session.paired){
- [session transferCurrentComplicationUserInfo:userInfo];
- //update the WKInterfacelabel
- complicationLabel.text = [NSString stringWithFormat:@"UserInfo: %@",[userInfo objectForKey:@"data"]];
- }
- }
- else {
- complicationLabel.text = @"Session Not Reachable";
- }
- }
- }
- - (void)awakeWithContext:(id)context {
- [super awakeWithContext:context];
- // Activate the WCSession
- if ([WCSession isSupported]) {
- WCSession *session = [WCSession defaultSession];
- session.delegate = self;
- [session activateSession];
- NSLog(@"WCSession is supported");
- }
- }
- - (void)session:(WCSession *)session didReceiveUserInfo:(NSDictionary<NSString *,id> *)userInfo {
- dispatch_async(dispatch_get_main_queue(), ^{
- //store the dictionary to a variable
- self.lastReceivedUserInfo = userInfo;
- //Q1) but how do I pass the userInfo dictionary to the ComplicationController
- //update the WKInterfaceLabel
- [self.userInfoLabel setText:[NSString stringWithFormat:@"UserInfo: %@",[userInfo objectForKey:@"data"]]];
- });
- //Q2) how do i update the complication when the session isn't active
- //trigger the Complication to update
- CLKComplicationServer *server = [CLKComplicationServer sharedInstance];
- for (CLKComplication *complication in server.activeComplications) {
- [server reloadTimelineForComplication:complication];
- }
- }
- //setup the placeholder for the complication
- - (void)getLocalizableSampleTemplateForComplication:(CLKComplication *)complication withHandler:(void(^)(CLKComplicationTemplate * __nullable complicationTemplate))handler {
- if (complication.family == CLKComplicationFamilyModularLarge) {
- CLKComplicationTemplateModularLargeTallBody *complicationTemplate = [[CLKComplicationTemplateModularLargeTallBody alloc] init];
- [complicationTemplate setHeaderTextProvider: [CLKSimpleTextProvider textProviderWithText:@"HeaderText"]];
- [complicationTemplate setBodyTextProvider: [CLKSimpleTextProvider textProviderWithText:@"Second Line"]];
- handler(complicationTemplate);
- } else {
- handler(nil);
- }
- }
- // this method is called when the updateComplicationServer method reloads timeline for the active complication.
- - (void)getCurrentTimelineEntryForComplication:(CLKComplication *)complication withHandler:(void(^)(CLKComplicationTimelineEntry * __nullable))handler {
- //this will just update the complication with the current time when update received.
- NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
- [dateFormatter setDateFormat:@"HH:mm:ss"];
- strCurrentTime = [dateFormatter stringFromDate:[NSDate date]];
- //Q3. Here I need to get the userInfo and update the compllication
- NSDate *now = [NSDate date];
- if (complication.family == CLKComplicationFamilyModularLarge) {
- CLKComplicationTemplateModularLargeTallBody *complicationTemplate =
- [[CLKComplicationTemplateModularLargeTallBody alloc] init];
- [complicationTemplate setHeaderTextProvider: [CLKSimpleTextProvider textProviderWithText:@"Last Refresh Time:"]];
- complicationTemplate.bodyTextProvider = [CLKSimpleTextProvider textProviderWithText:strCurrentTime];
- //this is the line I want to update
- //complicationTemplate.bodyTextProvider = [CLKSimpleTextProvider textProviderWithText:myText];
- CLKComplicationTimelineEntry *entry = [CLKComplicationTimelineEntry entryWithDate:now complicationTemplate:complicationTemplate];
- handler(entry);
- } else {
- handler(nil);
- }
- }
Add Comment
Please, Sign In to add comment