iOS Custom Framework Development

iOS Custom Framework Development

Introduction

Apple defines framework as 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.

Frameworks are bundles that contain a linkable library (usually a dylib) and the associated resources and headers for development. Frameworks are available in two different types, “public” and “private” frameworks. Frameworks have an extension of .framework. Headers, if any, are stored in a subdirectory called Headers.

Use Case

This use case demonstrates the steps for creating a new Framework using iOS Xcode.

What we want to do:

  • Prerequisites
  • Open Xcode
  • Create a new Project
  • Test the Framework

Solution

Prerequisites

  • Download and Install Mavericks Version 10.9.4 OS X:

http://www.apple.com/osx/

  • Download and Install Xcode 5:

https://developer.apple.com/xcode/downloads/

Steps to Create a Project

Step 1: Create new project

Create new project, select Project template as “Utility Application”.

createproject

Step  2: Create Project Name “iFramework”.

project-details

Step  3: Create new target.

target

Choose Xcode menu, File > New > Target.

Step  4: Select “Bundle” option.

bundle

Step  5: Create bundle name “MyAppBundle”.

product-name

Step 6: Remove the files .plist, .pch and .strings

When we navigate to the project navigator, we can find the group for “MyAppBundle”. Remove the .plist, .pch and .strings files of “MyAppBundle” only.

select

Step  7: Select  project head > MyAppBundle

targetselect

Step  8: Change build settings.

Change below list of build settings for “MyAppBundle” target only.

  • Base SDK: Latest OS X. (For e.g. Latest OS X 10.9)
  • Architectures: Standard Architectures
  • Build Active Architecture Only: NO
  • Supported Platforms: iOS
  • Valid Architecture: i386 x86_64
  • OS X Deployment Target: Compiler Default.
  • Code Stripping: NO.
  • Link With Standard Libraries: NO.
  • Mach-O Type: Relocatable Object File.
  • Other Linker Flags (optional change, depends upon your framework)
  • Info.plist File: empty
  • Wrapper Extension: framework

Step 9: Build Phase > Link Binary with Libraries > remove all references.

libraries

Step 10: We have added the source code and resource files under “MyAppBundle” target.

  • Here we will create only class called “Myframework”

Step 11:  Add New Copy Headers Build Phases1

buildphases

Add all header files in the following sections:

  • Public: Headers that other developers must have access to in order to work with our framework. In the final framework product, these headers will be visible even to Xcode.
  • Private: Headers that are not intended for other developers, but is meant to be consulted or for reference. These headers will not be visible to Xcode, yet will be included in the framework folder.
  • Project: Headers which neither the other developers nor Xcode have access to. In reality, these headers will not be placed in the final product. This just instructs the compiler to create our custom framework.

We have added “Myframework.h” to public header.

Add below file to Compile Source section.
Add .m, .cpp, .mm,.c extension files goes to this section for compilation.

In our case it will be “Myframework.m” file.

myframework

If our Framework includes images, sounds, nibs and other resources, we include them under “Build Phase > Copy Bundle Resources”.

Step 12: Build configuration

Navigate from main menu,

  • Xcode Product > Manage Schemes
  • Select “MyAppBundle”
  • Click edit
  • Go to “Run” Section
  • Change build configuration to “Release”

Step 13:  Check below build settings in your bundle

  • Precompile Prefix Header: NO
  • Prefix Header: empty
  • Generate Debug Symbols: NO

Step 14: Build Project

  • Select “MyAppBundle” scheme
  • Build project for “iOS device”
  • Build project for “iPhone simulator”

Step 15: Add new target (to Create Universal Framework)

At this juncture, we can see two different frameworks in our project’s release folder, one for Simulator and one for Device. Now, we need to merge both the Frameworks.

Let’s create a new target:

  • Navigate through, Main Menu > File > Target
  • Select “Aggregate” template.
  • Named target as “iUniversal”
    aggregate

Step 16 : Add Run Script to Aggregate target

Add Below, Run script to Aggregate(“iUniversal”) target.

Go to Build Phase > Add Build Phase > Add Run Script

Paste below Script in Shell section.

In Above Script, Replace “<FW_NAME>” with the Framework name, In our case “MyAppBundle”.

Step 17: Build Project with Aggregate scheme.

  • Select Aggregate(“iUniversal”) Scheme
  • Select iOS device
  • Build Project

Step 18: Copy Framework and Test

From Finder Navigate to Build folder of our Project
Under Products folder we can find our merged Framework. Copy this Framework to safe place for future use.

Test the Framework

Let’s test the Framework:

  • Create new project.
  • Go to build phase.
  • Navigate to “Link binary with Libraries”.
  • Select “Add Other”, add your(MyAppBundle.framework) Framework.
  • Go to AppDelegate.h.
  • Import framework header like this (#import <MyAppBundle/Myframework.h>).
  • Now, Create object of Myframework and try accessing methods of class.

Conclusion

In this blog, we have discussed about iOS Custom Framework development with the latest version of Xcode (5.x). By using Custom framework we can create and manage new functionalities for iOS app development. These custom functionalities that have been added into our existing Framework can be used in our applications; for example, creating media player with some special controls.

Thus, if we create Custom Framework we can incorporate the special controls directly into the framework and reuse it everywhere in our iOS app development.

Reference

4395 Views 2 Views Today
  • Bharath Antony

    Its pretty helpful.. Good job

    • http://www.treselle.com/ Treselle Systems Blog

      Thanks for your feedback. Also have a look at our other blog posts.

  • Karthik Rao

    Excellent article. I do have one question though.

    Do I need to submit the framework to apple review before using it in any apps?

  • Avijit

    I already have framework “A” (unix Executable) now i want to create new custom framework “B” in which framework “A”(Already Created) as sub-framework how to do that, when i tried that i am getting error as framework A files not found.