Xcode Export Localizations for an iOS app using Swift Package Manager packages fails: No such module UIKit

I'm building an iOS app (the supported destinations in the app target are iOS, iPad and Mac - Designed for iPad). I've many SPM frameworks in order to split my codebase in different features. I want to export the localisations for the different packages and the app using Xcode but it fails. I followed the Apple guide regarding SPM localization: the packages contains a Resources folder with a folder for each language supported. I specified the platforms .iOS(.v16) in the packages.

But it seems that exporting the localisations using the Product > Export Localizations feature in Xcode compiles also the packages and app for macOS. Here is the error message:

Showing Recent Messages

/Users/axel/Developer/AppName/Packages/Helpers/Sources/Helpers/UIKit/UIImage+Extension.swift:7:14:
No such module 'UIKit'

/Users/axel/Developer/AppName/Packages/Helpers/Sources/Helpers/UIKit/UIImage+Extension.swift:7:14:
UIKit is not available when building for macOS. Consider using `#if canImport(UIKit)` to conditionally import this framework.

Is there a way to have the export feature work when building an iOS app with packages specified for iOS?

Answered by Developer Tools Engineer in 745707022

Unfortunately, this is a known limitation of exporting Swift packages for localization. In general, Swift packages aren’t able to restrict code to specific platforms (see discussion here). The platforms key in the package manifest is used for setting a minimum deployment target, not declaring if a platform is supported. So you may need to conditionalize any iOS-specific API calls with compiler directives as the error suggests:

UIKit is not available when building for macOS. Consider using #if canImport(UIKit) to conditionally import this framework

Accepted Answer

Unfortunately, this is a known limitation of exporting Swift packages for localization. In general, Swift packages aren’t able to restrict code to specific platforms (see discussion here). The platforms key in the package manifest is used for setting a minimum deployment target, not declaring if a platform is supported. So you may need to conditionalize any iOS-specific API calls with compiler directives as the error suggests:

UIKit is not available when building for macOS. Consider using #if canImport(UIKit) to conditionally import this framework

Thanks! I added the compiler directives as the error suggests and I can now export localisations. I'll have a look hat the Swift package evolutions to better understand what platforms mean.

Xcode Export Localizations for an iOS app using Swift Package Manager packages fails: No such module UIKit
 
 
Q