Search code examples
swiftxcodeswift-package-manager

How do you build iOS specific packages using SwiftPM?


I am using Swift Package Manager that doesn't having a xcodeproject file associated with it and I get an error when building through he terminal. When I call the swift build command I get an error that the MacOS build failed. The package I'm building doesn't support MacOS (It uses UIKit), but only iOS. I can't figure out a way to call the command to only specify that the build is targeted for iOS. I've Google searched around with no luck. Does anybody know the correct syntax if it exists to build an SwiftPM package for iOS from the terminal?

The version of Swift I'm using is: "Apple Swift version 5.2.4 (swiftlang-1103.0.32.9 clang-1103.0.32.53)"

I have specified the platform in the Package.swift file

let package = Package(
    name: "Package",
    platforms: [.iOS(.v10), ],
    products: [
...

I have created a sample project on github https://github.com/mike011/Swift-Package-Manager-Example. When I run swift build it fails with

/git/Swift-Package-Manager-Example/Sources/Swift-Package-Manager-Example/iOSSpecificFile.swift:9:8: error: no such module 'UIKit' import UIKit ^ /git/Swift-Package-Manager-Example/Sources/Swift-Package-Manager-Example/iOSSpecificFile.swift:9:8: error: no such module 'UIKit' import UIKit ^


Solution

  • I "solved" this by wrapping all my files in #if !os(macOS) #endif blocks. So the package builds on a Mac, but it doesn't have any content.