Porting Your iOS App to macOS

Learn how to port iOS apps to macOS. If you’re developing apps for iOS, you already have skills that you can use to write apps for macOS! By Andy Pereira.

Leave a rating/review
Save for later
Share
You are currently viewing page 4 of 4 of this article. Click here to view the first page.

Final Touches

You’ve learned a lot about the similarities and differences between iOS and macOS development. There’s another concept that you should familiarize yourself with: Settings/Preferences. In iOS, you should be comfortable with the concept of going into Settings, finding your desired app, and changing any settings available to you. In macOS, this can be accomplished inside your app through Preferences.

Build and run the BeerTracker target, and in the simulator, navigate to the BeerTracker settings in the Settings app. There you’ll find a setting allowing your users to limit the length of their notes, just in case they get a little chatty after having a few.

Settings - iOS

In order to get the same feature in your mac app, you’ll create a Preferences window for the user. In BeerTracker-mac, open Main.storyboard, and drop in a new Window Controller. Select the Window, open the Size Inspector, and change the following:

  1. Set Content Size width to 380, and height to 55.
  2. Check Minimum Content Size, and set width to 380, and height to 55.
  3. Check Maximum Content Size, and set width to 380, and height to 55.
  4. Check Full Screen Minimum Content Size, and set width to 380, and height to 55.
  5. Check Full Screen Maximum Content Size, and set width to 380, and height to 55.
  6. Under Initial Position, select Center Horizontally and Center Vertically.

Next, select the View of the empty View Controller, and change the size to match the above settings, 380 x 55.

Doing these things will ensure your Preferences window is always the same size, and opens in a logical place to the user. When you’re finished, your new window should look like this in the storyboard:

Preferences - Mac

At this point, there is no way for a user to open your new window. Since it should be tied to the Preferences menu item, find the menu bar scene in storyboard. It will be easier if you drag it close to the Preferences window for this next part. Once it is close enough, do the following:

  1. In the menu bar in storyboard, click BeerTracker-mac to open the menu.
  2. Control-drag from the Preferences menu item to the new Window Controller
  3. Select Show from the dialog

Find a Check Box Button, and add it to the empty View Controller. Change the text to be Restrict Note Length to 1,024 Characters.

With the Checkbox Button selected, open the Bindings Inspector, and do the following:

  1. Expand Value, and check Bind to.
  2. Select Shared User Defaults Controller from the drop down.
  3. In Model Key Path, put BT_Restrict_Note_Length.

Create a new Swift file in the Utilities group named StringValidator.swift. Make sure to check both targets for this file.

Open StringValidator.swift, and replace the contents with the following code:

import Foundation

extension String {
  private static let noteLimit = 1024

  func isValidLength() -> Bool {
    let limitLength = UserDefaults.standard.bool(forKey: "BT_Restrict_Note_Length")
    if limitLength {
      return self.characters.count <= String.noteLimit
    }
    return true
  }
}

This class will provide both targets with the ability to check if a string is a valid length, but only if the user default BT_Restrict_Note_Length is true.

In ViewController.swift add the following code at the bottom:

extension ViewController: NSTextViewDelegate {
  func textView(_ textView: NSTextView, shouldChangeTextIn affectedCharRange: NSRange, replacementString: String?) -> Bool {
    guard let replacementString = replacementString else { return true }
    let currentText = textView.string
    let proposed = (currentText as NSString).replacingCharacters(in: affectedCharRange, with: replacementString)
    return proposed.isValidLength()
  }
}

Finally, change the names of each Window in Main.storyboard to match their purpose, and give the user more clarity. Select the initial Window Controller, and in the Attributes Inspector change the title to BeerTracker. Select the Window Controller for the Preferences window, and change the title to Preferences.

Build and run your app. If you select the Preferences menu item, you should now see your new Preferences window with your preferences item. Select the checkbox, and find some large amount of text to paste in. If this would make the note more 1024 characters, the Text View will not accept it, just like the iOS app.

Build and Run with Preferences

Where to Go From Here?

You can download the finished project here.

In this tutorial you learned:

  • How to utilize a current iOS project to host a macOS app.
  • When to separate code based on your platform's needs.
  • How to reuse existing code between both projects.
  • The differences between some of Xcode's behaviors between the two platforms.

For more information about porting your apps to macOS, check out Apple's Migrating from Cocoa Touch Overview.

If you have any questions or comments, please join in the forum discussion below!