Tuesday, August 19, 2014

Creating iOS Framework - Part II

In this part, the plan is to create the framework. Below given is some explicit properties of a framework

- The directory Structure - The framework is having a special directory structure that is recognised by the Xcode. To create this directory structure, we can create a build script. 
- The Slices - Currently, when we build the library, it is only for the current architecture. In order to be a framework useful, it needs to include builds for all the architectures on which it needs to run. 
To do this, we can create a new product which will build the required architectures and place them in the framework. 

The framework has a special directory structure like given below. 

RWUIControls.framework 
- Headers/ 
- RWUIcontrols 
- Versions
Headers 
RWUIknobcontrols.h
RWUIControls.h

RWUIControls
      Current 

Inorder to create a framework, we need to create a script that does it. The script can be added to the library project to the target and the script looks like below
to add the script, move tot he project build phases of the library project and add the Build phase run script phase. 

- The script basically needs to do the following 

- create the framework folder upto the Versions 
- Create the required SimLinks 
- Copy the public headers into the framework 

#ensure if any part of the script fail, then entire script fail 
set -e 
export FRAMEWORK_LOCN="${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.framework"

#create path to the real headers 
mkdir -p "${FRAMEWORK_LOCN}/Versions/A/Headers"

#create required simlinks
/bin/ln -sfh A "${FRAMEWORK_LOCN}/Versions/Current"
/bin/ln -sfh Versions/Current/Headers "${FRAMEWORK_LOCN}/Headers"
/bin/ln -sfh "Versions/Current/${PRODUCT_NAME}" "${FRAMEWORK_LOCN}/PRODUCT_NAME"

#copy the public headers into the framework 
/bin/cp -a "${TARGET_BUILD_DIR}/${PUBLIC_HEADERS_BUILD_PATH}/"  "${FRAMEWOR_LOCN}/Versions/A/Headers"

The symbolic link is nothing but a special kind of file that contains reference to other files or directory in the form of an absolute or relative path and that affects the path resolution 
Once the above steps are over, we can build the target and see in the same products folder we can see the framework is appearing. However, this doesn't have the library file inside. 
To do this bit more steps involved and given below.

Multi Architecture Build

By default when the build is made is Release mode, it will build for 3 device architectures and they are 

arm7 - used in the oldest iOS 7.0 supporting devices
arm7s - as used in iPhone 5 and 5C
arm64 - used in iPhone 5S 

However it will be also useful to build for the remaining two architectures as well for development benefits 

i386 - For 32 bit simulator 
x86_64 Used in 64 bit simulator 

The process of creating the binary for all 5 architectures creates so called fat binary. 

This involves a bit more scripting and below are different phases in it 

Step 1: Create a framework aggregate target . in the library project, create an aggregate target. This can be done by add target -> Others -> Aggregate 
Once the aggregate target is added, it can be named as Framework 

Step2 : Inorder for the library to be built before the framework is created, add the dependancy as the library. 

Step 3: Now add one more build phases with Run script phase and in this script below are the main steps involved 

In the script, there is a build_static_library function which takes an SDK argument and the ONLY_ACTIVE_ARCH to be set to NO. This arguments are for the xcrun xcodebuild command line utility. 
the other important function in this is, make_fat_library. this takes two arguments and combine into one. This uses the lipo command 

xcrun lip -create "${1}" "${2}" -output "{3}" 


Once the fat library is built, we can see the architectures included in this by the below commands

My-MacBook-Pro:vee24.framework username$ cd /Users/username/Desktop/RR/projects/MyProject/docs/VideoChat/SDK/12Sep2014/vee24.framework/
My-MacBook-Pro:vee24.framework username$ xcrun lipo -info vee24
Architectures in the fat file: vee24 are: armv7 arm64

That would print something like below. 
Architectures in the fat file: vee24 are: armv7 arm64


The entire script can be just copy pasted into the run script phase and the RW page is available for future reference is at 

https://dl.dropboxusercontent.com/u/27466522/Per/samplecode/multi_platform_framework_script.rtf


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

No comments:

Post a Comment