Xcode 10.2 Cannot build Swift framework for ObjC app

I use the same build script I always use to build a Swift universal binary framework, but now my Objective-C app cannot read that binary framework headers anymore (even if I build with Swift 4.2 or 5). Is this a Xcode 10.2 bug?

UNIVERSAL_OUTPUTFOLDER=${BUILD_DIR}/${CONFIGURATION}-universal

# make sure the output directory exists
mkdir -p "${UNIVERSAL_OUTPUTFOLDER}"
env > env.txt
# Step 1. Build Device and Simulator versions
xcodebuild -project "${PROJECT_NAME}.xcodeproj"  -target "${PROJECT_NAME}" ONLY_ACTIVE_ARCH=NO -configuration ${CONFIGURATION} -sdk iphoneos  BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" clean build 
xcodebuild -project "${PROJECT_NAME}.xcodeproj" -target "${PROJECT_NAME}" -configuration ${CONFIGURATION} -sdk iphonesimulator ONLY_ACTIVE_ARCH=NO BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" clean build

# Step 2. Copy the framework structure (from iphoneos build) to the universal folder
cp -R "${BUILD_DIR}/${CONFIGURATION}-iphoneos/${PROJECT_NAME}.framework" "${UNIVERSAL_OUTPUTFOLDER}/"

# Step 3. Copy Swift modules from iphonesimulator build (if it exists) to the copied framework directory
SIMULATOR_SWIFT_MODULES_DIR="${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${PROJECT_NAME}.framework/Modules/${PROJECT_NAME}.swiftmodule/."
if [ -d "${SIMULATOR_SWIFT_MODULES_DIR}" ]; then
cp -R "${SIMULATOR_SWIFT_MODULES_DIR}" "${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/Modules/${PROJECT_NAME}.swiftmodule"
fi

# Step 4. Create universal binary file using lipo and place the combined executable in the copied framework directory
lipo -create -output "${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/${PROJECT_NAME}" "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${PROJECT_NAME}.framework/${PROJECT_NAME}" "${BUILD_DIR}/${CONFIGURATION}-iphoneos/${PROJECT_NAME}.framework/${PROJECT_NAME}"

# Step 5. Convenience step to copy the framework to the project's directory
cp -R "${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework" "${PROJECT_DIR}"

# Step 6. Convenience step to open the project's directory in Finder
open "${PROJECT_DIR}"

Are you using Carthage or something similar? I believe there was a change in the header that the Swift compiler generates that some dependency managers need to update to account for.

edit: Looks like you're using lipo, I've had users of one of my frameworks complain about this too

For reference: Add functionality to merge Swift header files by DavidBrunow · Pull Request #2723 · Carthage/Carthage · GitHub

I tried the integration via Carthage/CocoaPods/manual, all doesn't work

I upgraded to Carthage 0.32 and tried carthage build also, the problem remains

I’m not sure what’s going on in this specific case, but i want to be clear that trying to build a framework that supports both device and simulator builds is not supported. That’s because iOS and the iOS Simulator are different platforms. For more background on this, see this DevForums thread.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

I built independently for 2 platforms, but I still don't know how to follow your post here as my framework requires being added to "Embedded Framework"
https://forums.developer.apple.com/message/335047#339884
Edit: also ship both binary frameworks will make it impossible to deliver via CocoaPods or Carthage

I still don't know how to follow your post here as my framework
requires being added to "Embedded Framework"

I’m sad to say that all the advice I have on that topic is encapsulated in that DevForums post.

also ship both binary frameworks

A binary framework containing Swift? That’s a bad idea in general, due to Swift’s current lack of module stability (a restriction that’s being actively worked on).

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

So Xcode team has put it in Known issues of Build System
It is due to merging header problem. I applied the fix suggested, things worked fine now

#if TARGET_OS_SIMULATOR
<contents of original iOS Simulator/Framework.framework/Framework-Swift.h>
#else
<contents of original iOS/Framework.framework/Framework-Swift.h>
#endif

https://developer.apple.com/documentation/xcode_release_notes/xcode_10_2_release_notes

@quangDecember could you please provide the script for the fix?

Please use the script below:

1 Like

hi i have the same issue
i have framework app write in swift
i have in the targets: tool (the framework) , and framework builder
i use xcode 10.2.1
i replace my script with yours (see attachment)
and i have example project with file in swift and file objc
the swift work fine
the objective c recognise the object from frame work but not the classes , function, and properties

please help

Did you try setting Always Embed Swift Standard Libraries to true?

In the framework project?

the swift work , the objective c cant acsses to the class

I don't think "Always embed" is needed with Swift 5

did you check the modulename-Swift header file and see if the function is there yet?
it works fine on my side

the script dont do it ?

work fine xcode 10.1 without swift 4.2

You can ask this link for script which works on xcode 10.2