Chapters

Hide chapters

SwiftUI Cookbook

Live Edition · iOS 16.4 · Swift 5.8.1 · Xcode 14.3.1

Import Platform-Specific Frameworks Using Conditional Compilation
Written by Team Kodeco

In developing SwiftUI apps for multiple platforms, there may be instances where you’ll want to use a platform-specific framework. However, attempting to import a framework that’s not supported by the target platform will cause a build failure. To navigate around this issue, you can use conditional compilation.

One common scenario is attempting to import Apple’s ARKit, which is only available on iOS and iPadOS. If you try to import ARKit in a SwiftUI view that targets macOS, it would cause a build error since ARKit isn’t supported on macOS.

Thankfully, Swift provides a canImport() conditional compilation block to handle these cases. If the framework can be imported on the target platform, the code inside the canImport() block will be compiled. If not, the compiler will ignore it.

Here’s how to use canImport() with ARKit in SwiftUI:

#if canImport(ARKit)
import ARKit

struct ContentView: View {
  var body: some View {
    Text("ARKit is available!")
      .font(.largeTitle)
      .padding()
      .foregroundColor(.green)
  }
}

#else

struct ContentView: View {
  var body: some View {
    Text("ARKit is not available!")
      .font(.largeTitle)
      .padding()
      .foregroundColor(.red)
  }
}
#endif

Here’s what your preview should look like on an iPhone:

ARKit is available on iOS.
ARKit is available on iOS.

And here’s what it should look like on a Mac:

ARKit is not available on macOS.
ARKit is not available on macOS.

In the example above, we have two ARKitView structs. The first one is defined if ARKit can be imported, displaying a message that ARKit is available. The second one, defined otherwise, displays the message that ARKit is not available. This provides a way to handle the UI for platforms where certain frameworks are not available.

When you run a preview in Xcode, the message displayed will depend on whether ARKit can be imported. ARKit requires an iOS device and is not available on macOS. Thus, when previewing on a Mac, you should see “ARKit is not available!”.

Building a File on only a specific platform

While developing multiplatform applications using SwiftUI, you might encounter scenarios where certain source files only apply to specific platforms due to the use of platform-specific APIs or frameworks. In such cases, you may not only need to conditionally import frameworks but also exclude entire source files from non-applicable platforms to prevent build-time issues.

As an example, consider a scenario where you have a source file that extensively uses ARKit to provide an AR experience. This file is applicable only for iOS and iPadOS, as ARKit isn’t available on macOS or tvOS. You can manage such platform-specific files directly from Xcode.

To exclude a file from a certain platform:

  1. Navigate to your target in Xcode.
  2. Select the Build Phases tab in the project editor.
  3. Find Compile Sources and click the disclosure triangle to see the list of files.
  4. Identify the specific source file from the list.
  5. In the Filters column, click on Always Used, then uncheck Allow any platform. Then, select the platforms for which this file should be compiled.

Here’s what Xcode should look like:

Excluding a file from macOS.
Excluding a file from macOS.

In summary, managing platform-specific source files and using conditional compilation directives can significantly streamline the development process for multiplatform SwiftUI apps. By doing so, you can leverage platform-specific features while maintaining a clean and organized codebase that successfully builds across multiple platforms.

Have a technical question? Want to report a bug? You can ask questions and report bugs to the book authors in our official book forum here.
© 2024 Kodeco Inc.