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. 1.

    For Developing URL Routing of SwiftUI App freddi - LINE

    Fukuoka at potatotips (27th April, 2020)
  2. 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 }
  3. 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ͷ͋ΔΞϓϦͰ͸ݺ͹Εͳ͘ͳΔ
  4. 9.

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

    import UIKit import SwiftUI class SceneDelegate: UIResponder, UIWindowSceneDelegate { var window: UIWindow? … New
  5. 10.
  6. 11.

    Route to Goal Screen from Current Screen - UIWindow ͔ΒSwiftUIͷViewࣗମΛભҠͤ͞Δ΂͖ʁ

    - …Ͱ͸ͳ͘ɺैདྷͲ͓ΓͷViewControllerͷํ͕ࣜྑ͍? SwiftUIͷ .sheet ͸࣮࣭ViewControllerͷpresent
  7. 12.

    Route to Goal Screen from Current Screen - UIWindow ͔ΒSwiftUIͷViewࣗମΛભҠͤ͞Δ΂͖ʁ

    - …Ͱ͸ͳ͘ɺैདྷͲ͓ΓͷViewControllerͷํ͕ࣜྑ͍? UIWindow UIHostingController(VC) SwiftUIͷ .sheet ͸࣮࣭ViewControllerͷpresent SwiftUI
  8. 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
  9. 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), … }
  10. 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ͷ্ʹදࣔ
  11. 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()) }() } }
  12. 20.

    Conclusion of URL Routing of SwiftUI App - ͍͔ͭ͘ͷؔ਺͸iOS13޲͚ʹҠߦ͢Δ͜ͱʹͳΔ -

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