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

既存アプリへのSwiftUI導入事例

 既存アプリへのSwiftUI導入事例

F551b41a97314625f112881b96ff5117?s=128

Takashico

July 05, 2021
Tweet

Other Decks in Programming

Transcript

  1. طଘΞϓϦ΁ͷSwiftUIಋೖࣄྫ 2021/6/30 Shiko Takahashi

  2. 2 ࣗݾ঺հ • Shiko Takahashi • iOSΞϓϦΤϯδχΞ • Android΋͸͡Ί·ͨ͠ʂ •

    ٳΈͷ೔͸ɺೣͱ༡Ϳ ɻ @takashico @takashico smtakahashi
  3. 3 ຊ೔͓࿩͢͠Δ͜ͱ

  4. 4 ຊ೔͓࿩͢͠Δ͜ͱ • SwiftUIͱ͸ • ಋೖͷ͖͔͚ͬ • ಋೖલͷݕ౼ʹ͍ͭͯ • ·ͱΊ

  5. 5 SwiftUIͱ͸

  6. 6 SwiftUIͱ͸ • ৽͍͠UIϑϨʔϜϫʔΫʢWWDC19ʣ ◦ গͳ͍ίʔυͰUIߏங͕Մೳ ◦ એݴܕγϯλοΫεʢγϯϓϧʣ ◦ ϓϨϏϡʔػೳʢσόοάޮ཰UPʣ

    ◦ etc… • αϙʔτΛiOS13.0Ҏ্ʹ͢Δඞཁ͕͋Δ SwiftUI ΑΓ༏ΕͨAppΛɺΑΓগͳ͍ίʔυͰɻ ࢀর: https://developer.apple.com/jp/xcode/swiftui
  7. 7 SwiftUIͱ͸ • ৽͍͠UIϑϨʔϜϫʔΫʢWWDC19ʣ ◦ গͳ͍ίʔυͰUIߏங͕Մೳ ◦ એݴܕγϯλοΫεʢγϯϓϧʣ ◦ ϓϨϏϡʔػೳʢσόοάޮ཰UPʣ

    ◦ etc… • αϙʔτΛiOS13.0Ҏ্ʹ͢Δඞཁ͕͋Δ SwiftUI ΑΓ༏ΕͨAppΛɺΑΓগͳ͍ίʔυͰɻ ࢀর: https://developer.apple.com/jp/xcode/swiftui ͜ͷ੍໿ʹΑͬͯಋೖ͕೉͔ͬͨ͠
  8. 8 SwiftUIͱ͸ • ৽͍͠UIϑϨʔϜϫʔΫʢWWDC19ʣ ◦ গͳ͍ίʔυͰUIߏங͕Մೳ ◦ એݴܕγϯλοΫεʢγϯϓϧʣ ◦ ϓϨϏϡʔػೳʢσόοάޮ཰UPʣ

    ◦ etc… • αϙʔτΛiOS13.0Ҏ্ʹ͢Δඞཁ͕͋Δ SwiftUI ΑΓ༏ΕͨAppΛɺΑΓগͳ͍ίʔυͰɻ ࢀর: https://developer.apple.com/jp/xcode/swiftui ͜ͷ੍໿ʹΑͬͯಋೖ͕೉͔ͬͨ͠ εϖʔεϚʔέοτΞϓϦʹ ͍͔ͭಋೖ͍ͨ͠ͳͱߟ͍͑ͯ·ͨ͠🤔 ʢಋೖલ·Ͱ͸iOS11.4·Ͱαϙʔτͯ͠ ͍·ͨ͠ʣ
  9. 9 ಋೖͷ͖͔͚ͬ

  10. 10 ಋೖͷ͖͔͚ͬ ϗʔϜը໘ͷUIϦχϡʔΞϧΛߦ͏͜ͱʹɻ

  11. 11 ಋೖͷ͖͔͚ͬ ৽͍͠ϗʔϜը໘

  12. 12 ಋೖͷ͖͔͚ͬ ৽͍͠ϗʔϜը໘ ͔̍Β࡞Γ௚͢ඞཁ͕͋ΔͷͰɺ ͜ͷػձʹSwiftUIͰ΍ͬͯΈ͍ͨ😃 ͍͟ݕ౼ͯ͠ΈΑ͏ʂ

  13. 13 ಋೖલͷݕ౼

  14. 14 SwiftUIಋೖલͷݕ౼ 1. αϙʔτΛiOS13.0Ҏ্ʹͰ͖Δ͔ʁ 2. SwiftUIͰߏஙՄೳͳը໘ͳͷ͔ʁ 3. ϝϦοτ͕͋Δ͔ʁ

  15. 15 1. αϙʔτΛiOS13.0Ҏ্ʹ͢Δ͜ͱ͕Ͱ͖Δ͔ʁ

  16. 16 αϙʔτΛiOS13.0Ҏ্ʹͰ͖Δ͔ʁ • શମʢ৽نϢʔβʔʣ ◦ iOSόʔδϣϯผͷར༻ঢ়گΛ֬ೝ ◦ iOSͷΞοϓσʔτ༧ఆ • εϖʔεϚʔέοτʢطଘϢʔβʔʣ

    ◦ iOSόʔδϣϯผͷར༻ঢ়گΛ֬ೝ ͦΕͧΕiOS13.0Ҏ্ʹ͋͛ͯ΋໰୊ͳ͍ͷ͔Λ֬ೝ͍͖ͯ͠·͢
  17. 17 શମతͳiOSόʔδϣϯ࢖༻཰ͱঢ়گ

  18. 18 աڈ4೥ؒʹಋೖ͞ΕͨσόΠεͷiOSόʔδϣϯ࢖༻཰ 90% 8% 2% ࢀর: https://developer.apple.com/jp/support/app-store ※ 2021೥6݄3೔࣌఺

  19. 19 աڈ4೥ؒʹಋೖ͞ΕͨσόΠεͷiOSόʔδϣϯ࢖༻཰ 90% 8% 2% ࢀর: https://developer.apple.com/jp/support/app-store ※ 2021೥6݄3೔࣌఺ 98%ͷσόΠεͰ

    iOS13Ҏ্͕ਁಁ ͍ͯ͠Δ
  20. 20 ࠓळࠒʹiOS15͕ϦϦʔε༧ఆ ࢀর: https://www.apple.com/apple-events/june-2021

  21. 21 ࠓळࠒʹiOS15͕ϦϦʔε༧ఆ ࢀর: https://www.apple.com/apple-events/june-2021 ϦϦʔεޙ͸ɺ iOS13ະຬͷ࢖༻ ཰͕ݮগ͍ͯ͘͠ ͜ͱ͕ݟࠐΊΔ

  22. 22 શମతͳiOSόʔδϣϯผ࢖༻཰ͱঢ়گ iOS13Ҏ্͸98%ਁಁ͍ͯ͠Δ + ࠓळʹiOS15͕ϦϦʔε༧ఆ SwiftUIΛಋೖͰ͖Δ؀ڥ͕੔͖͍ͬͯͯΔ

  23. 23 εϖʔεϚʔέοτΞϓϦͷ iOSόʔδϣϯผར༻ঢ়گΛ֬ೝ

  24. 24 εϖʔεϚʔέοτΞϓϦͷiOSผར༻ঢ়گͷ֬ೝ ڐ༰ൣғͰ͋Δ͜ͱΛ֬ೝ App Store ConnectͰiOSόʔδϣϯผͷར༻ঢ়گΛ֬ೝͰ͖·͢ iOS12ҎԼͷར༻ ཰͸શମͷ0.3%΄ ͲͰͨ͠ɻ

  25. 25 αϙʔτΛiOS13.0Ҏ্ʹͰ͖Δ͔ʁ • શମ ◦ 98%ͷσόΠεͰiOS13Ҏ্Λ࢖༻͍ͯ͠Δʢաڈ4೥ؒͷσόΠεʣ ◦ ࠓळʹiOS15͕ϦϦʔε༧ఆ • εϖʔεϚʔέοτ

    ◦ iOS12ҎԼͷαϙʔτΛ੾Δ͜ͱ͕Ͱ͖Δ iOS13.0ʹ͢Δ͜ͱ͕Մೳ
  26. 26 2. SwiftUIͰߏஙՄೳͳը໘ͳͷ͔ʁ ʢཁ݅Λຬͨ͢͜ͱ͕Ͱ͖Δ͔ʣ

  27. 27 SwiftUIͰߏஙՄೳͳը໘ͳͷ͔ʁ • σβΠϯ௨Γͷը໘Λ࡞੒Ͱ͖Δͷ͔ʁ ◦ ຊ࣮૷͢Δલʹ͋Δఔ౓࢖༻͢ΔAPIͳͲΛ֬ೝ • ϗʔϜը໘ͷ࣮૷ͷͨΊɺͳΔ΂͘ϦεΫΛආ͚࣮ͨ૷͕Ͱ͖Δ͔ʁ ◦ ੹຿Λ෼͚࣮ͨ૷͕Ͱ͖Δ͔֬ೝ

    ◦ iOS13ಛ༗ͷ໰୊Λճආ
  28. 28 σβΠϯ௨Γͷը໘Λ࡞੒Ͱ͖Δͷ͔ʁ

  29. 29 σβΠϯ௨Γͷը໘Λ࡞੒Ͱ͖Δͷ͔ʁ • Ϧετදࣔʢ֊૚ʣ • CellͷλοϓʹΑͬͯల։ঢ়ଶΛ੍ޚͰ͖Δ ྫ͑͹ɺҎԼͷը໘͕࡞੒Ͱ͖Δͷ͔Λ֬ೝͯ͠ΈΔ

  30. 30 σβΠϯ௨Γͷը໘Λ࡞੒Ͱ͖Δͷ͔ʁ • ListʢchildrenʣΛ࢖༻͢Δํ๏ • DisclosureGroupΛ࢖༻͢Δํ๏ ͜ΕΒͷ࣮૷ํ๏Ͱ࣮ݱͰ͖Δ͜ͱΛ֬ೝ // isExpanded: ల։ϑϥά

    DisclosureGroup( isExpanded: $isExpanded, content: { // ࢠΧςΰϦCell }, label: { // ਌ΧςΰϦCell } ) // categories: ΧςΰϦʔͷ഑ྻ List(categories, children: \.children) { item in // ࢠΧςΰϦCell }
  31. 31 σβΠϯ௨Γͷը໘Λ࡞੒Ͱ͖Δͷ͔ʁ • ListʢchildrenʣΛ࢖༻͢Δํ๏ • DissclosureGroupΛ࢖༻͢Δํ๏ ͜ΕΒͷ࣮૷ํ๏Ͱ࣮ݱͰ͖Δ͜ͱΛ֬ೝ // isExpanded: ల։ϑϥά

    DisclosureGroup( isExpanded: $isExpanded, content: { // ࢠΧςΰϦCell }, label: { // ਌ΧςΰϦCell } ) // categories: ΧςΰϦʔͷ഑ྻ List(categories, children: \.children) { item in // ࢠΧςΰϦCell } ଞͷը໘΋࣮ݱՄೳͰ͋Δ͜ͱΛ֬ೝ͠ ·ͨ͠ɻ ※ ຊ࣮૷ͯ͠Έͳ͍ͱΘ͔Βͳ͍఺΋ଟ͍ͷ Ͱɺ͋͘·Ͱ΋βοΫϦͱ֬ೝ͠·ͨ͠ɻ
  32. 32 ϦεΫΛආ͚࣮ͨ૷͕Ͱ͖Δ͔ʁ ʢ੹຿Λ෼͚࣮ͨ૷ʣ

  33. 33 UIKitͱซ༻࣮ͨ͠૷

  34. 34 UIKitͱซ༻࣮ͨ͠૷ • SwiftUIͰ࡞੒ͨ͠ը໘ΛViewͱͯ͠ѻ͏͜ ͱͰॊೈʹରԠ ◦ ΋͠໰୊͕ൃੜͨ͠৔߹ʹ͸ɺ෦෼త ʹ࢖༻͢Δ͜ͱͳͲॊೈʹରԠՄೳ • ίϯτϩʔϥʔ෦ͷෆ۩߹͸க໋తͳӨڹΛ

    ༩͑ΔڪΕ͕͋ΔͨΊɺViewControllerʹ੹ ຿Λ࣋ͨͤΔߏ଄Ͱ࣮૷ • UIHostingControllerΛ࢖ͬͯUIKitଆ͔Β SwiftUIͰ࡞੒ͨ͠ΦϒδΣΫτΛੜ੒Ͱ͖Δ
  35. 35 SwiftUIΛ૊ΈࠐΉίʔυ // HomeContainerView: SwiftUIͰ࡞੒ͨ͠ը໘ let homeContainerVC = UIHostingController(rootView: HomeContainerView())

    guard let homeContainerView = homeContainerVC.view else { return } self.addChild(homeContainerVC) homeContainerVC.didMove(toParent: self) self.view.addSubview(homeContainerView) // Auto Layout੍໿Λઃఆ homeContainerView.translatesAutoresizingMaskIntoConstraints = false NSLayoutConstraint.activate([ homeContainerView.leadingAnchor.constraint(equalTo: self.view.leadingAnchor, constant: 0.0), homeContainerView.trailingAnchor.constraint(equalTo: self.view.trailingAnchor, constant: 0.0), homeContainerView.topAnchor.constraint(equalTo: self.view.topAnchor, constant: 0.0), homeContainerView.bottomAnchor.constraint(equalTo: self.view.bottomAnchor, constant: 0.0) ])
  36. 36 UIKitͱซ༻࣮ͨ͠૷ SwiftUIͰ࡞੒ͨ͠ViewΛѻ͍΍͘͢͢ΔͨΊʹ • delegate ◦ ΞΫγϣϯͷॲཧ • dataSource ◦

    σʔλͷߋ৽
  37. 37 Delegate λοϓΞΫγϣϯ

  38. 38 Delegate /// DelegateΛఆٛ protocol BannerViewDelegate: AnyObject { func didSelectBanner(uid:

    String) } /// BannerViewDelegateͷॲཧ extension HomeViewController: BannerViewDelegate { func didSelectBanner(uid: String) { // ը໘ભҠͷॲཧ } } HomeViewController.swift struct BannerView: View { weak var delegate: BannerViewDelegate? var body: some View { // όφʔը૾ʹରͯ͠onTapGestureΛઃఆ Image(”όφʔը૾”) .onTapGesture { self.delegate?.didSelectBanner(uid: "όφʔUid") } } } BannerView.swift
  39. 39 DataSource /// ObservableObjectϓϩτϩϧΛܧঝͨ͠ΫϥεͰఆٛ͢Δ class DataSource: ObservableObject { @Published var

    banners: [Banner] = [] } // ஋Λߋ৽͢Δ BannerView.DataSorce.banners = ["όφʔ৘ใ"] σʔλΛऔಘ dataSourceʢObservedObjectʣ ͷ஋Λ؂ࢹ͍ͯ͠ΔͨΊɺ ஋ʢbannersʣ͕ߋ৽͞Εͨ৔߹͸ Viewͷ࠶ඳը͕ߦΘΕΔ ᶃ ᶄ ᶅ dataSourceʢObservedObjectʣ ʹऔಘͨ͠σʔλΛ౉͢
  40. 40 • SwiftUIͰ࡞੒ͨ͠ը໘ΛViewͱͯ͠ѻ͏͜ ͱͰϦεΫϔοδʢॊೈʹରԠͰ͖Δʣ • delegate, dataSourceͳͲఆٛͯ͠ѻ͍΍͢ ࣮͘૷ UIKitͱซ༻࣮ͨ͠૷

  41. 41 ϦεΫΛආ͚࣮ͨ૷͕Ͱ͖Δ͔ʁ ʢiOS13ಛ༗ͷ໰୊Λճආʣ

  42. 42 iOS13ಛ༗ͷ໰୊Λճආ • ಈ࡞͕ෆ҆ఆͱ͍͏ใࠂ͕͋Δ ◦ SwiftUI NavigationView broken in iOS

    13.3 ◦ ϚΠφʔόʔδϣϯʹΑͬͯڍಈ͕มΘΔ • iOS13Ͱ࢖༻Ͱ͖ΔAPI͕ݶΒΕ͍ͯΔ ◦ ࢖༻Ͱ͖ΔAPIͷΈͰ࣮૷͠Α͏ͱ͢ΔͱɺύϑΥʔϚϯεΛམͱ͢ڪΕ͕͋Δ 
 ʢLazyܥͳͲ͕࢖༻Ͱ͖ͳ͍ͨΊʣ ◦ ࣮૷ʹ޻෉͕ඞཁʢUIKitΛϥοϓͯ͠࢖༻͢ΔͳͲʣ ɹɹɹ˞ UIKitͱൺ΂ͯSwiftUI͸ػೳ͕ෆ଍͍ͯ͠Δ
  43. 43 iOS13 iOS14+ iOSόʔδϣϯʹΑͬͯग़͠෼͚͍ͯΔ

  44. 44 SwiftUIͰߏஙՄೳͳը໘ͳͷ͔ʁ • σβΠϯ௨Γͷը໘Λ࡞੒Ͱ͖Δͷ͔ʁ ◦ ͋Δఔ౓࣮૷Πϝʔδ͸Ͱ͖ͨ • ϗʔϜը໘ͷ࣮૷ͷͨΊɺͳΔ΂͘ϦεΫΛආ͚࣮ͨ૷͕Ͱ͖Δ͔ʁ ◦ SwiftUI͸͋͘·ͰViewͱͯ͠׆༻͢Δ

    ◦ iOS14Ҏ্Ͱ৽͍͠ϗʔϜը໘Λදࣔ͢Δ ཁ݅Λຬ࣮ͨͨ͠૷͕Մೳ
  45. 45 3. ϝϦοτ͕͋Δ͔ʁ

  46. 46 ϝϦοτ͕͋Δͷ͔ʁ • কདྷతʹΩϟονΞοϓ͕ඞཁͳٕज़ͷݕূɾ࣮ྫ͕Ͱ͖Δ • UIߏஙͷ։ൃޮ཰͕޲্͢ΔݟࠐΈ͕͋Δ ◦ ຊ࣮૷͢Δલʹײ৮Λ֬ೝ

  47. 47 • એݴܕγϯλοΫεͰ௚ײతͳίʔσΟϯά͕Ͱ͖Δ ◦ σβΠφʔͳͲͱ໾ׂ෼୲ͳͲՄೳ • Ϗϧυճ਺ΛݮΒ͢͜ͱͰ޻਺ɾετϨε࡟ݮ ͜Ε·Ͱͷ σόοάखॱ ։ൃޮ཰͕޲্͢ΔݟࠐΈ͕͋Δ

    UIߏங Ϗϧυ ֬ೝ͍ͨ͠ ը໘΁ભҠ ֬ೝ UIߏங Ϗϧυ ֬ೝ ϓϨϏϡʔͷ σόοάखॱ ͜ͷ෦෼ʹ࣌ؒΛཁ͍ͯͨ͠
  48. 48 ϝϦοτ͕͋Δͷ͔ʁ • কདྷతʹΩϟονΞοϓ͕ඞཁͳٕज़ͷݕূɾ࣮ྫ͕Ͱ͖Δ • UIߏஙͷ։ൃޮ཰͕޲্͢ΔݟࠐΈ͕͋Δ ◦ ௚ײతͰ࣮૷͠΍͍͢ ◦ σβΠφʔͱ໾ׂ෼୲͠΍͘͢ͳΔ

    ◦ σόοάָ͕ ಋೖΛਪͤΔϝϦοτ͕͋Δ
  49. 49 Ҏ্ɺݕ౼ͷ݁Ռ

  50. 50 SwiftUIಋೖલͷݕ౼݁Ռ 1. αϙʔτΛiOS13.0Ҏ্ʹͰ͖Δ͔ʁ ◦ શମతɾεϖʔεϚʔέοτΞϓϦͷར༻ঢ়گΛؑΈͯɺ 
 iOS13.0ʹ͋͛Δ͜ͱ͕Մೳ 2. SwiftUIͰߏஙՄೳͳը໘ͳͷ͔ʁ

    ◦ ࢖༻͢ΔAPIͳͲ֬ೝΛߦ͍ɺ࣮૷ͷ૝ఆ͕Ͱ͖ͨ ◦ UIKitͱซ༻࣮ͨ͠૷Λߦ͏͜ͱͰɺͰ͖Δ͚ͩϦεΫΛԼ࣮͛ͨ૷͕Ͱ͖Δ ◦ ৽͍͠ϗʔϜը໘͸iOS14Ҏ্ͰରԠ͢ΔΑ͏ʹ͢Δ 3. ϝϦοτ͕͋Δ͔ʁ ◦ UIߏஙͷ࡞ۀޮ཰޲্͕ݟࠐΊΔ ◦ σβΠφʔͱͷ࡞ۀ෼୲΋Ͱ͖ͦ͏ ◦ কདྷతʹΩϟονΞοϓ͕ඞཁͳٕज़ͷݕূɾ࣮༻ྫ͕Ͱ͖Δ OK OK OK
  51. 51 SwiftUIಋೖલͷݕ౼݁Ռ 1. αϙʔτΛiOS13.0Ҏ্ʹͰ͖Δ͔ʁ ◦ શମతɾεϖʔεϚʔέοτΞϓϦͷར༻ঢ়گΛؑΈͯɺ 
 iOS13.0ʹ͋͛Δ͜ͱ͕Մೳ 2. SwiftUIͰߏஙՄೳͳը໘ͳͷ͔ʁ

    ◦ ࢖༻͢ΔAPIͳͲ֬ೝΛߦ͍ɺ࣮૷ͷ૝ఆ͕Ͱ͖ͨ ◦ UIKitͱซ༻࣮ͨ͠૷Λߦ͏͜ͱͰɺͰ͖Δ͚ͩϦεΫΛԼ࣮͛ͨ૷͕Ͱ͖Δ ◦ ৽͍͠ϗʔϜը໘͸iOS14Ҏ্ͰରԠ͢ΔΑ͏ʹ͢Δ 3. ϝϦοτ͕͋Δ͔ʁ ◦ UIߏஙͷ࡞ۀޮ཰޲্͕ݟࠐΊΔ ◦ σβΠφʔͱͷ࡞ۀ෼୲΋Ͱ͖ͦ͏ ◦ কདྷతʹΩϟονΞοϓ͕ඞཁͳٕज़ͷݕূɾ࣮༻ྫ͕Ͱ͖Δ OK OK OK SwiftUIಋೖ΁ࢸΓɺ ແࣄෆ۩߹΋ͳ࣮͘૷Λߦ͏͜ͱ͕ Ͱ͖·ͨ͠🙌
  52. 52 ·ͱΊ

  53. 53 ·ͱΊ • SwiftUIͷಋೖʹ͍ͭͯ ◦ ϝϦοτ ▪ UIKitͱ૊Έ߹ΘͤͯॊೈʹରԠͰ͖Δ ▪ UIߏஙʹؔͯ͠࡞ۀޮ཰UP͕ݟࠐΊΔ

    ▪ কདྷతʹΩϟονΞοϓ͕ඞཁͳٕज़ͷݕূɾ࣮ྫ͕Ͱ͖Δ ◦ σϝϦοτ ▪ UIKitͱൺ΂ͯ଍Γͳ͍ػೳ͕ଟʑ͋Δ ▪ iOS13Ͱͷ࣮૷͸޻෉͕ඞཁ 
 ʢiOS14αϙʔτޙʹಋೖͨ͠ํ͕҆ఆ͢Δͱࢥ͍·͢ʣ
  54. 54 ·ͱΊ • ͜Ε͔Β ◦ αϙʔτόʔδϣϯͷϝϯςφϯεΛܧଓతʹߦ͏͜ͱ͕େࣄ ▪ SwiftUI࢖༻͢Δɾ͠ͳ͍ʹؔΘΒͣɺศརͳAPI͕࢖༻Ͱ͖Δ ◦ WWDC21Ͱ৽ͨʹ࢖༻Ͱ͖ΔAPI͕ެ։͞Εɺࠓޙ΋࢖͍উख͕ྑ͘ͳΔ

    ▪ ࠷৽ٕज़΁ͷΩϟονΞοϓͯ͠ɺΑΓྑ͍ϓϩμΫτΞοϓσʔτ΁ʂ • ΑΓৄ͍͠಺༰͸ͪ͜Β ◦ SwiftUIͰ࣮૷ͨ͠৽͍͠ϗʔϜը໘ΛϦϦʔε͠·ͨ͠ ◦ SwiftUIͰΧϧʔηϧΛ࣮૷͢Δ
  55. None