Swift Package Manager - UIKit Dependency

13,890

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:

Demo

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

enter image description here

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...

Share:
13,890

Related videos on Youtube

Onato
Author by

Onato

Updated on June 04, 2022

Comments

  • Onato
    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
    Onato over 8 years
    Can you point to documentation that Swift Package Manager is only meant for MacOS and Linux?
  • GoZoner
    GoZoner over 8 years
    Well, 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
    Onato over 8 years
    I was expecting it to build a framework like Carthage does.
  • Rick Ballard
    Rick Ballard about 8 years
    The 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
    GoZoner about 8 years
    Nice! Thanks for that.
  • Winter
    Winter about 7 years
    Depending 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 -
    Ky - over 4 years
  • GoZoner
    GoZoner over 4 years
    This is what happens over time. No? It clearly has numerous dates. Worth a downvote? Really?
  • spentag
    spentag about 4 years
    I am having the original issue, even when defining the platform.
  • spentag
    spentag almost 4 years
    Unfortunately no- I had to use git submodules for my in-house framework. :(
  • edbentley
    edbentley almost 4 years
    After defining the platform, make sure you have an iPhone simulator selected at the top when you build.
  • blackjacx
    blackjacx over 3 years
    This 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
    Skwiggs over 3 years
    OP needs a solution for a swift build error, not conditional imports...
  • Anton Plebanovich
    Anton Plebanovich about 3 years
    For the tvOS: swift build -Xswiftc "-sdk" -Xswiftc "`xcrun --sdk appletvsimulator --show-sdk-path`" -Xswiftc "-target" -Xswiftc "x86_64-apple-tvos14.3-simulator"
  • mgyky
    mgyky over 2 years
    Thats the answer!
  • user3773048
    user3773048 over 2 years
    How 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
    user3773048 over 2 years
    This 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
    Rob about 2 years
    LMAO I spent like 1h without realizing I was trying to build for macOS... ridiculous
  • AdamPro13
    AdamPro13 almost 2 years
    I'm trying to build for watchOS and macCatalyst. I've figured out I can use watchsimulator and macosx in place of iphonesimulator, but I can't figure out the list of available -target values. How did you find x86_64-apple-ios13.0-simulator?
  • Stoyan
    Stoyan almost 2 years
    actually, 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