In this tutorial our focus is to create a framework and then use in our application locally and remotely.
A framework is a hierarchical directory that encapsulates shared resources, such as a dynamic shared library, nib files, image files, localized strings, header files, and reference documentation in a single package. Multiple applications can use all of these resources simultaneously. The system loads them into memory as needed and shares the one copy of the resource among all applications whenever possible.
Benefits of Framework
- Code/Features modularization
- Sharing of code between multiple applications
- Sharing of resources between multiple applications
- Distribute it as a third party library
- Best for teams working parallely
Creating a Framework for iOS
From Xcode, choose File > New Project to create your project > On the Framework & Library section Select Cocoa Touch Framework > Click Next > Enter Product Name “
MyFramework” as shown in Figure 1 > Click Next > Choose Location > Create
Framework has created as shown in Figure 2 with the name
Framework responsibility is to fetch data from server and return to the consumer with the data. Let’s create a swift file From Xcode, choose File > File > Swift File >
APIRequestLoader > Create
As shown in Figure 3 we completed our framework code. To consume this framework client needs to provide object that conform to
APIRequest protocol where it implement the make and parseResponse method with the expected Request and Response Data Type.
Build the framework project to make sure that you get Build Succeeded with no build warnings or errors.
At this point we created framework. Now create a consumer project.
Choose File > New > Project > In Application section, select Single View Application and then click Next > In the dialog that appears Product Name:
Consumer > Next > Create .
As shown in Figure 4 we created a consumer application that will consume our framework service.
Consume Framework Using Drag And Drop
Go to the
MyFramework project > Right Click on
MyFramework.framework under the Products folder > Click on Show in Finder . This will redirect the location of the framework
Now open Consumer app and drag and drop framework to the project . Make sure to check Copy items if needed so that the files actually copy into the new project instead of just adding a reference as shown in Figure 6
As shown in Figure 7 after drop the framework into project. It added to the Linked Frameworks and Libraries section. Remove it from this section by selecting the framework and tap minus sign
Now tap on plus sign on Embedded Binaries and add framework as shown in Figure 8
As shown in Figure 9
MyFramework.framework now added. Adding framework on the
Embedded Binaries section will add it to the
Linked Frameworks and Libraries section as well
As shown in Figure 10 we consumed our framework by doing following steps
- First we
import MyFramework. We can’t access Framework api without importing it as we do to access Foundation API
- We used
APIRequestLoaderclass api in the framework with the data needed by the framework. It will perform network call and decode data in the model
(MyGitHub)as provided when initializing the class.
Consume Framework Using Local CocoaPods
CocoaPods is a popular dependency manager for iOS projects. It’s a tool for managing and versioning dependencies.
First install the cocoapods by follow the instruction in the link
Open to the downloaded project folder as shown in Figure 11
Open terminal and go to the root of the
MyFramework folder as shown in Figure 12 and run the following command
pod spec create MyFramework
MyFramework.podspec should created. Open file in any text editor. Replace it with the code as shown in Figure 13 and save it.
A pod specification describes a version of Pod library. It includes details about where the source should be fetched from, what files to use, the build settings to apply, and other general metadata such as its name, version, and description.
name → The name of the Pod
version → The version of the Pod
platform → A specification should indicate the platform and the correspondent deployment targets on which the library is supported.
source → The location from where the library should be retrieved.
At this point our framework now configured. Now open Consumer app project and delete the framework we added in previous section. Right click on the framework > Select Delete > Move to Trash as shown in Figure 14. Build the project you will see the error
No such module ‘MyFramework’. In the next step we will fix this by installing our framework locally using pods
Open terminal and do to the root folder of Consumer app and run following code. This will create a Podfile as shown in Figure 15. Follow this tutorial if you never worked with pod
podfile code with the following as shown in Figure 16. In this file we are saying that Consumer app needs
MyFramework as a dependency for this project and we gave local path where to find the dependency.
Open terminal > Go to the root folder of Consumer app (Same level in which podfile was created) > Run the following command as shown in Figure 17
At this point our dependency which is MyFramework now configured. First close the Consumer app and open
Consumer.xcworkspace. Run the project and now our application will work without any error.
With this command, you’re searching the CocoaPods repository and downloading any new or updated pods that match the Podfile criteria. It also resolves any dependencies, updates the Xcode project files so it knows how to build and link the pods, and performs any other required configuration.
As shown in Figure 18, our dependency now added you can see the source code as well and framework extension file
At this point we setup local repository of our framework. Now open
MyFramework project and add new method which will return hard coded data as shown in Figure 19. Build
MyFramework you will see the Build Succeeded
Consumer.xcworkspace. You will see the you can access this method now as shown in Figure 20
Distribute Framework Using Remote Repository
This section walks you through publishing your pod to GitHub and using it like a third party framework.
Created MyFramework repository on github as shown in Figure 21
Clone the project to some empty folder
MyFramework project contents into the clone repository folder
Before commit and push the code. First replace the
MyFramework.podspec code with the following. We replace source url from local to remote. Now we are telling podspec the location from where the library should be retrieved as shown in Figure 24
As shown in Figure 25 we pushed the code to the repository we created
We pushed the tag as shown in Figure 26 and 27. As we specified in
Open the Consumer app
podfile and replace the pod (dependency) url from local to remote.
Open terminal and go to the root folder of Consumer app directory and run the following code
Now as shown in Figure 29 it is fetching from remote git repo with the tag specified in podfile
In the next part we will see how to configure cocoapods for the library that requires other library as a dependency