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

Useful 3 techniques developing in iOS Apps

yashigani
April 18, 2015

Useful 3 techniques developing in iOS Apps

Cocoa study Kansai #61

yashigani

April 18, 2015
Tweet

More Decks by yashigani

Other Decks in Programming

Transcript

  1. ͓໾ཱͪςΫχοΫ࿈ൃ
    @yashigani
    Cocoaษڧձؔ੢ #61

    View Slide

  2. yashigani
    id:yashigani_w @yashigani
    Mobile Application Engineer
    http://yashigani.hatenablog.com
    Hatena

    View Slide

  3. એ఻

    View Slide

  4. IUUQDPOOQBTTDPNFWFOU

    View Slide

  5. IUUQDPOOQBTTDPNFWFOU

    View Slide

  6. IUUQDPOOQBTTDPN
    FWFOU

    View Slide

  7. ը໘αΠζʹ߹Θͤͯ
    6*4DSPMM7JFXΛ
    ͍͍͔Μ͡ʹ͢Δ
    @yashigani
    Cocoaษڧձؔ੢ #61

    View Slide

  8. View Slide

  9. View Slide

  10. ͜Ε4ͩͱͲ͏ͳΔΜͰ͔͢ʁ
    ͏ʔΜɼεΫϩʔϧ͠·͢

    View Slide

  11. View Slide

  12. View Slide

  13. ࠔͬͨ

    View Slide

  14. ը໘αΠζຖʹϨΠΞ΢τ
    • ߴղ૾౓୺຤
    • ը໘ϐολϦʹ޿͛ͯηϯλϦϯά͢Δ
    • ௿ղ૾౓୺຤
    • ্Լʹద੾ͳϚʔδϯΛͱͬͯεΫϩʔϧ͢Δ

    View Slide

  15. Ṝ͍ΑΔ஍ࠈ
    • UIScrollView + Auto Layout
    • ୭΋͕ϋϚΔϙΠϯτ
    • Mixed Approach or Pure Auto Layout Aploach
    • https://developer.apple.com/library/ios/
    technotes/tn2154/_index.html
    • ઈରstoryboard͚ͩͰղܾ͍ͨ͠

    View Slide

  16. ཱͪ͸͔ͩΔ2ͭͷ՝୊
    • ը໘ʹೖΓ͖Βͳ͍ͱ͖͚ͩεΫϩʔϧ͍ͨ͠
    • ্Լʹ࠷௿ݶͷϚʔδϯΛઃ͚͍͕ͨɼ༨༟͕͋Δ
    ৔߹͸ແࢹͯ͠ηϯλϦϯά͍ͨ͠

    View Slide

  17. DPOUFOUTJ[F͸TVCWJFXͷ
    ੍໿ʹΑܾͬͯ·Δ

    View Slide

  18. View Slide

  19. ηϯλϦϯάͰ͖ͨ

    View Slide

  20. িಥ੍ͨ͠໿Λ
    ௥Ճ͢Δ

    View Slide

  21. View Slide

  22. ੍໿ʹ
    QSJPSJUZΛ͚ͭΔ

    View Slide

  23. View Slide

  24. View Slide

  25. ͜͏ͯ͠
    ৺ʹฏԺ͕๚Εͨ

    View Slide

  26. 6*5BCMF7JFX%BUB4PVSDF
    ࣮૷ύλʔϯ
    @yashigani
    Cocoaษڧձؔ੢ #61

    View Slide

  27. ͳͥ෼཭͢Δͷ͔
    • ϙʔλϏϦςΟ
    • ςετ༰қੑ
    • storyboardʹஔ͚Δ
    • view controllerΛdata sourceʹґଘͤ͞Δ
    • prepareForSegueͰϓϦϛςΟϒͳσʔλΛ΍Γͱ
    Γͨ͘͠ͳ͍

    View Slide

  28. TVCTDSJQUΛ࣮૷͢Δ

    View Slide

  29. class DataSource: NSObject {
    var models: [Model] = []
    subscript(indexPath: NSIndexPath) -> Model? {
    return models.isEmpty ? nil
    : models[indexPath.row]
    }
    }
    extension DataSource: UITableViewDataSource {
    func tableView(tableView: UITableView,
    cellForRowAtIndexPath indexPath: NSIndexPath)
    -> UITableViewCell {
    switch self[indexPath] {
    case .Some(let m):
    let cell =
    tableView.dequeueReusableCellWithIdentifier("Cell",
    forIndexPath: indexPath) as! ModelCell
    cell.configure(m)
    return cell
    default: abort()
    }
    }
    func tableView(tableView: UITableView,
    numberOfRowsInSection section: Int) -> Int {
    return models.count
    }
    }

    View Slide

  30. TFDUJPOΛ௥Ճ͢Δ

    View Slide

  31. enum Section: Int {
    case SectionA = 0
    case SectionB = 1
    var title: String {
    switch self {
    case .SectionA: return "SectionA"
    case .SectionB: return "SectionB"
    default: return ""
    }
    }
    }
    class DataSource: NSObject {
    var sectionA: [Model] = []
    var sectionB: [Model] = []
    }

    View Slide

  32. class DataSource: NSObject {
    subscript(section: Int) -> [Model]? {
    if let section = Section(rawValue: section) {
    let models: [Model]
    switch section {
    case .SectionA: models = sectionA
    case .SectionB: models = sectionB
    }
    return models
    } else {
    return nil
    }
    }
    subscript(indexPath: NSIndexPath) -> Model? {
    if let models = self[indexPath.section] {
    return models.isEmpty ? nil
    : models[indexPath.row]
    } else {
    return nil
    }
    }
    }

    View Slide

  33. extension DataSource: UITableViewDataSource {
    func tableView(tableView: UITableView,
    cellForRowAtIndexPath indexPath: NSIndexPath)
    -> UITableViewCell {
    switch self[indexPath] {
    case .Some(let m):
    let cell =
    tableView.dequeueReusableCellWithIdentifier("Cell",
    forIndexPath: indexPath) as! ModelCell
    cell.configure(m)
    return cell
    default: abort()
    }
    }
    func tableView(tableView: UITableView,
    numberOfRowsInSection section: Int) -> Int {
    return self[section]?.count ?? 0
    }
    }

    View Slide

  34. ઃܭ΋Α͘ͳͬͨ͠
    ܕ҆શʹͳͬͨ

    View Slide

  35. 8,8FC7JFXΛ
    γϡοͱ࢖͏།Ұͷํ๏
    @yashigani
    Cocoaษڧձؔ੢ #61

    View Slide

  36. ͓ͳ΍ΈϙΠϯτ
    • WKUIDelegateͷ࣮૷
    • ΞϥʔτΛॲཧ͢Δ͚ͩ
    • webView(_:createWebViewWithConfiguration:forNa
    vigationAction:windowFeatures:) -> WKWebView!
    • ΋͏KVO͸ॻ͖ͨ͘ͳ͍
    • storyboard͔Β௥ՃͰ͖ͳ͍

    View Slide

  37. 8FC,JU1MVT
    IUUQTHJUIVCDPNZBTIJHBOJ8FC,JU1MVT

    View Slide

  38. 8,6*%FMFHBUF1MVT

    View Slide

  39. • alert()ͷσϑΥϧτ࣮૷
    • ຖ౓ಉ͡΋ͷΛ࣮૷͠ͳͯ͘΋͍͍
    • target=“_blank”ͳϦϯΫ΋ϩʔυͰ͖Δ
    • ΋ͪΖΜ৔߹ʹΑͬͯ͸ผͷWKWebViewʹ
    • ࠩ͠ସ͑Δ͜ͱ΋Մೳ

    View Slide

  40. lazy var UIDelegate =
    WKUIDelegatePlus(self)
    override public func viewDidLoad()
    {
    super.viewDidLoad()
    webView.UIDelegate = UIDelegate
    }

    View Slide

  41. 8FC7JFX0CTFSWFS

    View Slide

  42. • WKWebView͸KVO compliantͳproperty͕ଟ͍
    • ͔͠͠KVOͳͲॻ͖ͨ͘͸ͳ͍
    • ؔ਺ͰobserveͰ͖ΔΑ͏ʹαϙʔτ

    View Slide

  43. lazy var observer =
    WebViewObserver(self.webView)
    override public func viewDidLoad()
    {
    super.viewDidLoad()
    observer.onTitleChanged =
    { [weak self] in
    self?.title = $0 }
    observer.onProgressChanged =
    { [weak self] in
    self?.progressbar.progress = $0
    }
    }

    View Slide

  44. ;FO8FC7JFX$POUSPMMFS

    View Slide

  45. • Zen = “ષ”
    • viewͷ࠷Լ૚ʹWKWebViewΛࠩ͠ࠐΉ
    • WKWebViewૢ࡞ͷͨΊͷIBAction
    • goBack/goForward/reload/stopLoading
    • WKUIDelegatePlus, WebViewObserverΛ࣮૷
    • storyboard͔Β࢖͑Δͧʂʂʂ

    View Slide

  46. %FNP4JNQMF#SPXTFS

    View Slide

  47. ͱʹ͔͘
    ͍·͙͢࢖ͬͯ͘Ε

    View Slide

  48. ࡶஊίʔφʔ
    @yashigani
    Cocoaษڧձؔ੢ #61

    View Slide

  49. • AFNetworking͕CarthageରԠڋ൱ͬͯΔ
    • https://github.com/AFNetworking/
    AFNetworking/pull/2552
    • LlamaKit͕ResultʹऔΓࠐ·ΕΔͷͭΒ͍
    • https://github.com/LlamaKit/LlamaKit
    • https://github.com/antitypical/Result
    • Xcode 6.3࠷ߴ͗͢Δ

    View Slide

  50. ͋Γ͕ͱ͏͍͟͝·ͨ͠

    View Slide