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

Serhii Bykov: macOS development from iOS develo...

Serhii Bykov: macOS development from iOS developer’s perspective

Serhii Bykov shares his experience about switching from iOS development to macOS. Is there a lot of differences? What is similar? How did it feel, did it require much time and effort?

MacPaw Tech Talks

December 12, 2019
Tweet

More Decks by MacPaw Tech Talks

Other Decks in Programming

Transcript

  1. Who am I • 5+ years of iOS Development •

    ≈ 10 different projects • 6 months of macOS Development 4
  2. Why did I switch to macOS • All my iOS

    projects were not so big • Most of the time I was the only one iOS developer in a team • I thought there were more challenging tasks in macOS development 5
  3. ❓ Why am I giving this talk • Cocoa community

    is big and it keeps growing • 90-95% people of Cocoa community are iOS developers • I am still struggling while developing for macOS • So I’ve decided to provide an easy ramp-up for iOS developers to start developing for macOS 6
  4. ♊ What’s similar - Frameworks • Foundation • Core Animation

    • Core Graphics • Core Text • Core Data • libdispatch • etc. 11
  5. ♊ What’s similar - UI frameworks • UIKit - lightweight

    version of AppKit • UIKit & AppKit are sharing a lot of concepts 12
  6. ♊ What’s similar - UI frameworks - Class names 13

    UIKit AppKit UIWindow NSWindow UIViewController NSViewController UIView NSView UIResponder NSResponder
  7. 14 Isn’t it enough to replace UI with NS in

    your iOS app to get macOS app?
  8. 15

  9. UIWindow vs NSWindow • UIWindow - cannot be modified, since

    it takes up the whole screen • NSWindow - can be resized, zoomed, minimised, hidden, closed, etc. 19
  10. Views • View system works very differently on the Mac

    • On iOS, views were backed by Core Animation layers by default • AppKit integrated Core Animation into views much later 23
  11. Layer-Backed Views • A layer-backed view is a view that

    is backed by a Core Animation layer • Any drawing done by the view is cached to the underlying layer object • You should never interact directly with the layer 25
  12. Modifying AppKit layer class ColoredView: NSView { override init(frame frameRect:

    NSRect) { super.init(frame: frameRect) wantsLayer = true } var backgroundColor: NSColor! { didSet { needsDisplay = true } } override var wantsUpdateLayer: Bool { return true } override func updateLayer() { layer?.backgroundColor = backgroundColor?.cgColor ?? nil } } 28
  13. Layer-Hosting View • A layer-hosting view is a view that

    contains a Core Animation layer that you intend to manipulate directly • You are responsible for managing the view’s layer • Do not rely on the view for drawing • Do not add subviews 29
  14. Layer-Hosting View - Example class LayerHostingView: NSView { override init(frame

    frameRect: NSRect) { super.init(frame: frameRect) layer = CALayer() wantsLayer = true } } 30
  15. Layer-Hosting View - Why 31 • Why would one create

    a layer-hosting view? • When you need some advanced operations with layer or when you want to configure a hierarchy of layers associated with a single view
  16. Views - Different Kinds - Recap 32 • Layer-Backed Views

    • Basic operations with views • Layer-Hosting Views • Advanced operations that need some manipulations with layer
  17. Receiving events from mouse class MouseTrackingView: NSView { override func

    mouseMoved(with event: NSEvent) { // this method won't be called } } 37
  18. Receiving events from mouse class MouseTrackingView: NSView { private var

    trackingArea: NSTrackingArea? override func mouseMoved(with event: NSEvent) { // now this method will be called } override func updateTrackingAreas() { if let area = trackingArea { removeTrackingArea(area) } let options: NSTrackingArea.Options = [.mouseMoved, .activeAlways] let area = NSTrackingArea(rect: bounds, options: options, owner: self, userInfo: nil) addTrackingArea(area) self.trackingArea = area } } 38
  19. Label • In AppKit there’s no such class as NSLabel

    39 let label = NSTextField() label.drawsBackground = false label.isBezeled = false label.isEditable = false label.isSelectable = false
  20. NSCell • AppKit controls have been traditionally backed by NSCell

    subclasses • NSCell ≠ NSTableCellView ≠ NSCollectionCellView • Apple is deprecating this approach 40
  21. Collections - Common • No scroll view by default •

    You had to install your NSTableView or NSCollectionView to NSScrollView via documentView property 43
  22. NSTableView - Different cell classes • NSTableView can present the

    following objects • NSCell subclasses • NSTableCellView subclasses 46
  23. NSCell-based vs NSView-based 49 • NSCell-based - when you don’t

    need more than 1 control in cell • NSView-based - all other cases
  24. NSImage - Key Differences • NSImage can be backed by

    one or more image representations • NSImageRep - representations for BMP, EPS, PDF, etc • NSImage will cache the result when it’s drawn to the screen 52
  25. Colors • macOS supports fully color-calibrated workflows, so anything having

    to do with colors is potentially more complicated • A common task is to use a color in your app that your designers have specified for you • Make sure you’re using same color space for Xcode & <your_design_app> 54
  26. Sandboxing - Limitations A sandboxed app must explicitly state its

    intent to use any of the following resources 58 • Hardware (, , ) • Network Connections (⬆, ⬇) • App Data (, , ) • User Files (, , , , )
  27. Sandboxing - Mac App Store • Apps distributed through the

    Mac App Store must adopt App Sandbox. • Apps signed and distributed outside of the Mac App Store with Developer ID can use App Sandbox as well. 60
  28. 62

  29. Permissions - Pre-Catalina • Calendar, Reminders - 10.9 • Contacts

    - 10.11 • Photos - 10.13 • Full Disk Access - 10.14 63
  30. Permissions - Catalina • Camera, Microphone , • Screen Recording

    • Keyboard Input Monitoring ⌨ • User Folders • Accessibility 64
  31. Cocoa Bindings - What are they • Cocoa bindings technology

    provides a way of keeping model and view values synchronised 95
  32. Bindings - Basic Example • Text Field text content changes

    string variable • String variable changes Text Field text content 96
  33. Bindings - NSController & subclasses The other key part of

    Cocoa Bindings is the abstract class NSController and its 3 subclasses • NSObjectController • NSArrayController • NSTreeController 100
  34. Bindings - NSTableView • NSArrayController w/ NSTableView simplifies following operations:

    • Add, remove, edit • Sorting, filtering • Manage selection • etc. 101
  35. Bindings - PONSO Model class Speaker : NSObject { @objc

    dynamic var firstName : String = "" @objc dynamic var lastName : String = "" init(firstName: String, lastName: String) { super.init() self.firstName = firstName self.lastName = lastName } } 102
  36. NSViewController - Sample Data class ViewController: NSViewController { @objc dynamic

    var speakers: [Speaker] = [ Speaker(firstName: "Serhii", lastName: "Bykov"), Speaker(firstName: "Sergey", lastName: "Krivoblotsky"), Speaker(firstName: "Bohdan", lastName: "Mihiliev"), ] } 103
  37. NSWorkspace A workspace that can launch other apps and perform

    a variety of file- handling services • File operations • System operations 113
  38. NSWorkspace - Files Operations • Open files & URLs •

    Duplicating files ♊ • Moving files to the trash • Revealing a file location in Finder • Getting icons for files 114
  39. NSWorkspace - System Operations • Launching an application ⬇ •

    Hiding all other applications • Getting a list of all running apps or the frontmost app • Get or modify the Desktop picture • etc. 115
  40. NSRunningApplication • Getting running application instances • Activate, hide &

    unhide applications ✒ • Getting application info ℹ • Terminating applications ☠ 116
  41. NSTask / Process • NSTask run another program as a

    process • Apple API are feature-rich, but sometimes all you need is some command line tool • Some apps are just GUI shells for command line tools (e.g. any git clients) 117
  42. More and more • Documents • Background processes • Cross-process

    communication • AppleScript • Plugin Infrastructure • etc. 118
  43. Catalyst - Cons • New framework • You will still

    need to write a platform-specific code • macOS 10.15+ 121
  44. iOS Adoption Rate • iOS 13 - 81.5% • iOS

    12 - 14.2% • Other - 4.3% Other 4% iOS 12 14% iOS 13 82% Data: https://www.idownloadblog.com/2019/10/17/ios-13-adoption-rate-fifty-percent/ 122
  45. macOS Adoption Rate • Mojave - 33.49% • Catalina -

    27.12% • High Sierra - 17.11% • Sierra - 8.2% • Other - 14.1% Data: https://gs.statcounter.com/macos-version-market-share/desktop/worldwide/#monthly-201911-201911-bar Other 14% Sierra 8% High Sierra 17% Catalina 27% Mojave 33% 123
  46. Catalyst - Wrap Up • Catalyst can wait till it

    becomes stable and more users will update to Catalina • It is still worth trying since it’s a good way to try a lil bit of desktop development 124
  47. Conclusions • All missing API’s will be probably covered by

    code written by your colleagues • Despite all the differences between platforms, your daily development process will look pretty close to your iOS tasks, if you’re not doing something extraordinary • Setapp will soon have iOS application, so feel free to apply to us • https://macpaw.com/careers/macos-setapp 126
  48. Resources • https://www.objc.io/issues/14-mac/ • https://nshipster.com/ • https://pilky.me • https://www.highcaffeinecontent.com/blog/ •

    https://onmyway133.github.io/tags/macOS/ • http://www.cocoawithlove.com • https://eclecticlight.co 127