ExtensionDelegate.swift 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // Software License Agreement (BSD License)
  2. //
  3. // Copyright (c) 2010-2025, Deusty, LLC
  4. // All rights reserved.
  5. //
  6. // Redistribution and use of this software in source and binary forms,
  7. // with or without modification, are permitted provided that the following conditions are met:
  8. //
  9. // * Redistributions of source code must retain the above copyright notice,
  10. // this list of conditions and the following disclaimer.
  11. //
  12. // * Neither the name of Deusty nor the names of its contributors may be used
  13. // to endorse or promote products derived from this software without specific
  14. // prior written permission of Deusty, LLC.
  15. import WatchKit
  16. import CocoaLumberjackSwift
  17. final class ExtensionDelegate: NSObject, WKExtensionDelegate {
  18. func applicationDidFinishLaunching() {
  19. // Perform any final initialization of your application.
  20. }
  21. func applicationDidBecomeActive() {
  22. // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
  23. }
  24. func applicationWillResignActive() {
  25. // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
  26. // Use this method to pause ongoing tasks, disable timers, etc.
  27. }
  28. func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) {
  29. // Sent when the system needs to launch the application in the background to process tasks. Tasks arrive in a set, so loop through and process each one.
  30. for task in backgroundTasks {
  31. // Use a switch statement to check the task type
  32. switch task {
  33. case let backgroundTask as WKApplicationRefreshBackgroundTask:
  34. // Be sure to complete the background task once you’re done.
  35. backgroundTask.setTaskCompletedWithSnapshot(false)
  36. case let snapshotTask as WKSnapshotRefreshBackgroundTask:
  37. // Snapshot tasks have a unique completion call, make sure to set your expiration date
  38. snapshotTask.setTaskCompleted(restoredDefaultState: true, estimatedSnapshotExpiration: Date.distantFuture, userInfo: nil)
  39. case let connectivityTask as WKWatchConnectivityRefreshBackgroundTask:
  40. // Be sure to complete the connectivity task once you’re done.
  41. connectivityTask.setTaskCompletedWithSnapshot(false)
  42. case let urlSessionTask as WKURLSessionRefreshBackgroundTask:
  43. // Be sure to complete the URL session task once you’re done.
  44. urlSessionTask.setTaskCompletedWithSnapshot(false)
  45. case let relevantShortcutTask as WKRelevantShortcutRefreshBackgroundTask:
  46. // Be sure to complete the relevant-shortcut task once you're done.
  47. relevantShortcutTask.setTaskCompletedWithSnapshot(false)
  48. case let intentDidRunTask as WKIntentDidRunRefreshBackgroundTask:
  49. // Be sure to complete the intent-did-run task once you're done.
  50. intentDidRunTask.setTaskCompletedWithSnapshot(false)
  51. default:
  52. // make sure to complete unhandled task types
  53. task.setTaskCompletedWithSnapshot(false)
  54. }
  55. }
  56. }
  57. }