Tutorial: Open Source on iOS (Part 3): Creating Pseudo-Frameworks

On Mac OS X frameworks provide a convenient way to package the dynamic libraries and header files into a single directory structure making it easier to include them in Xcode projects. Apple does not allow dynamic libraries (other than ones they provide) to be used with iOS so it may appear the third-party frameworks are not possible, however it is possible to create a pseudo-framework using a static library giving many of benefits of a real framework. In this tutorial you will combine the static libraries you created in the previous tutorial in a single pseudo-framework that will contain binaries for both the device and simulator.

Creating Ogg.framework

First ensure that you are in the directory where you built libogg.

 
cd $IDZ_BUILD_ROOT/libogg/1.3.0

Now make a directory to hold the framework files.

mkdir Ogg.framework

Copy the header file from one of the install directories to the Headers sub-directory within the framework.

cp -R install-iPhoneSimulator-i386/include/ogg Ogg.framework/Headers

Finally create a fat static library in the framework directory with the same name as the framework

lipo -create -output Ogg.framework/Ogg \
-arch i386 install-iPhoneSimulator-i386/lib/libogg.a \
-arch armv7 install-iPhoneOS-armv7/lib/libogg.a

A script for creating frameworks idz_fw

The steps in the above section can be generalized and made into a script. Using a text editor create the file $IDZ_BUILD_ROOT/bin/idz_fw with the following contents:

#!/bin/bash
#
# Script to create a pseudo-framework
#
idz_usage()
{
  IDZ_SCRIPT_NAME=`basename $0`
  echo "Usage: $IDZ_SCRIPT_NAME <name> <lib_name> <header_dir>"  
  exit 1
}

IDZ_NAME=$1 # Name of the framework (e.g. Ogg)
IDZ_LIBNAME=$2 # Name of .a file (e.g. libogg.a)
IDZ_HEADERS_SRC=$3 # Name of the directory to copy to Headers

IDZ_FW=$IDZ_NAME.framework
IDZ_HEADERS=$IDZ_FW/Headers

if [ -e $IDZ_FW ] ; then
  echo "Backing up existing framework to $IDZ_FW.bak"
  mv $IDZ_FW $IDZ_FW.bak
fi
mkdir -p $IDZ_FW
cp -R $IDZ_HEADERS_SRC $IDZ_HEADERS

for IDZ_ARCH in i386 armv6 armv7 armv7s; do
  case $IDZ_ARCH in
    i386 )
      IDZ_PLATFORM=iPhoneSimulator
      ;;
    armv6 | armv7 | armv7s )
      IDZ_PLATFORM=iPhoneOS
      ;;
    * )
      echo "Unrecognised architecture $IDZ_ARCH"
      idz_usage
      ;;
  esac
  IDZ_INSTALL_DIR=install-$IDZ_PLATFORM-$IDZ_ARCH
  IDZ_LIB=$IDZ_INSTALL_DIR/lib/$IDZ_LIBNAME
  if [ -e $IDZ_LIB ] ; then
    IDZ_LIPO_ARCH="$IDZ_LIPO_ARCH -arch $IDZ_ARCH $IDZ_LIB"
  fi
done

lipo -create -output $IDZ_FW/$IDZ_NAME $IDZ_LIPO_ARCH

Don’t forget to make it executable.

The arguments to this script are the framework name without the .framework extension, the library name and directory containing the include files to be copied to Headers. The script assumes the build naming convention used in the previous tutorial.

Using this script all the steps of the previous section can accomplished with the command:

idz_fw Ogg libogg.a install-iPhoneSimulator/include/ogg

The Ogg.framework is now ready to use, however without a codec it is not all that interesting. In the next tutorial you will use the scripts developed so far to compile libvorbis an open source audio codec.


About idz

A professional software engineer dabbling in iOS app development.
This entry was posted in Tutorial. Bookmark the permalink.

Leave a Reply