How to build a static or dynamic framework for iOS?
There are many tutorials showing how to build an embedded dynamic framework for IOS.
But none of them details how to create a static framework.
Is it possible to build a static framework with 3Party frameworks?
What is the difference between a static and a dynamic framework? How to do it?
The following will guide you to dive deeply.
Static vs Dynamic Framework
Static Framework
A static framework is easy to maintain and extend libs because dependent frameworks do not need to be linked to the same binary.
- The size of the app is larger than using a dynamic framework.
- lanching time is slower than dynamic Framework
- can not include any resource files.
- not require to include dependent libraries
Dynamic Framework
A dynamic framework is hard to maintain because it’s required to link dependent frameworks in the same binary; this would cause a conflict with other libs.
- The size of the app is smaller
- lanching time is faster than static Framework
- can include .plist or resources files into the same framework
- must link dependent 3Party libraries If you declare and use them in source codes.
Below shows how to do create a framework for Google Sign In.
Steps to create Static Framework
- Create a template as below
-
Import the related frameworks (GoogleSignIn.framework, GoogleSignInDependencies.framework, GoogleSigIn.bundle) and implement your bridge class to use Google Sign’s API in the XCode.
-
Setup XCode to create Static Framework
Go to Build Settings → Linking → Mach-O Type → Choose Static Library
- Setup XCode to create Static Framework
Go to Signing & Capabilities → uncheck Automatically merge signing and Set up None in Team
-
Set up the Headers files that you want to be shared in Public
-
Build your.framework and import your.framework into your App’s XCode Project
-
Import dependent frameworks and bundles files related to your.framework into your app’s XCode Project.
- Add dependent frameworks (GoogleSignIn.framework, GoogleSignInDependencies.framework) in General → Frameworks, Libraries, and Embedded Content
- Add GoogleSigIn.bundle in Build Phases → Copy Bundle Resources
Steps to create a Dynamic Framework
1.Create a framework by XCode by default settings
2.Add and set Do not to embed in General → Frameworks and Libraries for GoogleSignIn.framework and GoogleSignInDependencies.framework as below.
The following show the dependent GoogleSignIn.framework and GoogleSignInDependencies.framework linked to the your.framework.
It means your.framework links GoogleSignIn.framework and GoogleSignInDependencies.framework.
If other frameworks use Google SignIn features, it will cause conflicts or warning messages because GoogleSignIn.framework can be loaded by other libs.
Note: If you create a dynamic framework, not to set Embed & SignIn on dependent frameworks. Otherwise, this would cause an error in which IPA will be failed to generate and verify by clicking Apple’s Distribute Button.
Conclusion
- Be careful that the default setting to create a framework in XCode is Dynamic Library which means Dynamic framework.
- A dynamic framework requires to link those dependent libraries and export to the same binary.
- Remember not to include GoogleSignIn.bundle file or any other resource file inside the static framework. Even if you include it, the exported framework won’t contain it.
- To integrate with a static framework, you need to add dependent libs or bundle files separately. Remember to set -ObjC or -all_load in Linking of Build Setting.
- While integrating a dynamic framework, you might only need to add a dynamic framework because of dependent libraries and .bundle files containing in the framework.