So what is framework?
A framework is a modular and reusable set of code that is used as the building blocks of higher-level pieces of software.
It is a bundle (Directory structure) that contains shared libraries as well as sub directories of headers and other resources. However Frameworks can include the following,
- Shared Library
- Header Files describing the API
- Additional Resources
- Custom Appearance/ UI
- Configuration files
Why do we use it?
The best reason to use framework is that it can be built once and be reused an infinite number of times!
If you want to know more about what are frameworks, their types, and how they work? You can refer to various articles and courses present online.
As this topic have lot of detailing, so without wasting much time lets dive deeper into how to create them and using them in your apps.
How to start :
Open Xcode tool, start by clicking on new project from top menu bar and then on File. Choose Cocoa Touch Framework Option from the dialog. Choose whatever name you want, here I am using AppColors as it contains all the necessary colors required for the branding of your app.
Once framework is created, you will see .framework file created in Products folder.
Adding Files in Framework :
If you are building the framework from the files present in your App already, then go in the Finder where your project is present. Just copy those files and add them in your framework folder.
Now again go to the Xcode, add all relevant files by using ‘Add files to AppColors’ option or you can just use drag and drop.
Add the files you want to be as public in Headers section of Build Phases settings.
You can also import all the public files in AppColors.h header file for convenience.
Cheers! Now your framework is ready for compilation.
Don’t forget to delete those copied files from your app directory to avoid duplicity.
Compiling and Building the Framework :
To build your framework successfully for both Simulator and Real devices such as iPhone, firstly, we need to have a valid architecture settings.
For adding architecture settings, open your framework’s .xcodeproj file, go to build settings tab and search for Valid Architectures setting, you will see that debug and release options already contain the values armv7, armv7s, arm64 and if not add them, these architectures are required for compatibility with real devices.
For Simulators, you can add Any Simulator SDK setting within Debug and Release modes. Add architectures such as x86_64 for this setting.
Do not forget to add same settings for all the targets of your framework project.
Before building the framework, do check the Scheme of your target you are building.
Editing Scheme: Click on target you are about to build on top bar of Xcode. Then click on Edit Scheme from the drop-down menu. Set Build Configuration to “Debug” in Build Tab.
Now Build your framework on any device whether simulator or real device.
Hurray!! Now your framework is compiled and build successfully.
Finding your .framework file in Finder :
Now if you had build your framework on simulator, then you will find your .framework file in Debug-iphonesimulator folder present in your Derived Data of framework.
For locating the Derived Data of your framework, click on Xcode menu on top menu bar, and click on Preferences option. Open Locations tab, click on ➡️ icon present after derived data path. Finder will open with derived data folder in it, click on it. Open your framework folder, then click Build and then Products.
Bang!!! there you will find your Debug-iphonesimulator folder, click on it and you will find your .framework file.
Copy this file in your frameworks folder of app directory for convenience.
Using it in your app :
Open your app project. Go to target app in .xcodeproj file, then on General tab, search for Embed binaries section, click on +embed binaries, and then click on Add Other option on opened dialog. Locate your .framework file, and add it. Check tick on Copy items if needed option, it will copy your .framework file in other required sections.
Also, go to your Build Settings tab of your app project, mention the path of framework project (where .framework file is present) in ‘Framework Search Paths’ setting.
And there you are ready with your app with framework embedded in it. Just import it as,
Run your app on any simulator, it will build successfully.
Now try running on connecting with Real device like iPhone……
Wait what!! it got some errors. Some absurd errors of invalid architectures or conflicting architectures. ‘Apple Mach-O Linker error’
Here what happened, the framework you had build before was only compatible for simulator devices. Hence integrating that .framework file won’t run with real devices because that framework doesn’t support architectures required for real devices.
Here is the simple solution for this problem too…
Aggregate or Universal Framework :
Open your framework project and, go to Xcode menu on top menu bar. Click on new target.
Select iOS, from side menu on the dialog, choose others, then select Aggregate.
Name your aggregate as framework-Universal. Example, ApplicationColors-Universal. Make architecture settings same as your other targets of framework.
Now go to Build Phases tab in your aggregate target settings. Click on + symbol present on top menu bar. Click on Add New Run Script option. Add following script in the Run Script section,
Now build your Aggregate target. And see the magic!!!!
Finder will open the folder containing new .framework file, which is compatible for all simulators as well as real devices.
Integrate it in your app using the process you used for earlier framework..
Build your app..
Now your app will be successfully running on both simulator devices and real devices without any error. Enjoy!!
Uploading the app with framework on App Store :
For making the successful archive for your app and uploading it to the App Store, ensure the following things to be done:
- If you want to enable bitcode in your framework, make sure you have added the bitcode settings in your framework. For this, go to your framework project settings and then for each target,
** Search for Enable Bitcode setting and set it to YES for Debug and Release modes.
** Search for bitcode settings. Add -fembed-bitcode in both Debug and Release modes or you can add -fembed-bitcode-marker in Debug and
-fembed-bitcode in Release mode.
** Add BITCODE_GENERATION_MODE under User Defined setting , and then add bitcode for both Debug and Release modes or you can add marker in Debug and bitcode in Release mode.
For more details, refer to stackoverflow.
- Open your app project, go to target app settings, and then open Build Phases tab, add following Run Script to remove unsupported architecture for uploading build,
** If you are using Carthage, follow 8th, 9th and 10th step given in “Quick Start” section. Mention the path for input file as your .framework file path as,
$(SRCROOT)/Frameworks/AppColors/AppColors.framework** Else, refer this.
Huh!!!! Quite a long procedure… But guess what, now you are done!!! And Congrats you finally built your own custom framework for iOS App in Objective C… Cheers!!!! 😇
Hope you like my article, please do leave a feedback and questions if you have any doubt. And off course share and do claps if you found it helpful…👏🏻