OnOpenURLModifier.swift 787 B

12345678910111213141516171819202122232425
  1. import Foundation
  2. extension View {
  3. /// Performs an action whenever the system asks the app to handle
  4. /// a URL with a registered scheme.
  5. ///
  6. /// - Parameter action: The action to perform. Receives the URL in
  7. /// question.
  8. public func onOpenURL(
  9. perform action: @escaping (URL) -> Void
  10. ) -> some View {
  11. PreferenceModifier(self) { preferences, environment in
  12. var newPreferences = preferences
  13. newPreferences.onOpenURL = { url in
  14. action(url)
  15. if let innerHandler = preferences.onOpenURL {
  16. innerHandler(url)
  17. } else {
  18. environment.bringWindowForward()
  19. }
  20. }
  21. return newPreferences
  22. }
  23. }
  24. }