简体   繁体   中英

Error importing own Swift Cocoa Touch Framework in Xcode 7

I have problems importing my own Swift Cocoa Touch Framework in an Swift app. The Framework contains a Swift File, the Header File and the Info.plist. The Project, in which I created the Framework contains also a test app as another target (Single View Application).

The testing in the Test App worked fine. I imported the Framework in my Swift Test App with

import MyUtilities

and it worked. But when I copy the .framework file to another Projekt and add it to "Embedded Binaries" and "Linked Frameworks and Libraries", I get an error when importing it.

Cannot load underlying module for 'MyUtilities'

I use the .framework file in the debug-iphoneos folder after building the framework with my iPhone selected and also build and run my framework with my iPhone selected in the new project.

I also tried it with a script from the internet, which should create a universal.framework file for both, simulator and device. It showed the same error.

The weird thing is, that I get the error above, but Xcode says Build Succeeded and also runs the app on my iPhone and lets me use code of my framework... So practically the import works, but I don't get completition and cannot see the documentation of my framework... Probably it's a bug of Xcode...

So to my question: Does anybody knows, how to fix that, so I see code completition in Xcode again and get rid of this error?

Here is the script, I downloaded to export the framework for universal use:

#CONFIG=${CONFIGURATION}
CONFIG=Release

# Step 1. Make sure the output directory exists
UNIVERSAL_OUTPUTFOLDER=${BUILD_DIR}/${CONFIG}-universal
mkdir -p "${UNIVERSAL_OUTPUTFOLDER}"

# Step 2. Build Device and Simulator versions
xcodebuild -target "${PROJECT_NAME}" ONLY_ACTIVE_ARCH=NO -configuration ${CONFIG} -sdk iphoneos  BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" clean build

xcodebuild -target "${PROJECT_NAME}" ONLY_ACTIVE_ARCH=NO -configuration ${CONFIG} -sdk iphonesimulator -arch x86_64 -arch i386 BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" clean build

# Step 3. Copy the framework structure to the universal folder. Subsequently copy the files of the swiftmodule (in Modules directory) of -iphonesimulator to the swiftmodule of universal framework directory.

cp -R "${BUILD_DIR}/${CONFIG}-iphoneos/${PROJECT_NAME}.framework" "${UNIVERSAL_OUTPUTFOLDER}/"

cp -R "${BUILD_DIR}/${CONFIG}-iphonesimulator/${PROJECT_NAME}.framework/Modules/${PROJECT_NAME}.swiftmodule/" "${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/Modules/${PROJECT_NAME}.swiftmodule"

# 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}/${CONFIG}-iphonesimulator/${PROJECT_NAME}.framework/${PROJECT_NAME}" "${BUILD_DIR}/${CONFIG}-iphoneos/${PROJECT_NAME}.framework/${PROJECT_NAME}"

Here are a few screenshots of my Project in which I coded the framework:

The script, which exports the universal framework:

The dependencies of the test app:

No error, when I use my framework in the test app:

And from the new project, in which I tried to import the framework:

The framework is correctly linked:

The error when trying to import:

The Output message when running the code from my framework:

I think the problem is that you are not enabling access to the header files of the framework. I just struggled with this issue for quite a while. In Build Settings, I think you need to refer to the framework. If you are building your framework in your project that is using the framework, this should work:

Now for the coup de grace: Under Build Settings, search for Framework Search Paths. And add in:

${TARGET_BUILD_DIR}/YourFrameworkName.framework

Look at How Do I Create a Development Framework In iOS Including Swift? for more details.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM