For Developing URL Routing of SwiftUI App

For Developing URL Routing of SwiftUI App

1405a601755e5fcbfdc93a2560368bb1?s=128

freddi(Yuki Aki)

April 27, 2020
Tweet

Transcript

  1. For Developing URL Routing of SwiftUI App freddi - LINE

    Fukuoka at potatotips (27th April, 2020)
  2. Goal of This Presentation - SwiftUI(SceneDelegate)Ͱ࡞ΔΞϓϦͷURL Routing - URL RoutingΛ࣮૷͢ΔͨΊͷϕʔε஌ࣝΛ஌Δ

    - iOS12·Ͱͷ࣮૷ͷҧ͍Λ஌Δ
  3. URL Routing - URLͳͲΛ։͍ͨΒಛఆͷը໘ʹભҠ͢Δ https://snews.app/alerm snews://alerm

  4. URL Routing - ࣮૷͢Δͷʹඞཁͳ͜ͱ - SceneDelegateͰURLΛύʔε (طଘͷํ๏ͱҧͬͯ͘Δ) - ύʔε݁ՌΛ΋ͱʹݱࡏ։͍͍ͯΔը໘͔ΒRouting -

    ݱࡏͷը໘ͷ্ʹը໘Λදࣔ͢Δํ๏
  5. Parse URL in AppDelegate (Old way) - AppDelegateͷapplication(_:, open:, options:)

    - Ҏલ·Ͱ͸URLͷ৘ใ͸͜ͷؔ਺ʹ౉͞ΕΔ // application(_:, open:, options:) -> Bool func application(_ app: UIApplication, open url: URL, ...) -> Bool { // Write down URL Parsing Procedure ... return true }
  6. Parse URL in SceneDelegate // scene(_:, openURLContexts:) func scene(_ scene:

    UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) { // Write down URL Parsing Procedure ... } New - SceneDelegateͷscene(_:, openURLContexts:) - AppDelegate൛͸Deprecated ※ SceneDelegateͷ͋ΔΞϓϦͰ͸ݺ͹Εͳ͘ͳΔ
  7. URL Routing - ࣮૷͢Δͷʹඞཁͳ͜ͱ - SceneDelegateͰURLΛύʔε - ύʔε݁ՌΛ΋ͱʹݱࡏ։͍͍ͯΔը໘͔ΒRouting - ݱࡏͷը໘ͷ্ʹը໘Λදࣔ͢Δํ๏

  8. Route to Goal Screen from Current Screen - UIApplication.keyWindow ͸Deprecated

  9. Route to Goal Screen from Current Screen - SceneDelegate.window Λ୅ΘΓʹ࢖͏ͱྑ͍

    import UIKit import SwiftUI class SceneDelegate: UIResponder, UIWindowSceneDelegate { var window: UIWindow? … New
  10. Route to Goal Screen from Current Screen - UIWindow ͔ΒSwiftUIͷViewࣗମΛભҠͤ͞Δ΂͖ʁ

    - …Ͱ͸ͳ͘ɺैདྷͲ͓ΓͷViewControllerͷํ͕ࣜྑ͍?
  11. Route to Goal Screen from Current Screen - UIWindow ͔ΒSwiftUIͷViewࣗମΛભҠͤ͞Δ΂͖ʁ

    - …Ͱ͸ͳ͘ɺैདྷͲ͓ΓͷViewControllerͷํ͕ࣜྑ͍? SwiftUIͷ .sheet ͸࣮࣭ViewControllerͷpresent
  12. Route to Goal Screen from Current Screen - UIWindow ͔ΒSwiftUIͷViewࣗମΛભҠͤ͞Δ΂͖ʁ

    - …Ͱ͸ͳ͘ɺैདྷͲ͓ΓͷViewControllerͷํ͕ࣜྑ͍? UIWindow UIHostingController(VC) SwiftUIͷ .sheet ͸࣮࣭ViewControllerͷpresent SwiftUI
  13. Route to Goal Screen from Current Screen - UIWindow ͔ΒSwiftUIͷViewࣗମΛભҠͤ͞Δ΂͖ʁ

    - …Ͱ͸ͳ͘ɺैདྷͲ͓ΓͷViewControllerͷํ͕ࣜྑ͍? UIWindow SwiftUIͷ .sheet ͸࣮࣭ViewControllerͷpresent .sheet .present UIHostingController(VC) SwiftUI UIHostingController(VC) SwiftUI
  14. Route to Goal Screen from Current Screen func scene(_ scene:

    UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) { let url = URLContexts.first?.url let distinationView = parseURLToDistination(url: url) var currentTopVC = window?.rootViewController while true { if let presentedVC = currentTopVC?.presentedViewController { currentTopVC = presentedVC } else { break } } currentTopVC?.present(UIHostingController(rootView: distinationView), … }
  15. Route to Goal Screen from Current Screen func scene(_ scene:

    UIScene, openURLContexts URLContexts: Set<UIOpenURLContext>) { let url = URLContexts.first?.url let distinationView = parseURLToDistination(url: url) var currentTopVC = window?.rootViewController while true { if let presentedVC = currentTopVC?.presentedViewController { currentTopVC = presentedVC } else { break } } currentTopVC?.present(UIHostingController(rootView: distinationView), … } urlΛऔಘ url͔Β໨తͷViewΛऔಘ ݱࡏҰ൪্ʹදࣔ͞Ε͍ͯΔVCΛ୳ࡧ UIHostingControllerͰ໨తͷViewΛ ୳ࡧͨ͠VCͷ্ʹදࣔ
  16. URL Routing - ࣮૷͢Δͷʹඞཁͳ͜ͱ - SceneDelegateͰURLΛύʔε - ύʔε݁ՌΛ΋ͱʹݱࡏ։͍͍ͯΔը໘͔ΒRouting - ݱࡏͷը໘ͷ্ʹը໘Λදࣔ͢Δํ๏

  17. Other Tips - Navigation΋ࢹ໺ʹೖΕΔͳΒCoordinatorύλʔϯ - sheet΍NavigationLinkͷtagͷϑϥά؂ཧ͕൥ࡶ - NavigationͰ޷͖ͳViewΛpushͯ͠ભҠ͢Δͷ͸ʁ - pushViewController૬౰ͷ΋ͷΛऔΓѻ͑ͳ͍

    - NavigationΛUINavigationControllerͱͯ͠ѻ͑ͣ - NavigatorInsertionView(߲࣍)Λར༻͢Δ
  18. NavigatorInsertionView struct NavigatorInsertionView: View { static var contentView: AnyView? =

    nil var body: some View { NavigatorInsertionView.contentView ?? { () -> AnyView in assertionFailure("Could not get AnyView") return AnyView(EmptyView()) }() } }
  19. NavigatorInsertionView struct NavigatorInsertionView: View NavigationLink("", destination: NavigatiorInsertionView(), tag: Coordinator.MovingState.on, selection:

    movingState) .frame(width: 0, height: 0, alignment: .bottom)
  20. Conclusion of URL Routing of SwiftUI App - ͍͔ͭ͘ͷؔ਺͸iOS13޲͚ʹҠߦ͢Δ͜ͱʹͳΔ -

    CoodinatorͷΑ͏ͳΫϥεΛ࡞Δํ͕΍ΕΔ͜ͱ͸ଟ͍ - SceneDelegateͷ scene(_ scene:, openURLContexts:) - Navigation͕ͳ͍ͳΒطଘͷVCͷpresentͰ͓̺
  21. એ఻

  22. https://hakata-swift.connpass.com/event/168308/