Creation of the project for framework

Creation of the project for framework

Step 1

Create New project

Select iOS -> Framework & Library -> Cocoa Touch Framework.

Select project name for eg. i took SampleAlert_9to5iOS

Finish … It will create SampleAlert_9to5iOS.h file


Step 2

Create a new file name it your accordingly for eg i took AlertViewController , it will create AlertViewController.h and AlertViewController.m

Code inside AlertViewController.m below

#import "AlertViewController.h"

@interface AlertViewController ()

+ (UIAlertController *)alertWithQuestion:(NSString *)question;


@implementation AlertViewController

- (void)viewDidLoad {

    [super viewDidLoad];

    // Do any additional setup after loading the view.


- (void)didReceiveMemoryWarning {

    [super didReceiveMemoryWarning];

    // Dispose of any resources that can be recreated.


+ (UIAlertController *)alertWithQuestion:(NSString *)sampleMessage {

    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"9to5iOS"



    UIAlertAction *yesAction = [UIAlertAction actionWithTitle:@"YES" style:UIAlertActionStyleDefault handler:nil];

    UIAlertAction *noAction = [UIAlertAction actionWithTitle:@"NO" style:UIAlertActionStyleDestructive handler:nil];

    [alert addAction:yesAction];

    [alert addAction:noAction];


    return alert;



#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {

    // Get the new view controller using [segue destinationViewController].

    // Pass the selected object to the new view controller.




Step 3

Until now this class is not for public use, so to make it public we change the file in the target to public

and import this class into SampleAlert_9to5iOS.h file

#import <SampleAlert_9to5iOS/AlertViewController.h>

Step 4

Now  create your classes both in Swift and Objective-C according to your need.

Step 5

Now we need to configure our project and make it exportable as a universal framework.

When you build your framework it is going to be build for the architecture that you currently building for, for instance, i386 in case you build for simulator. But we need also x86_64 architectures. So firstly you need to turn this setting in Build Settings to No:


Step 6

After that you have to create new target, that is going to be making this magic of building a universal framework:


Give it a name and hit Finish, i took MyAlertFramework9to5iOS

Then select your newly created target and navigate to Build Phases and add New Run Script Phase to include our little script, that is going to be gluing development and production versions of your framework (by production I mean ability to use it via Simulator).

Step 7

Script is below




# Merge Script

# 1

# Set bash script to exit immediately if any commands fail.

set -e

# 2

# Setup some constants for use later on.


# 3

# If remnants from a previous build exist, delete them.

if [ -d "${SRCROOT}/build" ]; then

rm -rf "${SRCROOT}/build"


# 4

# Build the framework for device and for simulator (using

# all needed architectures).

xcodebuild -target "${FRAMEWORK_NAME}" -configuration Release -arch arm64 -arch armv7 -arch armv7s only_active_arch=no defines_module=yes -sdk "iphoneos"

xcodebuild -target "${FRAMEWORK_NAME}" -configuration Release -arch x86_64 -arch i386 only_active_arch=no defines_module=yes -sdk "iphonesimulator"

# 5

# Remove .framework file if exists on Desktop from previous run.

if [ -d "${HOME}/Desktop/${FRAMEWORK_NAME}.framework" ]; then

rm -rf "${HOME}/Desktop/${FRAMEWORK_NAME}.framework"


# 6

# Copy the device version of framework to Desktop.

cp -r "${SRCROOT}/build/Release-iphoneos/${FRAMEWORK_NAME}.framework" "${HOME}/Desktop/${FRAMEWORK_NAME}.framework"

# 7

# Replace the framework executable within the framework with

# a new version created by merging the device and simulator

# frameworks' executables with lipo.

lipo -create -output "${HOME}/Desktop/${FRAMEWORK_NAME}.framework/${FRAMEWORK_NAME}" "${SRCROOT}/build/Release-iphoneos/${FRAMEWORK_NAME}.framework/${FRAMEWORK_NAME}" "${SRCROOT}/build/Release-iphonesimulator/${FRAMEWORK_NAME}.framework/${FRAMEWORK_NAME}"

# 8

# Copy the Swift module mappings for the simulator into the

# framework.  The device mappings already exist from step 6.

cp -r "${SRCROOT}/build/Release-iphonesimulator/${FRAMEWORK_NAME}.framework/Modules/${FRAMEWORK_NAME}.swiftmodule/" "${HOME}/Desktop/${FRAMEWORK_NAME}.framework/Modules/${FRAMEWORK_NAME}.swiftmodule"

# 9

# Delete the most recent build.

if [ -d "${SRCROOT}/build" ]; then

rm -rf "${SRCROOT}/build"


Step 8

Now is the last step is to run build


That’s it , after run BUILD you will get your framework in your desktop destination.


Add a Comment

Your email address will not be published. Required fields are marked *