iosframeworkscocoapodsxcframework

How to create XCFramework for closed source CocoaPod when it has dependencies on other pods


I am looking to create a closed source CocoaPod. From my research the recommendation seems to be to distribute it as an XCFramework. (source) It seems it should also be possible to make your framework dependent upon other CocoaPods by specifying them in your Podspec file. (source) (source) That will ensure when someone adds this pod to their Podfile and runs pod install, it will install this framework and its dependencies.

I have created a framework Xcode project and have been developing it inside our app's workspace as a subproject. So at this time, the app has all of the dependencies installed via CocoaPods, which allows the framework in it to utilize them. Now I am ready to prepare the framework for distribution so it can be used in other apps.

From what I understand, I need to create an XCFramework first and then can create a CocoaPod for it. When I go to archive the framework project, I get an error because it cannot find the dependencies, which makes sense. At this point I don't understand how this is supposed to work, because it seems the framework needs the dependencies included in itself in order to successfully create the XCFramework, but I anticipated from my research this would be handled by CocoaPods and not included in the framework itself.

xcodebuild archive \
-scheme MyFramework \
-configuration Release \
-destination 'generic/platform=iOS' \
-archivePath './build/MyFramework.framework-iphoneos.xcarchive' \
SKIP_INSTALL=NO \
BUILD_LIBRARIES_FOR_DISTRIBUTION=YES
/Users/.../AppName/MyFramework/Helpers/Extensions.swift:10:8: error: no such module 'Kingfisher'
import Kingfisher
       ^

** ARCHIVE FAILED **

Solution

  • To resolve this, what I did is create a Podfile for the framework project and run pod install so it has its own workspace. I verified I was able to successfully build the framework in Xcode when opening that workspace. Then to create the archive, specify it should build the workspace rather than the project like so:

    xcodebuild archive \
    -workspace MyFramework.xcworkspace \
    -scheme MyFramework \
    -configuration Release \
    -destination 'generic/platform=iOS' \
    -archivePath './build/MyFramework.framework-iphoneos.xcarchive' \
    SKIP_INSTALL=NO \
    BUILD_LIBRARIES_FOR_DISTRIBUTION=YES
    

    From what I understand, when you create the XCFramework, the dependencies won't be included it will only be your own framework. However the dependencies have to be accessible in order to successfully build the archive.

    This understanding resolves the confusion I had when asking this question. Now I can proceed to create a CocoaPod for my XCFramework and specify its dependencies in the Podspec file.