How to load framework at runtime in Xcode? - ios

I have a third party framework customx.framework (iOS) customx.framework (Simulator).
To run project on simulator customx.framework (Simulator) to be imported & for device
customx.framework (iOS) to be imported
simultaneously importing is not supported by xcode
At present i am manually importing framework, so i am looking for runtime scrip changes or combined (iOS+Simulator) framework to import in xcode project.
For that
I have tried lipo & libtool but seems didn't worked.
I used validate workspace but it fails when importing modules.
tried links -
iOS merge several framework into one
Building for iOS Simulator, but the linked framework '****.framework' was built for iOS

simultaneously importing is not supported by Xcode
Supported. It is named XCFramework.
So assuming each variant of frameworks have been built correctly (pay attention that BUILD_LIBRARY_FOR_DISTRIBUTION=YES, just in case - Xcode set it to YES automatically), join them in terminal or Xcode script phase with next command:
$ xcodebuild -create-xcframework
-framework "/full_path_to_iOS_variant/customx.framework"
-framework "/full_path_to_Simulator_variant/customx.framework"
-output "/full_path_to_result/customx.xcframework"
and then add it once in target dependency
and that's it.

You can archive for iOS device and iOS simulator and merge them to one xcframework. This is my current project xcodebuild command for it.
- Archive for iOS devices
xcodebuild archive \
-scheme Framework-scheme \
-configuration Release \
-destination 'generic/platform=iOS' \
-archivePath './build/native/Framework-Name.framework-iphoneos.xcarchive' \
SKIP_INSTALL=NO \
BUILD_LIBRARIES_FOR_DISTRIBUTION=YES
- Archive for iOS simulator
xcodebuild archive \
-scheme Framework-scheme \
-configuration Release \
-destination 'generic/platform=iOS Simulator' \
-archivePath './build/native/Framework-Name.framework-iphonesimulator.xcarchive' \
SKIP_INSTALL=NO \
BUILD_LIBRARIES_FOR_DISTRIBUTION=YES
- build framework from archives
xcodebuild -create-xcframework \
-framework './build/native/AdTrue-Native.framework-iphoneos.xcarchive/Products/Library/Frameworks/Framework-Name.framework' \
-framework './build/native/AdTrue-Native.framework-iphonesimulator.xcarchive/Products/Library/Frameworks/Framework-Name.framework' \
-output './build/native/Framework-Name.xcframework'

Related

Missing Required Module 'RxCocoaRuntime'

I am using RxCocoa in custom framework, so I am trying to inject dependencies with SPM and make that .xcframework.
I made the .xcframework, but the following error keeps appearing.
The process of creating and configuring the framework project and creating the xcframework was as follows.enter image description here
Development Environment
CPU : Apple slicon (M1 PRO)
MacOS : Ventura 13.1
Xcode : 14.2(14C18)
Step
Create framework project (km-ios-sdk)
Set Build Active Architecture Only YES from NO
Mach-O Type is Dynamic Library
Write simple code using RxSwift, RxCocoa
Create .xcarchive and .xcframework
xcodebuild archive \
-scheme km-ios-sdk \
-archivePath ./archive/km-ios-sdk.framework-iphoneos.xcarchive \
-sdk iphoneos \
SKIP_INSTALL=NO \
BUILD_LIBRARY_FOR_DISTRIBUTION=YES \
xcodebuild archive \
-scheme km-ios-sdk \
-archivePath ./archive/km-ios-sdk.framework-iphonesimulator-arm64.xcarchive \
-sdk iphonesimulator \
SKIP_INSTALL=NO \
BUILD_LIBRARY_FOR_DISTRIBUTION=YES \
xcodebuild -create-xcframework \
-framework './archive/km-ios-sdk.framework-iphoneos.xcarchive/Products/Library/Frameworks/km_ios_sdk.framework' \
-framework './archive/km-ios-sdk.framework-iphonesimulator-arm64.xcarchive/Products/Library/Frameworks/km_ios_sdk.framework' \
-output './KmSDK.xcframework'
Created Package.swift as below and uploaded it to git.
enter image description here
If you download and import km-ios-sdk as spm in the sample app, the error appears
enter image description here
It is the same even if you manually download .xcframework, and it is the same even if you download it with cocoapod. Is there any way to solve it?

Xcode: build all the dependent targets when building a framework target

I have an Xcode project with a Framework A target.
That target depends on a few other modules, e.g. SubFramework A and SubFramework B.
When running the Xcodebuild command:
xcodebuild archive \
-scheme SchemeName \
-configuration Release \
-destination 'generic/platform=iOS Simulator' \
-archivePath './build/SchemeName.framework-iphonesimulator.xcarchive' \
SKIP_INSTALL=NO \
BUILD_LIBRARIES_FOR_DISTRIBUTION=YES
I'm getting only the resulting library out, i.e. SchemeName.framework. When I then try to bundle it into the application, of course, I cannot compile it as SubFramework A and SubFramework B are not included.
Q:
Is it possible to modify the scheme's build settings, xcodebuild command or target settings in order to get the SubFramework A and SubFramework B as the build output results too?

xcodebuild -create-xcframework - BCSymbolMaps missing - on M1 Xcode 14.0

I use the same bash script for building the XCFramework for at least 2 years and everything worked successfully until the moment I switched my Mac to M1 and my Xcode is 14.0.
The script is pretty standard (see below).
On MacPro M1, Xcode 14.0 I get the following error (the same script works just fine on Xcode 13.1).
error: the path does not point to a valid debug symbols file: /Users/*******/build/Release-iphoneos.xcarchive/BCSymbolMaps/*
Indeed when I look at build/Release-iphoneos.xcarchive folder - the BCSymbolMaps is not there. I verified that the Xcode setting "debug information format" is dwarf with dsym file.
Can someone please help me understand what is this error? and why it started happening on M1, Xcode 14.0 ?
Thank you
See my bash build script below.
# Build the framework for device and for simulator (using
# all needed architectures).
xcodebuild archive -scheme "${TARGET_NAME}" -destination="iOS" -sdk iphonesimulator SKIP_INSTALL=NO BUILD_LIBRARIES_FOR_DISTRIBUTION=YES -archivePath "${SRCROOT}/build/Release-iphonesimulator"
xcodebuild archive -scheme "${TARGET_NAME}" -destination="iOS" -sdk iphoneos SKIP_INSTALL=NO BUILD_LIBRARIES_FOR_DISTRIBUTION=YES -archivePath "${SRCROOT}/build/Release-iphoneos"
ls -l "${SRCROOT}/build/"
# https://developer.apple.com/forums/thread/655768
# First, get all the UUID filepaths for BCSymbolMaps, because these are randomly generated and need to be individually added as the `-debug-symbols` parameter. The dSYM path is always the same so that one is manually added
echo "XCFramework: Generating IPHONE BCSymbolMap paths..."
IPHONE_BCSYMBOLMAP_PATHS=(${SRCROOT}/build/Release-iphoneos.xcarchive/BCSymbolMaps/*)
IPHONE_BCSYMBOLMAP_COMMANDS=""
for path in "${IPHONE_BCSYMBOLMAP_PATHS[#]}"; do
IPHONE_BCSYMBOLMAP_COMMANDS="$IPHONE_BCSYMBOLMAP_COMMANDS -debug-symbols $path "
echo $IPHONE_BCSYMBOLMAP_COMMANDS
done
echo "XCFramework: Generating IPHONE BCSymbolMap paths... --> Done"
# XCFramework with debug symbols - see https://pspdfkit.com/blog/2021/advances-in-xcframeworks/#built-in-support-for-bcsymbolmaps-and-dsyms
xcodebuild -create-xcframework -allow-internal-distribution \
-framework "${SRCROOT}/build/Release-iphoneos.xcarchive/Products/Library/Frameworks/${FRAMEWORK_NAME}.framework" \
-debug-symbols "${SRCROOT}/build/Release-iphoneos.xcarchive/dSYMs/${FRAMEWORK_NAME}.framework.dSYM" \
$IPHONE_BCSYMBOLMAP_COMMANDS \
-framework "${SRCROOT}/build/Release-iphonesimulator.xcarchive/Products/Library/Frameworks/${FRAMEWORK_NAME}.framework" \
-debug-symbols "${SRCROOT}/build/Release-iphonesimulator.xcarchive/dSYMs/${FRAMEWORK_NAME}.framework.dSYM" \
-output "${SF_RELEASE_DIR}/${FRAMEWORK_NAME}.xcframework"
bit code is not supported by xcode14 so bcsymbolmaps are no more relevant for xcode14

Why am I getting this error while creating XCFramework?: While building for iOS Simulator, no library was found in path

I have written the below commands in a script file to create XCFrameworks for both iPhoneSimulator and iPhoneOS like this below:
xcodebuild archive -scheme "ProjectD" -archivePath "/Users/Ron/Archives/ProjectDiphoneSimulator.xcarchive" -sdk 'iphonesimulator' SKIP_INSTALL=NO
xcodebuild archive -scheme "ProjectD" -archivePath "/Users/Ron/Archives/ProjectDiphoneOS.xcarchive" -sdk 'iphoneos' SKIP_INSTALL=NO
xcodebuild -create-xcframework -framework "/Users/Ron/Archives/ProjectDiphoneSimulator.xcarchive/Products/Library/Frameworks/ProjectDiphoneSimulator.framework" -output "/Users/Ron/XCFrameworks/ProjectDiphoneSimulator.xcframework"
xcodebuild -create-xcframework -framework "/Users/Ron/Archives/ProjectDiphoneOS.xcarchive/Products/Library/Frameworks/ProjectDiphoneOS.framework" -output "/Users/Ron/XCFrameworks/ProjectDiphoneOS.xcframework"
I tried to use this in a project after dragging and dropping it onto the Embedded Frameworks and Libraries
When I run the project with a device as the target, it works fine. But when I run it on a simulator, it throws the below error:
While building for iOS Simulator, no library was found in /Users/Ron/XCFrameworks/ProjectD.xcframework
If I change the order of the archive and XCFramework creation like below:
xcodebuild archive -scheme "ProjectD" -archivePath "/Users/Ron/Archives/ProjectDiphoneOS.xcarchive" -sdk 'iphoneos' SKIP_INSTALL=NO
xcodebuild archive -scheme "ProjectD" -archivePath "/Users/Ron/Archives/ProjectDiphoneSimulator.xcarchive" -sdk 'iphonesimulator' SKIP_INSTALL=NO
xcodebuild -create-xcframework -framework "/Users/Ron/Archives/ProjectDiphoneOS.xcarchive/Products/Library/Frameworks/ProjectDiphoneOS.framework" -output "/Users/Ron/XCFrameworks/ProjectDiphoneOS.xcframework"
xcodebuild -create-xcframework -framework "/Users/Ron/Archives/ProjectDiphoneSimulator.xcarchive/Products/Library/Frameworks/ProjectDiphoneSimulator.framework" -output "/Users/Ron/XCFrameworks/ProjectDiphoneSimulator.xcframework"
It works for simulator and not on the device with the same error:
While building for iPhone, no library was found in /Users/Ron/XCFrameworks/ProjectD.xcframework
I have set the Build Libraries for Distribution to Yes as well. If I try to create them with different destinations(2 XCFrameworks for iphone and simulator) it is working. But that literally defeats the purpose of XCFrameworks.
Am I missing something or doing any of the steps wrong? Thanks for the answers in advance.
You need to create a single .xcframework file from both .framework. (that will include simulator and device architectures) To do that run a single xcodebuild -create-xcframework command and pass the 2 different .framework files:
xcodebuild
-create-xcframework
-framework "/Users/Ron/Archives/ProjectDiphoneOS.xcarchive/Products/Library/Frameworks/ProjectDiphoneOS.framework"
-framework "/Users/Ron/Archives/ProjectDiphoneSimulator.xcarchive/Products/Library/Frameworks/ProjectDiphoneSimulator.framework"
-output "/Users/Ron/XCFrameworks/ProjectDiphoneOS.xcframework"
When you instead run 2 different xcodebuild -create-xcframework commands you have as a result your .xcframework to include only the last .framework file you passed, which is for devices. That's why your project fails to run in simulator.

Error invalid architecture 'arm' when app from command line + iOS

I want to build an iOS app from command line with iOS simulator.
The build settings are:
1. Architectures - armv7
2. Base SDK - Latest IOS(6.1)
3. Build Active Architecture only - yes
4. Valid architectures - armv7 (also tried adding i386)
5. IOS deployment target - IOS 4.3
I am executing the following command:
xcodebuild -target splistapp2 -sdk iphonesimulator6.1 -configuration Release (also tried with -arch "i836")
But this command gives following error:
invalid architecture 'arm' for deployment target '-mios-simulator-version-min=4.2'
What could be the problem?
In case anyone running into the same annoying problem again, I will share my script here: Remember to run this command under the directory that has the xcodeproj file.
xcodebuild \
-project "full-path-to-your-xcodeproj-file" \
-target YOUR_TARGET \
-sdk iphonesimulator6.1 \
-arch i386 \
-configuration Debug \
VALID_ARCHS="armv6 armv7 i386" \
ONLY_ACTIVE_ARCH=NO \
TARGETED_DEVICE_FAMILY="1" \
clean install
I modified the TARGETED_DEVICE_FAMILY because I only build for iPhone. If you want to build for both iPhone and iPad, delete this line or replace with TARGETED_DEVICE_FAMILY="1, 2".
The device uses ARM; while the simulator uses i386. Pick one or the other:
iphonesimulator6.1 and arch i386
iphoneos and arch armv7 (or armv7s)
Implement using arch1386 architecture.
xcodebuild -project splistapp2 -target TEST_TARGET -sdk iphonesimulator -configuration "Debug" -arch i386

Resources