ExtensionDelegate.swift 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. //
  2. // ExtensionDelegate.swift
  3. //
  4. // Copyright (c) 2020 Alamofire Software Foundation (http://alamofire.org/)
  5. //
  6. // Permission is hereby granted, free of charge, to any person obtaining a copy
  7. // of this software and associated documentation files (the "Software"), to deal
  8. // in the Software without restriction, including without limitation the rights
  9. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  10. // copies of the Software, and to permit persons to whom the Software is
  11. // furnished to do so, subject to the following conditions:
  12. //
  13. // The above copyright notice and this permission notice shall be included in
  14. // all copies or substantial portions of the Software.
  15. //
  16. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  18. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  19. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  20. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  21. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  22. // THE SOFTWARE.
  23. //
  24. import WatchKit
  25. class ExtensionDelegate: NSObject, WKExtensionDelegate {
  26. func handle(_ backgroundTasks: Set<WKRefreshBackgroundTask>) {
  27. // 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.
  28. for task in backgroundTasks {
  29. // Use a switch statement to check the task type
  30. switch task {
  31. case let backgroundTask as WKApplicationRefreshBackgroundTask:
  32. // Be sure to complete the background task once you’re done.
  33. backgroundTask.setTaskCompletedWithSnapshot(false)
  34. case let snapshotTask as WKSnapshotRefreshBackgroundTask:
  35. // Snapshot tasks have a unique completion call, make sure to set your expiration date
  36. snapshotTask.setTaskCompleted(restoredDefaultState: true, estimatedSnapshotExpiration: Date.distantFuture, userInfo: nil)
  37. case let connectivityTask as WKWatchConnectivityRefreshBackgroundTask:
  38. // Be sure to complete the connectivity task once you’re done.
  39. connectivityTask.setTaskCompletedWithSnapshot(false)
  40. case let urlSessionTask as WKURLSessionRefreshBackgroundTask:
  41. // Be sure to complete the URL session task once you’re done.
  42. urlSessionTask.setTaskCompletedWithSnapshot(false)
  43. case let relevantShortcutTask as WKRelevantShortcutRefreshBackgroundTask:
  44. // Be sure to complete the relevant-shortcut task once you're done.
  45. relevantShortcutTask.setTaskCompletedWithSnapshot(false)
  46. case let intentDidRunTask as WKIntentDidRunRefreshBackgroundTask:
  47. // Be sure to complete the intent-did-run task once you're done.
  48. intentDidRunTask.setTaskCompletedWithSnapshot(false)
  49. default:
  50. // make sure to complete unhandled task types
  51. task.setTaskCompletedWithSnapshot(false)
  52. }
  53. }
  54. }
  55. }