Slide 1

Slide 1 text

For Developing URL Routing of SwiftUI App freddi - LINE Fukuoka at potatotips (27th April, 2020)

Slide 2

Slide 2 text

Goal of This Presentation - SwiftUI(SceneDelegate)Ͱ࡞ΔΞϓϦͷURL Routing - URL RoutingΛ࣮૷͢ΔͨΊͷϕʔε஌ࣝΛ஌Δ - iOS12·Ͱͷ࣮૷ͷҧ͍Λ஌Δ

Slide 3

Slide 3 text

URL Routing - URLͳͲΛ։͍ͨΒಛఆͷը໘ʹભҠ͢Δ https://snews.app/alerm snews://alerm

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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 }

Slide 6

Slide 6 text

Parse URL in SceneDelegate // scene(_:, openURLContexts:) func scene(_ scene: UIScene, openURLContexts URLContexts: Set) { // Write down URL Parsing Procedure ... } New - SceneDelegateͷscene(_:, openURLContexts:) - AppDelegate൛͸Deprecated ※ SceneDelegateͷ͋ΔΞϓϦͰ͸ݺ͹Εͳ͘ͳΔ

Slide 7

Slide 7 text

URL Routing - ࣮૷͢Δͷʹඞཁͳ͜ͱ - SceneDelegateͰURLΛύʔε - ύʔε݁ՌΛ΋ͱʹݱࡏ։͍͍ͯΔը໘͔ΒRouting - ݱࡏͷը໘ͷ্ʹը໘Λදࣔ͢Δํ๏

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

Route to Goal Screen from Current Screen - SceneDelegate.window Λ୅ΘΓʹ࢖͏ͱྑ͍ import UIKit import SwiftUI class SceneDelegate: UIResponder, UIWindowSceneDelegate { var window: UIWindow? … New

Slide 10

Slide 10 text

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

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

Route to Goal Screen from Current Screen func scene(_ scene: UIScene, openURLContexts URLContexts: Set) { 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), … }

Slide 15

Slide 15 text

Route to Goal Screen from Current Screen func scene(_ scene: UIScene, openURLContexts URLContexts: Set) { 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ͷ্ʹදࣔ

Slide 16

Slide 16 text

URL Routing - ࣮૷͢Δͷʹඞཁͳ͜ͱ - SceneDelegateͰURLΛύʔε - ύʔε݁ՌΛ΋ͱʹݱࡏ։͍͍ͯΔը໘͔ΒRouting - ݱࡏͷը໘ͷ্ʹը໘Λදࣔ͢Δํ๏

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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()) }() } }

Slide 19

Slide 19 text

NavigatorInsertionView struct NavigatorInsertionView: View NavigationLink("", destination: NavigatiorInsertionView(), tag: Coordinator.MovingState.on, selection: movingState) .frame(width: 0, height: 0, alignment: .bottom)

Slide 20

Slide 20 text

Conclusion of URL Routing of SwiftUI App - ͍͔ͭ͘ͷؔ਺͸iOS13޲͚ʹҠߦ͢Δ͜ͱʹͳΔ - CoodinatorͷΑ͏ͳΫϥεΛ࡞Δํ͕΍ΕΔ͜ͱ͸ଟ͍ - SceneDelegateͷ scene(_ scene:, openURLContexts:) - Navigation͕ͳ͍ͳΒطଘͷVCͷpresentͰ͓̺

Slide 21

Slide 21 text

એ఻

Slide 22

Slide 22 text

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