iOS

Create framework in iOS8

Kundan

11 Jun 2015

Create framework in iOS8

Creating a framework seems to be a tricky job in earlier version of iOS. I got an opportunity to work on framework creation and faced some issues which takes a lot of time to resolve. So, I thought of writing a tutorial on how to create a framework.

In iOS 8 framework creation seems to be quiet easy.

Let’s start open your Xcode –> New Project –> In left panel(select Framework & library under iOS section) –> Select Cocoa Touch Framework –> Click Next (Check Below Image)

img1

Then give a Product Name and click Next. (Check Below Image)

img2

Click on next and then click on create. Now your framework file has been created.

Next now lets add some file. New file — > select (iOS and then source in left panel) and then select Cocoa Touch class –> Click Next –> Give a class name and subclass of NSObject(select from dropdown) –> Click Next. (check below image).

img3

Now click create. You can see NSObject class got created. I am going to teach you to just print(NSLog) using framework. If you want other things that your framework will do. You can code for it.

Go to Your NSObject class .h file and add  “-(void)printLog;”  Below @interface.

Now go to .m file and put this code below @implementation.

-(void)printLog

{

NSLog(@”This is a Framework creation test”);

}

We have added this log statement such that whenever this framework gets added to other project and printLog method gets called, This will log “This is a Framework creation test”.

Now Some settings.

Click on ProjectName –> Click on build settings –> Set “NO” to “Build Active Architecture only”.

Now go to Build Phases.

Click on Headers –> You can see 3 sections (Public, Private and Project).There will be one .h file in Project, Drag and drop that .h file into public Section. This will make your framework file public and accessible from another class.

Now build your project. you can see that the red color framework in product folder of your project will turn black.

Now go there and click to show in finder (See below image).

img4

There is your current created framework. That’s much you have to do to create the framework. Now drag and drop that framework to your project and use it.

Let’s create a new project. Now drag and drop your framework there. Now in viewcontroller.m, import the framework header like this  “#import ” and

also, import the NSObject class included in the framework like this

 “#import ”.

Now go to viewdidload and create an instance of your NSObject class and call the framework method like this

FrameworkFunction *frameWork = [[FrameworkFunction alloc]init];

[frameWork printLog];

Now you can see that its printing the value and  basically your framework is working. 🙂

So, i think now you guys/gals understand how to create a framework in iOS. But have to add few things here. If you build your framework for device it will run only on device and if you build your framework for simulator it will run only in simulator. So, the question is to make it Universal. If the framework is universal it will work with both. To make your Framework universal :-

Go to prooject build scheme –> Select Edit Scheme from Drag drop menu (See the pic Below).

img5

Now this screen will come up:-

img6

Now go to Archive –> Post-actions and click on the bottom left add(+) button.

img7

Click on New Run Script Action.

img8

Now click on “Provide Build setting from” and select your project from drop down menu(See Below image).

img9

Next, you have to add an Archive post-action script in script section, Copy and paste the below Script there.

set -e
 
 
 
 DEVICE_BIN="${OBJROOT}/UninstalledProducts/${TARGET_NAME}.framework"
 
 SIMULATOR_BIN="${SYMROOT}/../../../../Products/Debug-iphonesimulator/${TARGET_NAME}.framework"
 
 
 
 ARCHIVE_PATH="${SRCROOT}/_Archive"
 
 rm -rf "${ARCHIVE_PATH}"
 
 mkdir "${ARCHIVE_PATH}"
 
 
 
 if [ "${CONFIGURATION}" = "Release" ]; then
 
 
 
 if [ -d "${DEVICE_BIN}" ]; then
 
 DEVICE_PATH="${ARCHIVE_PATH}/Release"
 
 mkdir "${DEVICE_PATH}"
 
 cp -r "${DEVICE_BIN}" "${DEVICE_PATH}"
 
 fi
 
 if [ -d "${SIMULATOR_BIN}" ]; then
 
 SIMULATOR_PATH="${ARCHIVE_PATH}/Debug"
 
 mkdir "${SIMULATOR_PATH}"
 
 cp -r "${DEVICE_BIN}" "${SIMULATOR_PATH}"
 
 lipo -create "${DEVICE_BIN}/${TARGET_NAME}" "${SIMULATOR_BIN}/${TARGET_NAME}" -output "${SIMULATOR_PATH}/${TARGET_NAME}.framework/${TARGET_NAME}"
 
 
 
 fi
 
 fi
 
 exit 0;
 
 
 
 Script courtesy:- https://kodmunki.wordpress.com/2015/03/04/cocoa-touch-frameworks-for-ios8-remix/comment-page-1/#comment-111
 
 
 
 And after putting the script there, click on close button at right corner.
 
 
 
 After that go to build settings and add "$(PROJECT_DIR)/lib/$(CONFIGURATION)" to Framework Search paths,Runpath Search paths in project target and test target.
 
 
 
 Now Build the project for simulator(any simulator device you can choose) and then Archive the project for iOS device.
 
 
 
 Now you can see an _Archive folder has got created in your project folder. Open that, you can find 2 folders there Debug and Release.Open Debug folder and you can see your framework there.
 
 
 
 Use this framework. This is an Universal framework which will work for both Device as well as Simulator.
 
 
 
 Nice to know :- 
 
 
 
 1) If you see apple Framework like "#import ". They only include a single header and all classes in the framework get called.To do so,Follow these steps :-
 
 
 
 Go to header file of your class (.h file).

img10

Now in this header class #import all the other classes header needed.
 
 That's all.
 
 
 
 2)If you see parse, crashlytics. you can see they call methods like this :-

[Crashlytics startWithAPIKey:];
 
 
 
 You can also do it with your project. Here Crashlytics is the class name and startWithAPIKey is a class method(method with + sign, to be more precise).

Till then Enjoy. :)