Guest User

Untitled

a guest
Nov 22nd, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. - (IBAction)btnUpdateComplication:(id)sender {
  2.  
  3. //create a dictionary with value of UITextField
  4. NSDictionary *userInfo = @{@"data" : txtSend.text};
  5. if(WCSession.isSupported){
  6. WCSession *session = [WCSession defaultSession];
  7. session.delegate = self;
  8. [session activateSession];
  9.  
  10. if(session.reachable){
  11. if(session.paired){
  12. [session transferCurrentComplicationUserInfo:userInfo];
  13.  
  14. //update the WKInterfacelabel
  15. complicationLabel.text = [NSString stringWithFormat:@"UserInfo: %@",[userInfo objectForKey:@"data"]];
  16. }
  17. }
  18. else {
  19. complicationLabel.text = @"Session Not Reachable";
  20. }
  21. }
  22. }
  23.  
  24. - (void)awakeWithContext:(id)context {
  25. [super awakeWithContext:context];
  26.  
  27. // Activate the WCSession
  28. if ([WCSession isSupported]) {
  29. WCSession *session = [WCSession defaultSession];
  30. session.delegate = self;
  31. [session activateSession];
  32. NSLog(@"WCSession is supported");
  33. }
  34. }
  35.  
  36.  
  37. - (void)session:(WCSession *)session didReceiveUserInfo:(NSDictionary<NSString *,id> *)userInfo {
  38.  
  39. dispatch_async(dispatch_get_main_queue(), ^{
  40.  
  41. //store the dictionary to a variable
  42. self.lastReceivedUserInfo = userInfo;
  43.  
  44. //Q1) but how do I pass the userInfo dictionary to the ComplicationController
  45.  
  46. //update the WKInterfaceLabel
  47. [self.userInfoLabel setText:[NSString stringWithFormat:@"UserInfo: %@",[userInfo objectForKey:@"data"]]];
  48.  
  49. });
  50.  
  51.  
  52. //Q2) how do i update the complication when the session isn't active
  53.  
  54. //trigger the Complication to update
  55. CLKComplicationServer *server = [CLKComplicationServer sharedInstance];
  56. for (CLKComplication *complication in server.activeComplications) {
  57. [server reloadTimelineForComplication:complication];
  58. }
  59.  
  60. }
  61.  
  62. //setup the placeholder for the complication
  63. - (void)getLocalizableSampleTemplateForComplication:(CLKComplication *)complication withHandler:(void(^)(CLKComplicationTemplate * __nullable complicationTemplate))handler {
  64. if (complication.family == CLKComplicationFamilyModularLarge) {
  65. CLKComplicationTemplateModularLargeTallBody *complicationTemplate = [[CLKComplicationTemplateModularLargeTallBody alloc] init];
  66. [complicationTemplate setHeaderTextProvider: [CLKSimpleTextProvider textProviderWithText:@"HeaderText"]];
  67. [complicationTemplate setBodyTextProvider: [CLKSimpleTextProvider textProviderWithText:@"Second Line"]];
  68. handler(complicationTemplate);
  69. } else {
  70. handler(nil);
  71. }
  72. }
  73.  
  74. // this method is called when the updateComplicationServer method reloads timeline for the active complication.
  75. - (void)getCurrentTimelineEntryForComplication:(CLKComplication *)complication withHandler:(void(^)(CLKComplicationTimelineEntry * __nullable))handler {
  76.  
  77. //this will just update the complication with the current time when update received.
  78. NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
  79. [dateFormatter setDateFormat:@"HH:mm:ss"];
  80. strCurrentTime = [dateFormatter stringFromDate:[NSDate date]];
  81.  
  82. //Q3. Here I need to get the userInfo and update the compllication
  83.  
  84. NSDate *now = [NSDate date];
  85. if (complication.family == CLKComplicationFamilyModularLarge) {
  86. CLKComplicationTemplateModularLargeTallBody *complicationTemplate =
  87. [[CLKComplicationTemplateModularLargeTallBody alloc] init];
  88. [complicationTemplate setHeaderTextProvider: [CLKSimpleTextProvider textProviderWithText:@"Last Refresh Time:"]];
  89. complicationTemplate.bodyTextProvider = [CLKSimpleTextProvider textProviderWithText:strCurrentTime];
  90.  
  91. //this is the line I want to update
  92. //complicationTemplate.bodyTextProvider = [CLKSimpleTextProvider textProviderWithText:myText];
  93.  
  94. CLKComplicationTimelineEntry *entry = [CLKComplicationTimelineEntry entryWithDate:now complicationTemplate:complicationTemplate];
  95. handler(entry);
  96. } else {
  97. handler(nil);
  98. }
  99. }
Add Comment
Please, Sign In to add comment