Guest User

Untitled

a guest
Nov 22nd, 2017
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. @interface ExtensionDelegate : NSObject <WKExtensionDelegate>
  2.  
  3. @property (nonatomic, weak) MainInterfaceController *interfaceController;
  4.  
  5. @implementation ExtensionDelegate
  6.  
  7. -(void)applicationDidEnterBackground {
  8.  
  9. [self scheduleRefreshBackgroundTask];
  10. [self scheduleSnapshotRefreshBackgroundTask];
  11. }
  12.  
  13. -(void)scheduleRefreshBackgroundTask WK_AVAILABLE_WATCHOS_ONLY(3.0){
  14.  
  15. // Background Refresh
  16.  
  17. NSDictionary *backgroundRefreshUserInfo = @{@"reason": @"Bakcground Refresh"};
  18.  
  19. NSDate *backgroundRefreshDate = [NSDate dateWithTimeIntervalSinceNow:30 * 60];
  20.  
  21. [[WKExtension sharedExtension] scheduleBackgroundRefreshWithPreferredDate:backgroundRefreshDate
  22. userInfo:backgroundRefreshUserInfo
  23. scheduledCompletion:^(NSError *error){
  24.  
  25. if (error == nil) {
  26.  
  27. DMLogVerbose(DebugLogTypeWatch, @"successfully scheduled background refresh tesk");
  28.  
  29. } else {
  30.  
  31. DMLogError(DebugLogTypeWatch, @"unable to schedule background refresh task, error:%@", error);
  32.  
  33. }
  34.  
  35. }];
  36.  
  37. }
  38.  
  39. -(void)scheduleSnapshotRefreshBackgroundTask WK_AVAILABLE_WATCHOS_ONLY(3.0){
  40.  
  41. //fire in 1 hr
  42.  
  43. NSDate *inputDate = [NSDate date];
  44.  
  45. NSDate *fireDate = [inputDate initWithTimeIntervalSinceNow:1 * 60 * 60];
  46.  
  47. //optional.. any sourceCoding compliant data can be passed here
  48.  
  49. // SnapShot
  50.  
  51. NSDictionary *userInfo = @{@"reason": @"Snapshot Refresh"};
  52.  
  53. [[WKExtension sharedExtension] scheduleSnapshotRefreshWithPreferredDate:fireDate
  54. userInfo:userInfo
  55. scheduledCompletion:^(NSError *error){
  56.  
  57. if (error == nil) {
  58. DMLogVerbose(DebugLogTypeWatch, @"successfully scheduled background tesk, use the crown to send the app to the background and wait for handle:backgroundTasks to fire.");
  59. }
  60. }];
  61.  
  62. }
  63.  
  64. -(void)handleBackgroundTasks:(NSSet<WKRefreshBackgroundTask *> *)backgroundTasks WK_AVAILABLE_WATCHOS_ONLY(3.0) {
  65.  
  66. for (WKRefreshBackgroundTask *task in backgroundTasks) {
  67.  
  68. if ([[WKExtension sharedExtension] applicationState] == WKApplicationStateBackground) {
  69.  
  70. // Snapshot
  71. if ([task isKindOfClass:[WKSnapshotRefreshBackgroundTask class]]) {
  72.  
  73. if (self.interfaceController) {
  74.  
  75. [self.interfaceController updateData:YES];
  76.  
  77. [self.interfaceController initializeDefaultUI];
  78.  
  79. [self scheduleSnapshotRefreshBackgroundTask];
  80.  
  81. }
  82.  
  83. }
  84.  
  85. // Connectivity Refresh
  86. else if ([task isKindOfClass:[WKWatchConnectivityRefreshBackgroundTask class]]) {
  87.  
  88. // do something
  89.  
  90. }
  91.  
  92. // Network (URLSession)
  93. else if ([task isKindOfClass:[WKURLSessionRefreshBackgroundTask class]]) {
  94.  
  95. // Resume download
  96.  
  97. NSURLSessionConfiguration *sessionConfig = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:[DataManager watchDataURLSessionIdentifier]];
  98.  
  99. NSURLSession *session = [NSURLSession sessionWithConfiguration:sessionConfig
  100. delegate:
  101. [TWNDataManager sharedDataManager].currentWatchDataDownloader
  102. delegateQueue:nil];
  103.  
  104. DMLogVerbose(DebugLogTypeWatch, @"Resume watch download session in background:%@", session);
  105.  
  106. }
  107. // Background Refresh
  108. else if ([task isKindOfClass:[WKApplicationRefreshBackgroundTask class]]){
  109.  
  110. NSDictionary *userInfo = [task userInfo];
  111.  
  112. NSString *reason = [userInfo objectForKey:@"reason"];
  113.  
  114. if ([reason isEqualToString:@"Bakcground Refresh"]) {
  115.  
  116. if (self.interfaceController && !self.isBackgroundUpdating) {
  117.  
  118. self.isBackgroundUpdating = YES;
  119.  
  120. [self.interfaceController updateData:YES];
  121.  
  122. self.isBackgroundUpdating = NO;
  123.  
  124. [self scheduleRefreshBackgroundTask];
  125.  
  126. }
  127. }
  128. }
  129. }
  130.  
  131. [task setTaskCompleted];
  132. }
  133. }
Add Comment
Please, Sign In to add comment