Friday, August 15, 2014

Creating framework in iOS - Part 1

There are two main approaches for distributing the logic for other application vendors. 

1. Create Static library => problem with this that the header files are to be distributed explicitly which is an awkward approach. Not very elegant. 
2. Create framework  => This is a way to package a static library and header files into a single 

Trying to create a framework in below route: 

- Build a basic static library project in Xcode
- Build an app with dependancy on this library project 
- Discover how to convert this static library into a framework
- Package other resource files into a framework 

A framework basically is a collection of resources; it collects a static library and its header files into a single structure that Xcode can easily incorporate into a project. 

On OSX, its possible to create a Dynamically Linked framework. Through dynamic linking, frameworks can be updated easily and transparently without requiring applications to relink them. At runtime, a single copy of the library's code is shared among all process using it thus reducing memory usage and improving system performance. This is a powerful stuff. 

On iOS such a custom framework cannot be added in this above manner, so the only dynamically linked frameworks are the ones apple provides 

Due to this, statically linked frameworks are used and it is a convenient way to package up a code-base for re-use in different apps. 
Since framework is nothing really different from a static library, instead it is just a one stop shop for static framework, the first thing first is to create a static library. 

Decided to follow the exact same step as mentioned by raywenderlich.com and do something different later 

Step 1: Create a static library project 
Used the Xcode to create a static library project and the name given was RWUIControls. This created a header and implementation file by default. Sicne the tutorial instructs to delete the .m file with the intention that every control is accessible using this function, and other specific ui controls are imported in this function, the .m file doenst do anything much for this. Deleted from the project. 

Step 2: The next major step is to make the header files publicly accessible. For this, a new build phase needs to be added. The new build phase can be added by the focusing the target and selecting Editor -> Add Build Phase -> Add Copy Header Phase.
When creating this, it added  a build phase and the Copy Header Build phase had Public and Private sections, To the public section, added the RWUIControls.h file. 

Step 3: Including some functionality. The RW had given a knob control implementation file and that is going to be added to this project as a group now. When added the files to the project, it created the group and the header file RWKnobControl.h is added to the public headers section. 

Also since the developer user of this framework will be only referring the RWUIControls.h, it is important that we import the knob control header also into this header file. This way, user of this framework doesn't have to import multiple header files. 

Step 4: This step is to configure the build settings. 
The first step here is to set the path at which the public header files will be copied. For setting this, by selecting the target, search for Public Header in the Build settings tab. Now, set this to incldue/${PROJECT_NAME}. After entering, this changed to incldue/RWUIControls. 

The following are the other settings

Dead Code Stripping : NO
String Debug Symbols during Copy : NO
String Style : Non Global Symbols 

Step 5: 
In the tutorial, there is a way we can make the library that is created as a dependancy project. This is done to make sure the framework development is done using the sample application. For this one, in this step, it creates a dependancy sample application. 

This is fairly not so complex. First we need to create a UI project say for e.g. a Single View application. Now in the build Phases tab, add the RWUIControls library as the target dependancy. Also in the link binary with libraries section, add the library. it may show up in red, but that is okay. This will make sure the library is built before the UI application is compiled.

To note, inorder to have the library appear in the dependancy project listing, the project needs to be copied by checking the "copy items into destination group's folder"

References: 
http://www.raywenderlich.com/65964/create-a-framework-for-ios



No comments:

Post a Comment