LifecycleNotifications.swift 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. //
  2. // Copyright 2022 Google LLC
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. import XCTest
  16. import Dispatch
  17. #if os(iOS) || os(tvOS) || os(visionOS)
  18. import UIKit
  19. #elseif os(macOS)
  20. import AppKit
  21. import Cocoa
  22. #elseif os(watchOS)
  23. import WatchKit
  24. #endif // os(iOS) || os(tvOS)
  25. @MainActor func postBackgroundedNotification() {
  26. // On Catalyst, the notifications can only be called on the main thread
  27. let notificationCenter = NotificationCenter.default
  28. #if os(iOS) || os(tvOS) || os(visionOS)
  29. notificationCenter.post(name: UIApplication.didEnterBackgroundNotification, object: nil)
  30. #elseif os(macOS)
  31. notificationCenter.post(name: NSApplication.didResignActiveNotification, object: nil)
  32. #elseif os(watchOS)
  33. if #available(watchOSApplicationExtension 7.0, *) {
  34. notificationCenter.post(
  35. name: WKExtension.applicationDidEnterBackgroundNotification,
  36. object: nil
  37. )
  38. }
  39. #endif // os(iOS) || os(tvOS)
  40. }
  41. @MainActor func postForegroundedNotification() {
  42. // On Catalyst, the notifications can only be called on a the main thread
  43. let notificationCenter = NotificationCenter.default
  44. #if os(iOS) || os(tvOS) || os(visionOS)
  45. notificationCenter.post(name: UIApplication.didBecomeActiveNotification, object: nil)
  46. #elseif os(macOS)
  47. notificationCenter.post(name: NSApplication.didBecomeActiveNotification, object: nil)
  48. #elseif os(watchOS)
  49. if #available(watchOSApplicationExtension 7.0, *) {
  50. notificationCenter.post(
  51. name: WKExtension.applicationDidBecomeActiveNotification,
  52. object: nil
  53. )
  54. }
  55. #endif // os(iOS) || os(tvOS)
  56. }