Swift Package Manager - UIKit Dependency
Solution 1
Currently Swift Package Manager has full Xcode support. I was able to get around this error by specifying in my Package.swift manifest that the platform was iOS.
let package = Package(
name: "MyPackage",
platforms: [
.iOS(.v8)
],
Then you can open the Package.swift file in Xcode and it will just work.
Solution 2
You have to change some swiftc options to build the project against proper sdk and target
swift build -Xswiftc "-sdk" -Xswiftc "`xcrun --sdk iphonesimulator --show-sdk-path`" -Xswiftc "-target" -Xswiftc "x86_64-apple-ios13.0-simulator"
Solution 3
Make it work without limit the platforms:
You should select an iOS-based target to make it available:
If you leave it selecting macOS (by default), you will get the error.
Limit to a specific platform
if you want your package to be available only for specific platforms (for example only for iOS), you should specify the platform in the package.swift
file:
let package = Package(
name: "MyLibrary",
platforms: [
.iOS(.v10)
],
products: [
,,,
Support Multiplatform
If you need your framework to be available on multiple platforms, don't forget to check the availability of the imported framework like:
#if canImport(UIKit)
import UIKit
#endif
Solution 4
The Swift Package Manager builds executables to run on OS X (or Linux); UIKit is a framework in iOS and won't be accessible.
It may be iOS, tvOS and others become accessible as Swift Package Manager evolves.
On Dec 4, 2015, at 5:39 PM, Daniel Dunbar (@apple.com) wrote:
...
Right, now we only compile for the host platform (OS X or Linux, currently). Among other things, we currently have no knowledge (or options to choose) what SDK or architecture you are targeting. We also have no mechanisms for specifying what platforms targets are compatible with in the manifest.
Solution 5
Make sure you select an iPhone as a simulator target. a Mac target is the default and that won't work...It would be awesome if Xcode could look at the manifest and choose a default simulator based on that...
Related videos on Youtube
Onato
Updated on June 04, 2022Comments
-
Onato almost 2 years
I have a Package.swift in my project like:
import PackageDescription let package = Package( name: "ProjectName", dependencies: [ .Package(url: "https://github.com/example/repo.git", majorVersion: 0) ] )
When I run
swift build
I get errors like…/project/Packages/WebViewController.swift:1:8: error: no such module 'UIKit' import UIKit ^
Where should I tell the swift package manager where to find UIKit?
-
Onato over 8 yearsCan you point to documentation that Swift Package Manager is only meant for MacOS and Linux?
-
GoZoner over 8 yearsWell, even if you could build an iOS executable, how would you ever get it onto and running on an iOS device?
swift build --help
shows no architecture option. Documentation at swift.org/package-manager/#conceptual-overview does only mention Darwin and Linux. -
Onato over 8 yearsI was expecting it to build a framework like Carthage does.
-
Rick Ballard about 8 yearsThe Swift Package Manager is not only meant for OS X and Linux; those are just the current platforms it supports. Support for other platforms is planned.
-
GoZoner about 8 yearsNice! Thanks for that.
-
Winter about 7 yearsDepending on Apple Modules: "At this time there is no explicit support for depending on UIKit, AppKit, etc, though importing these modules should work if they are present in the proper system location. We will add explicit support for system dependencies in the future. Note that at this time the Package Manager has no support for iOS, watchOS, or tvOS platforms."
-
Ky - over 4 yearsThis answer seems thoroughly outdated now, since SPM supports macOS, iOS, tvOS, watchOS, and Linux, with Windows and Android coming soon, and macOS now has UIKit.
-
GoZoner over 4 yearsThis is what happens over time. No? It clearly has numerous dates. Worth a downvote? Really?
-
spentag about 4 yearsI am having the original issue, even when defining the platform.
-
spentag almost 4 yearsUnfortunately no- I had to use git submodules for my in-house framework. :(
-
edbentley almost 4 yearsAfter defining the platform, make sure you have an iPhone simulator selected at the top when you build.
-
blackjacx over 3 yearsThis should be the accepted answer! I can confirm it works with iOS 14 using the following too:
swift build -Xswiftc "-sdk" -Xswiftc "`xcrun --sdk iphonesimulator --show-sdk-path`" -Xswiftc "-target" -Xswiftc "x86_64-apple-ios14.0-simulator"
-
Skwiggs over 3 yearsOP needs a solution for a swift build error, not conditional imports...
-
Anton Plebanovich about 3 yearsFor the tvOS:
swift build -Xswiftc "-sdk" -Xswiftc "`xcrun --sdk appletvsimulator --show-sdk-path`" -Xswiftc "-target" -Xswiftc "x86_64-apple-tvos14.3-simulator"
-
mgyky over 2 yearsThats the answer!
-
user3773048 over 2 yearsHow do you open the Package.swift in Xcode? It didn't just work when I tried opening the file in Xcode. Does the file need to be added to the project?
-
user3773048 over 2 yearsThis worked for me, but then Xcode doesn't recognize the binary inside the project that I want to use the library dependency for that was specified in the Package.swift. I think I'm missing an obvious step...?
-
Rob about 2 yearsLMAO I spent like 1h without realizing I was trying to build for macOS... ridiculous
-
AdamPro13 almost 2 yearsI'm trying to build for
watchOS
andmacCatalyst
. I've figured out I can usewatchsimulator
andmacosx
in place ofiphonesimulator
, but I can't figure out the list of available-target
values. How did you findx86_64-apple-ios13.0-simulator
? -
Stoyan almost 2 yearsactually, you can't limit for what platform the package is compiled for, but you can say the minimum version. So having
platforms: [.iOS(.v10)]
means all macOS, tvOS etc... and iOS 10 and above