Upgrade to Pro — share decks privately, control downloads, hide ads and more …

For Developing URL Routing of SwiftUI App

For Developing URL Routing of SwiftUI App

freddi(Yuki Aki)

April 27, 2020
Tweet

More Decks by freddi(Yuki Aki)

Other Decks in Technology

Transcript

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

    Fukuoka at potatotips (27th April, 2020)
  2. 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. 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. Route to Goal Screen from Current Screen - SceneDelegate.window Λ୅ΘΓʹ࢖͏ͱྑ͍

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

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

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

    - …Ͱ͸ͳ͘ɺैདྷͲ͓ΓͷViewControllerͷํ͕ࣜྑ͍? UIWindow SwiftUIͷ .sheet ͸࣮࣭ViewControllerͷpresent .sheet .present UIHostingController(VC) SwiftUI UIHostingController(VC) SwiftUI
  8. 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), … }
  9. 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ͷ্ʹදࣔ
  10. 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()) }() } }
  11. Conclusion of URL Routing of SwiftUI App - ͍͔ͭ͘ͷؔ਺͸iOS13޲͚ʹҠߦ͢Δ͜ͱʹͳΔ -

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