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

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

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

Takashico

July 05, 2021
Tweet

More Decks by Takashico

Other Decks in Programming

Transcript

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

    ٳΈͷ೔͸ɺೣͱ༡Ϳ ɻ @takashico @takashico smtakahashi
  2. 6 SwiftUIͱ͸ • ৽͍͠UIϑϨʔϜϫʔΫʢWWDC19ʣ ◦ গͳ͍ίʔυͰUIߏங͕Մೳ ◦ એݴܕγϯλοΫεʢγϯϓϧʣ ◦ ϓϨϏϡʔػೳʢσόοάޮ཰UPʣ

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

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

    ◦ etc… • αϙʔτΛiOS13.0Ҏ্ʹ͢Δඞཁ͕͋Δ SwiftUI ΑΓ༏ΕͨAppΛɺΑΓগͳ͍ίʔυͰɻ ࢀর: https://developer.apple.com/jp/xcode/swiftui ͜ͷ੍໿ʹΑͬͯಋೖ͕೉͔ͬͨ͠ εϖʔεϚʔέοτΞϓϦʹ ͍͔ͭಋೖ͍ͨ͠ͳͱߟ͍͑ͯ·ͨ͠🤔 ʢಋೖલ·Ͱ͸iOS11.4·Ͱαϙʔτͯ͠ ͍·ͨ͠ʣ
  5. 30 σβΠϯ௨Γͷը໘Λ࡞੒Ͱ͖Δͷ͔ʁ • ListʢchildrenʣΛ࢖༻͢Δํ๏ • DisclosureGroupΛ࢖༻͢Δํ๏ ͜ΕΒͷ࣮૷ํ๏Ͱ࣮ݱͰ͖Δ͜ͱΛ֬ೝ // isExpanded: ల։ϑϥά

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

    DisclosureGroup( isExpanded: $isExpanded, content: { // ࢠΧςΰϦCell }, label: { // ਌ΧςΰϦCell } ) // categories: ΧςΰϦʔͷ഑ྻ List(categories, children: \.children) { item in // ࢠΧςΰϦCell } ଞͷը໘΋࣮ݱՄೳͰ͋Δ͜ͱΛ֬ೝ͠ ·ͨ͠ɻ ※ ຊ࣮૷ͯ͠Έͳ͍ͱΘ͔Βͳ͍఺΋ଟ͍ͷ Ͱɺ͋͘·Ͱ΋βοΫϦͱ֬ೝ͠·ͨ͠ɻ
  7. 34 UIKitͱซ༻࣮ͨ͠૷ • SwiftUIͰ࡞੒ͨ͠ը໘ΛViewͱͯ͠ѻ͏͜ ͱͰॊೈʹରԠ ◦ ΋͠໰୊͕ൃੜͨ͠৔߹ʹ͸ɺ෦෼త ʹ࢖༻͢Δ͜ͱͳͲॊೈʹରԠՄೳ • ίϯτϩʔϥʔ෦ͷෆ۩߹͸க໋తͳӨڹΛ

    ༩͑ΔڪΕ͕͋ΔͨΊɺViewControllerʹ੹ ຿Λ࣋ͨͤΔߏ଄Ͱ࣮૷ • UIHostingControllerΛ࢖ͬͯUIKitଆ͔Β SwiftUIͰ࡞੒ͨ͠ΦϒδΣΫτΛੜ੒Ͱ͖Δ
  8. 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) ])
  9. 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
  10. 39 DataSource /// ObservableObjectϓϩτϩϧΛܧঝͨ͠ΫϥεͰఆٛ͢Δ class DataSource: ObservableObject { @Published var

    banners: [Banner] = [] } // ஋Λߋ৽͢Δ BannerView.DataSorce.banners = ["όφʔ৘ใ"] σʔλΛऔಘ dataSourceʢObservedObjectʣ ͷ஋Λ؂ࢹ͍ͯ͠ΔͨΊɺ ஋ʢbannersʣ͕ߋ৽͞Εͨ৔߹͸ Viewͷ࠶ඳը͕ߦΘΕΔ ᶃ ᶄ ᶅ dataSourceʢObservedObjectʣ ʹऔಘͨ͠σʔλΛ౉͢
  11. 42 iOS13ಛ༗ͷ໰୊Λճආ • ಈ࡞͕ෆ҆ఆͱ͍͏ใࠂ͕͋Δ ◦ SwiftUI NavigationView broken in iOS

    13.3 ◦ ϚΠφʔόʔδϣϯʹΑͬͯڍಈ͕มΘΔ • iOS13Ͱ࢖༻Ͱ͖ΔAPI͕ݶΒΕ͍ͯΔ ◦ ࢖༻Ͱ͖ΔAPIͷΈͰ࣮૷͠Α͏ͱ͢ΔͱɺύϑΥʔϚϯεΛམͱ͢ڪΕ͕͋Δ 
 ʢLazyܥͳͲ͕࢖༻Ͱ͖ͳ͍ͨΊʣ ◦ ࣮૷ʹ޻෉͕ඞཁʢUIKitΛϥοϓͯ͠࢖༻͢ΔͳͲʣ ɹɹɹ˞ UIKitͱൺ΂ͯSwiftUI͸ػೳ͕ෆ଍͍ͯ͠Δ
  12. 50 SwiftUIಋೖલͷݕ౼݁Ռ 1. αϙʔτΛiOS13.0Ҏ্ʹͰ͖Δ͔ʁ ◦ શମతɾεϖʔεϚʔέοτΞϓϦͷར༻ঢ়گΛؑΈͯɺ 
 iOS13.0ʹ͋͛Δ͜ͱ͕Մೳ 2. SwiftUIͰߏஙՄೳͳը໘ͳͷ͔ʁ

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

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

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

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