XCFramework with Pods Dependencies
Asked Answered
V

3

25

Our goal is to create a framework that hides our internal code and provide SDK to our customers. We have thought of creating XCFramework which fulfills our requirement. Umbrella framework is also suggested over the internet but mostly suggested to avoid that approach. Our Framework is dependent on some third-party libraries which we are using via Pods.

Issue: XCFramework does not compile pods framework. We got an error like "Xyz(Pod) module not found". Even if we add pods from the client-side it does not work.

Code to create XCFramework is as bellow

1) Create an archive for iOS platform

xcodebuild archive -workspace ABC.xcworkspace \
  -scheme ABC \
  -sdk iphoneos \
  -archivePath "./archives/ios_devices.xcarchive" \
  BUILD_LIBRARY_FOR_DISTRIBUTION=YES \
  SKIP_INSTALL=NO

2) Create an archive for iOS-Simulator platform

  xcodebuild archive  -workspace ABC.xcworkspace \
  -scheme ABC \
  -sdk iphonesimulator \
  -archivePath "./archives/ios_simulators.xcarchive" \
  BUILD_LIBRARY_FOR_DISTRIBUTION=YES \
  SKIP_INSTALL=NO

3) Create an XCFramework from Archives

xcodebuild -create-xcframework \
-framework ./archives/ios_devices.xcarchive/Products/Library/Frameworks/ABC.framework \
-framework ./archives/ios_simulators.xcarchive/Products/Library/Frameworks/ABC.framework \
-output build/ABC.xcframework

We got ABC XCFramework successfully but dependencies are not included in XCFramework. Any solution for this? or Is there any way where we can set framework search path to client-side? or Any alternate approach?

Ventriloquize answered 2/11, 2020 at 12:29 Comment(8)
Did the below solution actually work? I'm trying something similar and when using the framework I get errors.Detonator
Yes creating a pod works for me @DustinPfannenstielVentriloquize
@BhavinVaghela - if you do not include the external dependancies in your Framework project, doesn't the compiler give you an error when trying to build the Framework ?Salzburg
@ShawnFrank I have tried in serval way, every time I got an error like a module(third party) is not found. The solution which works for me is to create a pod and add a third-party dependency in a pod spec. Also, I am not getting an error while building or creating a framework I got an error while adding to another project.Ventriloquize
@BhavinVaghela in my case only create of iOS device but not for simulatorBurkett
@BhadreshSonani Have you followed step 2 properly? Please share some more detailsVentriloquize
@BhavinVaghela Do you have any idea about integration of facebook login in framework ?Burkett
@BhadreshSonani No, i have not integrate facebook in frameworkVentriloquize
B
16

You can create a pod and publish it.

Check https://guides.cocoapods.org/making/making-a-cocoapod.html

Sample Podspec file with XCFramework + Third party dependency

Pod::Spec.new do |s|  
    s.name              = 'XCFrameworkTest' # Name for your pod
    s.version           = '0.0.1'
    s.summary           = 'Sample Spec'
    s.homepage          = 'https://www.google.com'

    s.author            = { 'Sample' => '[email protected]' }
    s.license = { :type => "MIT", :text => "MIT License" }

    s.platform          = :ios
    # change the source location
    s.source            = { :http => 'http://localhost:8080/XCFrameworkTest.zip' } 
    s.ios.deployment_target = '10.0'
    s.ios.vendored_frameworks = 'XCFrameworkTest.xcframework' # Your XCFramework
    s.dependency 'PromisesSwift', '1.2.8' # Third Party Dependency
end 

After you publish your pod, Customer can use cocopods to get our framework.

In Customer's Podfile

pod 'XCFrameworkTest' #Your pod name
Brownie answered 11/11, 2020 at 7:44 Comment(7)
This is elegant.Detonator
There are important bits to be found here though: github.com/CocoaPods/CocoaPods/issues/…Detonator
I tried this approach but I'm still unable to run an app, because of a build errors: > Missing required module 'Stripe3DS2'. > Failed to build module from its module interface; it may have been damaged or it may have triggered a bug in the Swift compiler when it was produced. My guess is that xcframework couldn't handle static dependency from Stripe.Detrimental
This solution makes sense however what I don't get is, if we do not include the exteral dependancies in the Framework project, how does the Framework compile and build ?Salzburg
@KonradSiemczyk - I am also facing the same issue. Did you got any solution???Grangerize
@Grangerize Nope, but if you will find some solution, please let me know :)Detrimental
@ShawnFrank You and I had the same question I think, does this answer it? https://mcmap.net/q/539673/-how-to-create-xcframework-for-closed-source-cocoapod-when-it-has-dependencies-on-other-podsSalter
B
6

I have created a template for this purpose. You can test it by running the command

pod lib create YourLibName --template-url="https://github.com/zalazara/pod-template-xcframework.git"

The template basically generates an example project together with its podfile file where, in turn, the framework to be developed is embedded, then the generation file compiles the framework using the workspace.

BUILD_DIR="Build"
TMP_DIR="${BUILD_DIR}/Tmp"
IOS_ARCHIVE_PATH="${TMP_DIR}/iOS.xcarchive"
IOS_SIM_ARCHIVE_PATH="${TMP_DIR}/iOSSimulator.xcarchive"

rm -rf ${BUILD_DIR}
rm -rf "${FRAMEWORK_NAME}.xcframework"

xcodebuild archive \
 -workspace "Example/${WORKSPACE}" \
 -scheme ${SCHEME} \
 -archivePath ${IOS_SIM_ARCHIVE_PATH} \
 -sdk iphonesimulator \
 SKIP_INSTALL=NO \
 | xcpretty


 xcodebuild archive \
 -workspace "Example/${WORKSPACE}" \
 -scheme ${SCHEME} \
 -archivePath ${IOS_ARCHIVE_PATH} \
 -sdk iphoneos \
 SKIP_INSTALL=NO \
 | xcpretty

 xcodebuild -create-xcframework \
 -framework ${IOS_SIM_ARCHIVE_PATH}/Products/Library/Frameworks/${FRAMEWORK_NAME}.framework \
 -framework ${IOS_ARCHIVE_PATH}/Products/Library/Frameworks/${FRAMEWORK_NAME}.framework \
 -output ${FRAMEWORK_NAME}.xcframework \
 | xcpretty

For more information : https://github.com/zalazara/pod-template-xcframework.git

Barrier answered 18/3, 2021 at 18:46 Comment(0)
B
0

Ayyanar's solution do the trick for me, but get an error on build: "Failed to build module from its module interface; it may have been damaged or it may have triggered a bug in the Swift compiler when it was produced".

I solve it linking the target to the .xcframwork from Pod.

Hope it helps somebody.

Berti answered 15/7, 2021 at 12:19 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.