At WWDC 2020, Apple introduced a bunch of great new updates to SwiftUIto make it even easier for developers to write apps for Apple platforms. In this tutorial, you’ll learn how to use those new features to make your app work on both iOS and macOS. By the end of this tutorial you will have created a fully functional HackerNews reader.

You can download the source code on my GitHub.

This tutorial requires Xcode 12, macOS 11.0 Big Sur, and iOS 14. See this guide on how to install the betas on a separate partition.

Image for post

Setting up a new Xcode project

Open Xcode 12 or newer and hit ⌘⇧N to create a new project. Navigate to the new “Multiplatform” section, select App and press Next. Then save your project as usual.

Image for post

The new multiplatform section in Xcode 12

Starter files

You can download the starter files for this project from GitHub. These files define interfaces to work with the HackerNews API and lay the groundwork for the MVVM design pattern, which you will use throughout the app. The next section explains the API and MVVM in more detail.

From the file navigator, create groups named Models and ViewModels in the Shared section. This section contains all code used by both the iOS and macOS apps. Drag the starter files from Finder into the correct group in Xcode. Don’t forget to check “Copy items if needed” and select both platform targets. You can place HackerNews.swift directly in the Sharedsection. After you’re done, your file navigator should look as follows:

Image for post

Allowing network connections on macOS

On macOS, you need to explicitly allow network usage. Select the project file, navigate to the macOS target, select the Signing & Capabilities tab, then check Outgoing Connections (Client).

Image for post

#macos #swiftui #heartbeat #ios #ios-app-development

Building a Multi-platform App with SwiftUI
32.85 GEEK