Conditionally import a cocoapod framework (own framework) present in app inside another cocoapod framework (own framework) - ios

I have two custom frameworks (Main and Child )both present on cocoapods. I want to conditionally import my child.framework into my Main.framework If user has installed child.framework. I am unable to conditionally import child framework into my main.framework as canImport always fails even If user has installed child.framework
I expected that in my main framework #if canImport(childFramework) will succeed If user has installed child.framework but It's always getting failed.

Related

XCFramework with other 3rd party dependencies

Problem
I am developing a Framework (let's name it MyFramework) which I want to use in a host iOS application. MyFramework depends on several other 3rd party libraries (Alamofire, ZIPFoundation, etc.) which I have added as Swift Package Manager dependencies in the MyFramework project.
After creating MyFramework.xcframework and adding it to the host iOS application, I get a compilation error saying No such module 'Alamomfire'. As suggested in this thread I have added Alamofire as SPM dependency in the host iOS Application as well, which resolves the compilation error. But during runtime I get a console message saying Class _TtC9Alamofire21CompositeEventMonitor is implemented in both <HOME>/Library/Developer/Xcode/DerivedData/DevSample-frwzbtlatwjedobzfossixhbtork/Build/Products/Debug-iphonesimulator/MyFramework.framework/MyFramework (0x10346eb98) and <HOME>/Library/Developer/CoreSimulator/Devices/D17DA122-DB76-4447-B8FD-14FC8405A856/data/Containers/Bundle/Application/F68CBE06-CC7F-40AA-ACA8-2210048CA495/DevSample.app/DevSample (0x102ed31f8). One of the two will be used. Which one is undefined.
Questions
Is this expected behavior? As mention in this Apple Doc creating Umbrella Frameworks is discouraged. How to get rid of those runtime warnings/errors then?
#Nominalista I have created a similar app-and-modules setup in this way: I created the app, and the framework within the same directory, and then I dragged this framework's .xcodeproj inside my app. Inside the framework, I have added Swift packages as Alamofire, ZIPFoundation. I can use either of these 2 transitive dependencies both from the framework itself, and the app target as well with a simple ie, "import Alamofire".
I am using Xcode 12.5 and I am explicitly adding Alamofire as Swift package both to the framework and the app, I am not getting "..is implemented in both..." warnings. So can I ask you how your project is being setup?

Requiring an external dependency for a Swift binary framework

I am in the process of building a set of SDK frameworks for use in my organization.
There is a core framework for user authentication Auth and another framework B that relies on it for user information. Framework B is optional.
How am I supposed to setup a Workspace and project target for B, so that
Auth is not embedded with B, but available during development
B uses Auth as an external dependency that needs to be present in whatever project it is embedded in
In the end I want to use xcframeworks and distribute over SwiftPM packages.
Currently I have a Project for B with targets for the framework and a Demo application.
The framework target includes Auth under Framework & Libraries as Do Not Embed and the Demo's Frameworks, Libraries and Embedded Content Section imports Auth and B as Embed & Sign.
This works on simulator, but running the Demo on device throws the following errors:
No such module 'Auth' on the import Auth line in code in B
Missing required module 'Auth' on the import B line in code in the Demo application
Is there any way to make B find the framework inside the project it's imported in as an external dependency?
Would this be easier to do with SwiftPM (just wait for Swift 5.3 and support for binary artifacts)?
I came across this document https://developer.apple.com/library/archive/technotes/tn2435/_index.html#//apple_ref/doc/uid/DTS40017543-CH1-PROJ_CONFIG-APPS_WITH_DEPENDENCIES_BETWEEN_FRAMEWORKS which kind of states that I'm on the right way:
The app target is responsible for embedding all of the frameworks, including any frameworks that other frameworks depend on

Xcode Create a swift framework that itself uses cocoapods

I'm still relatively a beginner. I'm trying to create a uber type app that uses firebase database (installed via cocoa pods). I'm creating an app for drivers and another for riders. I'm also trying to start out right by structuring my app as best i can to begin with.
In the riders app, I created a database handler singleton to control access to the database including functionality to add the rider to the database. A common firebase database is shared by both apps. Rather than have both apps have separate database handlers (and various other independent handlers), I've created an independent swift framework to hold the handlers.
I've added my swift framework to the top level of my workspace in both the rider app workspace and in the driver app workspace (by dragging in the framework xcodeproj file) and gone through all the processes I've read about to build the framework to a generic device and copy the framework to the actual app embedded binaries etc. Independently both app workspaces are working properly .... however ... i have 2 problems at this stage
if i try to open both workspaces at the same time (ie work on the rider app at the same time as working on the driver app), Xcode gives me a workspace integrity error basically giving error "couldn't load xcode project (ie the framework project) because it is already opened from another project". the app still runs ok but the error (red error) remains. how can I fix this?.
I want to use FirebaseDatabase within the database handler framework itself but even though I have FirebaseDatabase installed in the main app, the framework can't see it ie import FirebaseDatabase line is showing "no such module". I also tried installing FirebaseDatabase via cocoa pods separately in the framework project and am still getting the same "no such module" error. How can i get the framework code to see the required cocoa pod frameworks
Having a shared framework project outside of two app projects, but included in their workspaces can cause problems. Editing the framework in one workspace may break the app in the other workspace if the change is a major one.
I think you have two options. One is that you keep the framework app in a central repository and check it out into the app projects. This lets each app project use the version of the framework they are comfortable with and if one changes, the other doesn't see those changes until you tell it to update.
Another option is to switch to a single workspace with both apps and the framework in it. Effectively saying that you will always be working on both at the same time and they will always be in sync.
I can't say what the issue is with not finding the framework as I don't use CocoaPods, but I'd be checking all the framework search paths in each project settings to see if they are searching the right directories for the compiled frameworks. You should not be copying any dependency frameworks in the framework target, but all of them will need to be copied in the app targets.

Missing required module error

I am creating a framework "ABC" in Swift. I have integrated Objective C code using submodule "ABCPrivate" inside framework. I have set Import Paths for internal module.
When I use this framework in other applications it is referring to the same paths that i have set in Import Paths in Framework project.
Its working fine on machine in which its developed but not on other machines(as it is referring to same import paths for internal module).

Swift: using private framework

I have built an iOS Swift framework with Xcode.
After writing the code I have build the project and took the .framework file inside the Products folder to test it.
To test the framework have open a new application and drag and drop the .framework file previously built and I added it into the embedded binaries list of my application project.
To import it into my ViewController.swift class I wrote:
import frameworkName
No problem until here, this means that the project sees the framework.
When I try to use a public class inside the framework with:
var x : className?
I get the following error:
'className' is unavailable: cannot find Swift declaration for this class
What does it mean? What is the problem?
When you're referencing a framework in the products directory from your workspace, ensure that the location is "Built Products" and the filename is just the framework name, without any additional path components.
If you're referencing a framework that isn't in your workspace, I would recommend using Carthage instead of copying it directly into your repository. This will make versioning much easier, and will ensure that it is built correctly for both simulator and device.
To actual a self defined framework, u really have to do a lot of things.
Firstly, make sure ur framework is used on right device. It's to say framework can only be used on corresponding device(either one of Simulator, Device and Mac). In other words, if framework A built on simulator, project import framework A can only pass compile and successfully built on simulator.
P.S. if universal version desired, -lipo command is what u need to further explore.
Secondly, during implementing ur framework , make sure all the classes, methods and variables u want use outside start with Public.
Thirdly, check ur project setting Embedded Binaries and linked Frameworks and Libraries do contain ur framework.

Resources