Friday, 1 April 2016

Custom iOS Framework with resources


CUSTOM IOS FRAMEWORK With Resorces




Many people asking about how to include resources and facing issues in creating framework with latest xCode 5 by steps mentioned here iOS Framework.

Let's have a walk through for Creating iOS framework with xCode 5 and including resources as well.


Step 1: Create new project



Create new project, select Project template as "Utility Application" (Suppose Project name : "iOSFramework") 



Create New Project
Create New Project

Step 2: Create new target



  • Navigate to main xCode menu, File => New => Target
  • Select "Framework & Library" from left panel and select "Bundle" like shown in below snap!
  • Named something relevant to your framework name, in this tutorial I named it "iOSBundle"


Creating Bundle
Bundle Details

Step 3: Remove files .plist,.pch and language



  • Navigate to project navigator, you can find group for "iOSBundle"
  • Remove .plist, .pch and language files of "iOSBundle" only.
  • Make SURE you are not removing for main project's it must be your bundle which you just created.
  • See below snap!


Removing pList, pch, etc.
Step 4: Configuration changes in build settings of Bundle

Check below list of build settings for "iOSBundle" target only


  • Base SDK: Latest iOS (iOS X.X).
  • Architectures: Standard architectures (armv7, armv7s)
  • Build Active Architecture Only: NO
  • Supported Platforms: iOS
  • Valid Architecture: arm64 armv7 armv7s
  • OS X Deployment Target: Compiler Default.
  • Dead Code Stripping: NO.
  • Link With Standard Libraries: NO.
  • Mach-O Type: Relocatable Object File. 
  • Other Linker Flags [optional change, depends upon your framework]:  -ObjC
  • Info.plist File: empty
  • Wrapper Extension: framework 
Just to make sure you have selected Bundle only, see left panel I have selected "iOSBundle"

Step 5: Build Phase => Link Binary With Libraries => remove all references

Step 6: Develop your framework 


  • Create your classes, headers required for your framework.
  • If you want to use Resource like Images, Audio, Videos, etc. Create Bundle Using This Tutorial, in our case it's "ResourceBundle", drag this bundle into our project "iOSFramework"
  • Test your complete framework with the same project, in our case it's "iOSFramework"
  • In this tutorial we have "Utility" class and also added ResourceBundle.bundle with apple.png and tester.mp3 as resource file, you can add any resource file like plist, database, music, videos etc.
Resource Bundle and Other files




Utility.h




Utility.m




Step 7:  Add Source Code and Resource under your bundle target.



  • Add files to "Compile Sources" section, .m, .cpp, .mm,.c extension files goes to this section for compilation, in our case it's only "Utility.m"
  • Add Bundle file in "Copy Bundle Resources" section, for our tutorial I added "ResourceBundle.bundle"
Compile Sources
Copy Bundle Resources




Add "Copy Headers" build phase from Editor menu
Adding Copy Headers Build Phase
Add all header files in following sections


Public: Headers that other developers must know in order to work with your framework. In the final framework product, these headers will be visible even to Xcode.


Private: Headers that is not necessary to other developers, but is good for consult or for reference. These headers will not be visible to Xcode, but will be in the framework folder.


Project: Headers that the other developers nor Xcode have access. In reality these headers will not be placed in the final product, this is just to instruct the compiler to create your custom framework.


I have added "Utility.h" to public header

Step 8: Build configuration

Navigate from main menu,



  • Product => Scheme => Edit Scheme (or just press Cmd + <)
  • Select "iOSBundle"
  • Go to "Run" Section
  • Change build configuration to "Release"

Build configuration



Step 9:  Configuration changes in Build Settings of your Bundle

Perform below changes in build setting of "iOSBundle" target
  • Precompile Prefix Header: NO
  • Prefix Header: empty
  • Generate Debug Symbols: NO

Step 10: Build Project 
  • Select "iOSBundle" scheme
  • Build project for "iOS device" (⌘-B)
  • Build project for any iPhone simulator (⌘-B)
Build Project

Step 11: Add new target (to Create Universal Framework)
At this moment you can see two different framework in you project's release folder, one for Simulator and one for Device, now we need to merge both framework.

For that let's create one new target,
  • Navigate through, Main Menu => File => Target...
  • Select "Aggregate" template.
  • Named target as "UniversaliOS"

New target for Universal framework
Step 12: Add Run Script to Aggregate target

Add Below, Run script to Aggregate("UniversaliOS") target.
Go to Main Menu => Editor => Add Run Script


Add Run Script Build Phase


Paste below in Shell section.

In Above Script, Replace "<FW_NAME>" with your Framework name, In my case "iOSBundle"

Step 13: Build Project with Aggregate scheme



  • Select Aggregate("UniversaliOS") Scheme 
  • Select iOS device
  • Build Project (⌘-B) 

Compile Universal Framework
Step 14: Copy Framework and Test

From Finder Navigate to Build folder of you Project
Under Products folder you can find your merged Framework

For me path for Framework is below,

/Users/Aniruddh/Library/Developer/Xcode/DerivedData/
iOSFramework-doeqysadgntrrlguuvcivuhapnlr/Build/Products/iOSBundle.framework




PS: If you are using any Resource Bundle, you need to copy it manually, for me I copied "ResourceBundle.bundle" from Release-iphoneos/iOSBundle.framework to above "iOSBundle.framework"

Copy this Framework to safe place for future use.

Let's test the Framework,



  • Create new project
  • Go to build phase
  • Navigate to "Link binary with Libraries"
  • Select "Add Other", add your(iOSBundle.framework) Framework.
  • Add Dependent Frameworks, in our case it's "AVFoundation" as we are playing mp3 file.
  • Go to AppDelegate.h
  • Import framework header like this (#import <iOSBundle/Utility.h>)
  • Now, Create object of Utility and try accessing methods of class

1 comment:

  1. I would be very thankful if you continue with quality what you are serving right now with your blog...I really enjoyed it...and i really appreciate to you for this....its always pleasure to read so....Thanks for sharing!! iphone tips and tricks

    ReplyDelete