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

Sharing code between iOS and Mac

Sharing code between iOS and Mac

When you develop both an iOS and a Mac app, you want to share as much code as possible. While creating a shared codebase you also have to separate platform specific tasks. We started out with the Mac app and later added iOS. I will talk about how we structured our code to tackle this problem.

László Agárdi

April 26, 2018
Tweet

Other Decks in Programming

Transcript

  1. –Languages (Objective-C, Swift) –Tools (Xcode, Mac) –System frameworks – Foundation,

    CoreGraphics, CoreData, LocalAuthentication, etc. 8 iOS & Mac development have a lot in common
  2. 13 1. Add target for both platforms in Xcode 2.

    Add shared files to both targets 3. Add #if preprocessor directives 4. Implement UI & platform specific stuff 5. Done (This is OK for small projects.) Simplest Solution
  3. –Files, resources & information –One or more build targets –Targets

    “pick” the files they need from the project –Some files are shared, some are not –Include shared files in a framework! 15 Xcode project
  4. –A hierarchical directory that encapsulates shared resources –Include code and

    other resources (assets, localization) –It‘s a bundle, access it using the NSBundle class –Share code and resources! 16 Frameworks
  5. 1. Add new target 2. Add a – Cocoa Touch

    Framework (iOS) – Cocoa Framework (Mac) 3. Add files to the framework 4. Add the framework to your app 17 How to create a framework?
  6. –Shared files are added to the frameworks –Shared files are

    made independent from platform specific files –Improved structure of code 19 We have frameworks
  7. –Lots of iOS only files –Lots of Mac only files

    –Lots of shared files –The project gets cluttered –Files accidentally added to other targets 20 In the Xcode project we have
  8. 24 Xcode subproject –Add the shared project to iOS &

    Mac projects –Xcode cannot open the same project twice
  9. –Reference multiple projects –Projects can be interdependent –Add any other

    files/folders –Cannot be included as resource –File → New → Workspace… 25 Xcode workspace
  10. 30 What code to share? –Model, Business logic code –Utility

    classes –Avoid sharing: – Views, ViewControllers – Can be done with tricks, too different on the two platforms –Implement platform specific stuff outside the shared framework
  11. 33 How to share resources? –Add them to your framework

    like you would add it to your app –Access bundle of the shared framework by getting the framework for a shared class –let sharedBundle = Bundle(for: SharedClass.self)
  12. 34 How to share resources? sharedBundle.url – (forResource: String?, withExtension:

    String?) NSLocalizedString – (_ key: String, tableName: String?, bundle:Bundle, comment: String) UIImage – (named: String, in: Bundle?, compatibleWith: UITraitCollection?)
  13. 36 Summary –Add shared files to frameworks –Separate Xcode projects

    –Use Xcode workspace –Share code & resources too
  14. 38 UILabel & UITextField Separate classes – var – text:

    String? NSTextField –One class (configure) –var stringValue: String –var intValue: Int32 –var floatValue: Float –etc. (NSControl)
  15. 40 View controller lifecycle methods viewDidLoad – () viewWillAppear –

    () viewDidAppear – () etc. – Only since – macOS 10.10+