Swift Package Manager - UIKit Dependency

SwiftSwift2Swift Package-Manager

Swift Problem Overview


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?

Swift Solutions


Solution 1 - Swift

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 2 - Swift

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 3 - Swift

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

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

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

Solution 6 - Swift

Use conditional compilation blocks:

#if canImport(UIKit)

// Code specific to platforms where UIKit is available

#endif

Source: https://developer.apple.com/documentation/xcode/creating_a_swift_package_with_xcode

Solution 7 - Swift

If your umbrella header(<module_name.h>)[About] contains

#import <UIKit/UIKit.h>

it means that you can skip specifying import UIKit in every file. But it seems that SPM doesn't support it. Also I am not a supporter of using @_exported attribute

You can write import UIKit explicitly

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionOnatoView Question on Stackoverflow
Solution 1 - SwiftAbdelAliView Answer on Stackoverflow
Solution 2 - SwiftJoe SusnickView Answer on Stackoverflow
Solution 3 - SwiftMojtaba HosseiniView Answer on Stackoverflow
Solution 4 - SwiftGoZonerView Answer on Stackoverflow
Solution 5 - SwiftScottyBladesView Answer on Stackoverflow
Solution 6 - SwiftLachtanView Answer on Stackoverflow
Solution 7 - SwiftyoAlex5View Answer on Stackoverflow