Use static framework inside a dynamic swift framework - objective-c

I am creating custom dynamic framework in pure swift lang which is using a pre-compiled iOS Realm framework (downloaded from their website). I have tried various options to use this framework in an external project but it fails saying Realm was not found.
Here are some of the things I have tried already
Used Realm binary and headers from the framework, added them to the
project and import using a plain import Realm statement
Use the framework in the project ad directed by the instructions on
the above link. #import <Realm/Realm.h> in the umbrella header of
the framework
In both cases the framework compiled without any errors. But when I try to compile another project which uses my framework, I get different errors
Swift compiler error- Module 'Realm' not found (in the MyFramework-Swift.h file)
<Realm/Realm.h> file not found (inside the umbrella header)
My questions
is it possible to use a framework inside a dynamic framework?
if it is, do I need to give specific compile settings in order for a wrapper project to find Realm framework in MyFramework?

Related

Could not build Objective-C module 'Your Framework'

I am using broadcast extension to capture my iPhone screen, In same project I am using some framework, which are working perfect in my project target, If same Framework I am importing in BroadCast Extension target like in side "SampleHandler" class(This is broadcast extension "RPBroadcastSampleHandler" Class), I am facing compiler issue as "Could not build Objective-C module 'your framework name'", As framework build in objective-c and my project is in swift. I am using "POD" to install all framework. I have also try with killing Xcode, Clean drive data, uninstall POD and install back several time, But still not getting any luck. I have also try creating Bridging-Header and import these library in side Bridging-Header and getting "Could not build module 'Your framework name'"
Kindly suggest which changes need to do in framework code to compile in different target in same project. As we are only writing that framework.
to make this work
Go to traget -> setting-> Require Only App-Extension-Safe API = NO

Bridging headers in Swift frameworks not supported, but work in one project

I have a project that is mostly Swift, but has some (internal) Objective-C code wrapping around CommonCrypto functions. It took some some tinkering, but we managed to make it work without exposing the internals by making all the headers private and using a bridging header, not added to any target but referenced in build setting "Objective-C Bridging Header" (on the targets, not the project).
Now we decided to move this part of the project into its own framework. So I created a new project (as Cocoa Touch Framework, same as the old), copied over the necessary source files, recreated the bridging header, and expected it to compile.
However, I'm getting
error: using bridging headers with framework targets is unsupported
This is confusing since there is one in the other project, which builds just fine (for iOS and macOS).
I went through all of the build settings, project and target. The only differences are
iOS target version 10.3 (new) vs 10.2 (old),
no signing vs signing (that may just be because the new project never built), and
some static analysis setting about suspicious conversions.
Neither of those should impact this matter. Nevertheless, I confirmed that the new project doesn't build for 10.2, either.
So what's going on here?

Xcode: How to have shared code (i.e. framework) between main project and plugin bundle

In my project I had two targets, 1) The main application which loads 2) a bundle of plugins. I have started to have objects which need to be available in both targets, so naively tried to compile them separately in each target (producing warnings about having different implementations: Class X is implemented in both Y and Z. One of the two will be used. Which one is undefined.)
To solve this I decided to create a framework, so I added a framework, but I don't know how to make it so that a) both the main app and bundle access the framework (via the compiled framework, and not just through the headers in the project), and so the framework is compiled when I run the project.
If you know how to do this, thanks!
Here are the steps I followed:
Create a new framework in the project , copy all the shared code over.
In the main header of the framework, include the headers of all the shared code.
Build the framework to test it builds (e.g. select the scheme of the framework and click play)
Go to the Build Phases of both the Application and the Plugin Bundle and add the framework to ‘target dependencies’ and ‘Link binary with libraries’
To include the frameworks stuff in code in the app and bundle, just use the main header, and use <> rather than “" e.g if your framework was called Foo use #import
When it comes to deploying, there might have to be some fiddling with where the framework gets installed, currently it gets put alongside the app itself, rather than inside, but I will probably fix that later :)

Building a distributable static library that uses cocoapods

I'm building a static library to be distributed to other iOS developers and having some trouble configuring the linker to allow the static library to be used in another app. I have used this guide to create a MyStaticLibrary.framework bundle containing the lib itself, and other assets such as images. This builds successfully and uses cocoapods to source the required dependencies (AFNetworking, etc.). So far, so good.
But when I import MyStaticLibrary.framework into a new Xcode project to test build an app with the library, I get tons of linker errors (Undefined symbols for architecture i386, _OBJC_CLASS_$_CLASSNAME) indicating that I'm doing something very wrong here.
So my question is, how can I build MyStaticLibrary.framework with the dependencies sourced from cocoapods, such that I can provide a 3rd party with just my framework file and allow them access to all functions specified in the public headers?
Any libraries you include using CocoaPods will not be compiled into your framework by default - they're meant to be external dependencies that are not part of your actual product. However, according to their FAQ, they support a mode where you can download pods and not have them linked to your project. From their FAQ:
Note that CocoaPods itself does not require the use of a workspace. If
you prefer to use sub-projects, you can do so by running pod install
--no-integrate, which will leave integration into your project up to you as you see fit.
To include external dependencies in your compiled binary:
For code: Instead of using cocoapods, check out the repositories you want to include and copy the source files into your project -- this will ensure they are compiled with the rest of your code
For static libraries (i.e. .a files), in your framework's Link Binary With Libraries build phase, make sure to include all the ones you would like to compile. You should also make sure the associated header files are included in Copy Headers build phase, with the appropriate visibility.
Note When bundling third party libraries in this way, you run the risk of conflicting with the projects that are integrating your framework. For example, let's say you are using a lib called SOSomeView, and you choose to compile that in with your framework. Now, if the app you are integrating with also includes SOSomeView, you will get a compile-time error that the class is declared twice. To fix this issue, you should re-namespace any external dependencies you want to hardcode into your framework (i.e. rename the class to XXSOSomeView).
I don't know how to solve that problem if you are compiling static libraries in with your framework.

Understanding bundles frameworks and libraries

I'm developing ios B2B app and I have several questions regarding app modularization.
Firstly i need to understand main difference between bundles and frameworks. When to use bundles and when frameworks.
Another question is. Is it possible for bundle to contain a .framework inside in it and vice versa.
Is it possible to create a plugins for ios app and load them dynamically, if yes then what it should be? bundle framework or library?
Is it possible for library to contain a resource files ?
Is it possible to create a resource bundle and dynamic library and then load them dynamically at runtime.
Is it possible to create a plugins for ios app and load them
dynamically, if yes then what it should be? bundle framework or
library?
No
Is it possible for library to contain a resource files ?
No
Is it possible to create a resource bundle and dynamic library and
then load them dynamically at runtime.
No
A Bundle is a type of Directory, a folder. A Framework is a bundle. So is an Application and so is a Plugin.
A Static Library is a single file code archive you can compile into your app at build time
A Dynamic Library is a single file code archive you can load at Runtime
A Framework is a Dynamic library in a Bundle with other things
A Plugin is a Dynamic library in a Bundle with other things
The Xcode build option 'Bundle' means 'Place the compiled Dynamic Library in a Bundle' - this is what you do when you want to create a Plugin.
Static libraries are the only option for modularising your code on iOS.
On the desktop..
Typically a Framework is for sharing code and resources between multiple apps. You want your app to behave as though the code was actually compiled into it. You want loading to happen transparently and you don't want to do anything special to use the methods, functions, etc. contained in it.
A Plugin (a Bundle containing compiled code and resources) is for optional, dynamically loaded code, e.g. a software extension that you can choose to load or not. You want to carefully architect your app so that it isn't dependent on the Plugin but acquires new behaviour if you manually locate and load it at Runtime.
A Framework and a Plugin are very similar, but a Framework has a strict file layout to facilitate locating and loading code and resources. With a plugin, these jobs are your responsibility so you can structure the Bundle contents however you want.
Because loading code is so easy in Cocoa on OSX (but not iOS) Frameworks can contain Plugins which contain Frameworks which contain more Frameworks, etc.
On iOS some people put Static Libraries in Bundles with resources and call them Frameworks. This has none of the benefits and all of the drawbacks of a real framework.