Share
Embed
Crossroad - 最高のCustom URL Schemeルーティングを支える技術2021
[iPhone Dev Sapporo feat. HAKATA.swift - connpass](https://hakata-swift.connpass.com/event/232775/)
Transcript
56 slides
-
ࣗݾհ • @giginet (Twitter/GitHub) • ΫοΫύου iOSςοΫϦʔυ • ٕज़ސɿtaskey /
MoneyForward • Core Contributor: fastlane / Carthage / XcodeGen etc... 2 -
iPhone Dev Sapporo • ֶੜ࣌ɺࡳຈʹ͍ͨͷͰੲࢀՃ͍ͯͨ͠ • ॳࢀՃ2010ࠒͷݹࢀ ! • iPhone
4 + iOS 4ͷࠒ • Swift? • ͭ·ΓζϒζϒͬͯΔ " 3 -
Custom URL Schemeͱ • ΧελϜͨ͠URL͔ΒΞϓϦΛىಈͤ͞Δ͘͠Έ Defining a Custom URL Scheme
for Your App | Apple Developer Documentation 6 -
Custom URL SchemeΛऔΓר͘ • ϧʔςΟϯά͕૿͑ΔͱϋϯυϦϯά͕ΊͪΌͪ͘Όେม • ༷ΛύοͱѲͮ͠Β͍ • ͳΜ͔͍Ζ͍Ζ༻ޠભҠํ๏͕͍͋ͬͯ͠ •
Deep Link?, Universal Link?, Custom URL Scheme?, Dynamic Links? 8 -
Before class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ app: UIApplication,
open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool { let url = URL(string: "pokedex://pokemons/25")! let components = url.pathComponents if url.scheme == "pokedex" { if url.host == "pokemons" { if components.count == 2, let pokedexID: Int = Int(components[1]) { presentPokemonDetailViewController(of: pokedexID) return true } else if ( ... ) { } else { // ... } } } } } 11 -
Before class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ app: UIApplication,
open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool { let url = URL(string: "pokedex://pokemons/25")! let components = url.pathComponents if url.scheme == "pokedex" { if url.host == "pokemons" { if components.count == 2, let pokedexID: Int = Int(components[1]) { presentPokemonDetailViewController(of: pokedexID) return true } else if ( ... ) { } else { // ... } } } } } 11 -
Before class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ app: UIApplication,
open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool { let url = URL(string: "pokedex://pokemons/25")! let components = url.pathComponents if url.scheme == "pokedex" { if url.host == "pokemons" { if components.count == 2, let pokedexID: Int = Int(components[1]) { presentPokemonDetailViewController(of: pokedexID) return true } else if ( ... ) { } else { // ... } } } } } 11 -
After import Crossroad let router = try! DefaultRouter(accepting: [.customURLScheme("pokedex")]) {
registry in registry.route("/pokemons/:pokedexID") { context in let pokedexID: Int = try context.argument(named: "pokedexID") presentPokedexDetailViewController(of: pokedexID) } } class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool { router.openIfPossible(url, options: options) } } 13 -
After import Crossroad let router = try! DefaultRouter(accepting: [.customURLScheme("pokedex")]) {
registry in registry.route("/pokemons/:pokedexID") { context in let pokedexID: Int = try context.argument(named: "pokedexID") presentPokedexDetailViewController(of: pokedexID) } } class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool { router.openIfPossible(url, options: options) } } 13 -
After import Crossroad let router = try! DefaultRouter(accepting: [.customURLScheme("pokedex")]) {
registry in registry.route("/pokemons/:pokedexID") { context in let pokedexID: Int = try context.argument(named: "pokedexID") presentPokedexDetailViewController(of: pokedexID) } } class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool { router.openIfPossible(url, options: options) } } 13 -
After import Crossroad let router = try! DefaultRouter(accepting: [.customURLScheme("pokedex")]) {
registry in registry.route("/pokemons/:pokedexID") { context in let pokedexID: Int = try context.argument(named: "pokedexID") presentPokedexDetailViewController(of: pokedexID) } } class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool { router.openIfPossible(url, options: options) } } 13 -
After import Crossroad let router = try! DefaultRouter(accepting: [.customURLScheme("pokedex")]) {
registry in registry.route("/pokemons/:pokedexID") { context in let pokedexID: Int = try context.argument(named: "pokedexID") presentPokedexDetailViewController(of: pokedexID) } } class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool { router.openIfPossible(url, options: options) } } 13 -
After import Crossroad let router = try! DefaultRouter(accepting: [.customURLScheme("pokedex")]) {
registry in registry.route("/pokemons/:pokedexID") { context in let pokedexID: Int = try context.argument(named: "pokedexID") presentPokedexDetailViewController(of: pokedexID) } } class AppDelegate: UIResponder, UIApplicationDelegate { func application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool { router.openIfPossible(url, options: options) } } 13 -
աڈͷհهࣄ • iOSΞϓϦͷେنͳCustom URL SchemeΛࢧ͑Δٕज़ - Ϋο Ϋύου։ൃऀϒϩά • https://techlife.cookpad.com/entry/2018/06/01/113000
• Custom URL SchemeΛࢧ͑Δٕज़ - Speaker Deck • https://speakerdeck.com/giginet/custom-url-schemewozhi- eruji-shu?slide=10 16 -
ॻ͖͍͢DSL Before let router = DefaultRouter(scheme: "pokedex") router.register([ ("pokedex://pokemons/:pokedexID", {
context in guard let pokedexID: Int = try? context.arguments(for: "pokedexID") else { return false } presentPokemonDetailViewController(of: pokedexID) return true }) ]) • [(String, (Context) -> Bool)]Ͱهड़ • ޭɺࣦഊΛBoolͰฦ͍ͯͨ͠ 19 -
ॻ͖͍͢DSL Before let router = DefaultRouter(scheme: "pokedex") router.register([ ("pokedex://pokemons/:pokedexID", {
context in guard let pokedexID: Int = try? context.arguments(for: "pokedexID") else { return false } presentPokemonDetailViewController(of: pokedexID) return true }) ]) • [(String, (Context) -> Bool)]Ͱهड़ • ޭɺࣦഊΛBoolͰฦ͍ͯͨ͠ 19 -
After let router = try! DefaultRouter(accepting: [.customURLScheme("pokedex")]) { registry in
registry.route("/pokemons/:pokedexID") { context in let pokedexID: Int = try context.argument(named: "pokedexID") presentPokedexDetailViewController(of: pokedexID) } } • ResultBuilderΛͬͯײతʹॻ͚ΔΑ͏ʹͳͬͨ 20 -
Result Builder • Swift 5.4͔Βར༻Մೳʹͳͬͨ৽͍͠ݴޠػೳ • એݴతͳAPIઃܭΛ࣮ݱ͢Δ͜ͱ͕ग़དྷΔ • SwiftUIͷએݴతUIΛ࣮ݱ͢ΔͨΊʹݴޠػೳʹΈࠐ·Εͨ VStack(alignment:
.leading) { Text("Turtle Rock") .font(.title) HStack { Text("Joshua Tree National Park") .font(.subheadline) Spacer() Text("California") .font(.subheadline) } } 21 -
Result BuilderͰDSLΛઃܭ͠Α͏ • ResultBuilderͷ͍ํΛ͠ग़͢ͱ1͙࣌ؒΒ͍͔͔Δ • Write a DSL in Swift
using result builders - WWDC21 - Videos - Apple Developer • WWDCͷ͜ͷηογϣϯ͕Φεεϝ 22 -
Router { Route("/pokemon/:pokedexID") { context in // do something }
Route("/pokemons") { context in // do something } // ... } • ϒϩοΫͷதͷRouteͷཏྻΛ [Route] ͱͯ͠औΓ͍ͨ 23 -
@resultBuilder struct RouteBuilder { static func buildBlock(_ components: Route...) ->
[Route] { components } } struct Router { init(@RouteBuilder _ routeBuilder: () -> [Route]) { self.routes = routeBuilder() } } 24 -
@resultBuilder struct RouteBuilder { static func buildBlock(_ components: Route...) ->
[Route] { components } } struct Router { init(@RouteBuilder _ routeBuilder: () -> [Route]) { self.routes = routeBuilder() } } 24 -
• CrossroadͷDSLͬͱෳࡶ • ܕύϥϝʔλʔͷӅṭ • Group(ޙड़)ͷଘࡏɹ • ࣮ྫΛ͝ཡ͍ͩ͘͞ https://github.com/giginet/Crossroad/ blob/master/Sources/Crossroad/DSL.swift
• ResultBuilderΛͬͨDSLઃܭͷݟ͕ཷ·͔ͬͨΒ·ͨͲ͜ ͔Ͱ͍ͨ͠ 25 -
2. Universal LinkDeferred Deep Linkͱͷܦ࿏ڞ༗ • ݱ࣮ͷϏδωεʹCustom URL SchemeͷଞʹɺUniversal LinkFirebase
Dynamic Links(Deferred Deep Link)ͳͲɺ༷ʑͳ ΞϓϦૹ٬ख๏͕͋Δ • ࠓ·ͰͷCrossroadɺෳͷྲྀೖܦ࿏Λѻ͏͜ͱ͕Ͱ͖ͳ ͔ͬͨ 26 -
ෳͷྲྀೖܦ࿏ͷϧʔςΟϯάΛଋͶͯɺಉ͡ڍಈΛ͍ͤͨ͞ • Custom URL Scheme • pokedex://pokemons/25 • Universal Link
• https://my-pokedex.com/pokemons/25 27 -
Router͕ड͚͚ΔϦϯΫݩΛෳऔΕΔΑ͏ʹ let router = try! DefaultRouter(accepting: [ .customURLScheme("pokedex"), .universalLink(URL(string: "https://my-pokedex.com")!)
]) { registry in registry.route("/pokemons/:pokedexID") { context in let pokedexID: Int = try context.argument(named: "pokedexID") presentPokedexDetailViewController(of: pokedexID) } } 28 -
Router͕ड͚͚ΔϦϯΫݩΛෳऔΕΔΑ͏ʹ let router = try! DefaultRouter(accepting: [ .customURLScheme("pokedex"), .universalLink(URL(string: "https://my-pokedex.com")!)
]) { registry in registry.route("/pokemons/:pokedexID") { context in let pokedexID: Int = try context.argument(named: "pokedexID") presentPokedexDetailViewController(of: pokedexID) } } 28 -
Group let router = try! DefaultRouter(accepting: [ .customURLScheme("pokedex"), .universalLink(URL(string: "https://my-pokedex.com")!)
]) { registry in registry.route("/pokemons/:pokedexID") { context in let pokedexID: Int = try context.argument(named: "pokedexID") presentPokedexDetailViewController(of: pokedexID) } // ͜ͷGroupʹғ·ΕͨϧʔτఆٛCustom URL Schemeܦ༝Ͱ͔͠ΞΫηεͰ͖ͳ͍ registry.group(accepting: [.customURLScheme("pokedex")]) { group in group.route("/moves/:moveID") { context in // ... } } } 29 -
Group let router = try! DefaultRouter(accepting: [ .customURLScheme("pokedex"), .universalLink(URL(string: "https://my-pokedex.com")!)
]) { registry in registry.route("/pokemons/:pokedexID") { context in let pokedexID: Int = try context.argument(named: "pokedexID") presentPokedexDetailViewController(of: pokedexID) } // ͜ͷGroupʹғ·ΕͨϧʔτఆٛCustom URL Schemeܦ༝Ͱ͔͠ΞΫηεͰ͖ͳ͍ registry.group(accepting: [.customURLScheme("pokedex")]) { group in group.route("/moves/:moveID") { context in // ... } } } 29 -
ྫ1ɿෳͷϧʔτ͕ॏෳ͍ͯ͠Δέʔε let router = try! DefaultRouter(accepting: [.customURLScheme("pokedex")]) { registry in
registry.route("/pokemons/:pokedexID") { context in let pokedexID: Int = try context.argument(named: "pokedexID") presentPokedexDetailViewController(of: pokedexID) } // ... // ͏͔ͬΓಉ͡ఆ͕ٛॏෳ registry.route("/pokemons/:pokedexID") { context in } } 32 -
ྫ1ɿෳͷϧʔτ͕ॏෳ͍ͯ͠Δέʔε let router = try! DefaultRouter(accepting: [.customURLScheme("pokedex")]) { registry in
registry.route("/pokemons/:pokedexID") { context in let pokedexID: Int = try context.argument(named: "pokedexID") presentPokedexDetailViewController(of: pokedexID) } // ... // ͏͔ͬΓಉ͡ఆ͕ٛॏෳ registry.route("/pokemons/:pokedexID") { context in } } 32 -
ྫ2ɿͦͦड͚͚ͳ͍ϦϯΫݩΛGroupʹ͍ͯ͠ Δέʔε let router = try! DefaultRouter(accepting: [.customURLScheme("pokedex")]) { registry
in registry.group(accepting: [.customURLScheme("unknown")]) { group in group.route("/moves/:moveID") { context in // ... } } } 33 -
ྫ2ɿͦͦड͚͚ͳ͍ϦϯΫݩΛGroupʹ͍ͯ͠ Δέʔε let router = try! DefaultRouter(accepting: [.customURLScheme("pokedex")]) { registry
in registry.group(accepting: [.customURLScheme("unknown")]) { group in group.route("/moves/:moveID") { context in // ... } } } 33 -
1. Swift ConcurrencyʹΑΔϝΠϯεϨουอূ • جຊతʹURLεΩʔϜͷల։ɺଞͷϏϡʔʹભҠ͢ΔͳͲɺ UIͷૢ࡞Λߦ͏ͨΊɺϝΠϯεϨουͰಈ͖͘ registry.route("/pokemons/:pokedexID") { context in
let pokedexID: Int = try context.argument(named: "pokedexID") presentPokedexDetailViewController(of: pokedexID) // ϝΠϯεϨουͰಈ͍ͯཉ͍͠ } 35 -
• HandlerͱopenIfPossibleͷܕΛ @MainActor ʹ͢Δ typealias Handler = @MainActor (Context<UserInfo>) throws
-> Void @MainActor public func openIfPossible(_ url: URL, userInfo: UserInfo) -> Bool 37 -
• ͜ΕʹΑͬͯopenIfPossibleϝΠϯεϨου͔Β͔࣮͠ߦͰ ͖ͳ͘ͳΔͨΊɺAppDelegateࣗମ@MainActorͱ͢Δ @MainActor class AppDelegate: UIResponder, UIApplicationDelegate { func
application(_ app: UIApplication, open url: URL, options: [UIApplication.OpenURLOptionsKey: Any] = [:]) -> Bool { return router.openIfPossible(url, options: options) } } • Routerʹ͞Εͨhandler͕ϝΠϯεϨουͰಈ͘͜ͱ͕อূ͞ ΕΔΑ͏ʹͳΓ҆શ ! • https://github.com/giginet/Crossroad/pull/43 38 -
2. DocCରԠ • Documentation Compiler • ؆୯ʹΦϯϥΠϯυΩϡϝϯτ͕࡞ΕΔͷͰ༻ҙ͍ͨ͠ • ӳޠͱ͍͏͍͠ݴޠ͕ॻ͚ͳ͍ͷͰ͕͔͔͍࣌ؒͬͯΔ •
Θ͍Θ͍DocC ~ waiwai-docc ~ - Speaker Deck • https://speakerdeck.com/giginet/waiwaidocc-waiwai-docc 39 -
3. ෳϧʔςΟϯάରԠ registry.route(["/pokemons/:pokedexID", "/:pokedexID"]) { context in let pokedexID: Int
= try context.argument(named: "pokedexID") presentPokedexDetailViewController(of: pokedexID) // ϝΠϯεϨουͰಈ͍ͯཉ͍͠ } • pokedex://pokemons/25Ͱpokedex://25Ͱड͚͚ΔΑ͏ʹ • ϨγϐΞϓϦͷϧʔςΟϯά͕ෳࡶ͍͍͗ͯͨ͢ 40