How to build an iOS framework

19 Apr 2020

In this post, you’ll learn how to create an iOS framework written by swift and Objective-C, adding bundle resources, and how to integrate the framework to your project. sample code

Initialize an empty Framework

Create a framework

Create a new project, and choose Framework

Let me choose swift


So I have this project here

Create a resource bundle

File->New->Target, choose macOS, search for bundle

select this bundle, go to Build Setting->Base SDK, set the value to iOS

Select the framework->Build Phases->Dependencies, add bundle


Now this project is ready to used, let’s integrate this framework to an iOS project. And write some codes.

Integrate with project

Create a new project


Drag the framework project to the new created project.


Select your project, add this demo framework
Go to Build Phases->Copy Bundle Resources, drag the bundle to the resources list.

All set let’s write some codes.

I will just write some simple code here, a viewcontroll with a xib, and add an image xcassets.

Because apple woundn’t accept framework with resources inside, so we need to move any resource files that is not code file to the resource bundle.

Here’s my xib for ToolViewController.

Now I will import my framework, and try to use the ToolViewController, but it seems XCode can’t find this class. This is cause by the access control. The default one is internal, let’s set ToolViewController to be public. And try again.


Now I can run the project without error, but when I click setImage. Nothing happen, it seems I can’t find the image with this line.

imageView2.image = UIImage(named: "peisongcheSVG")

Because with UIImage(named: "imageName"), it will just search for image inside our main bundle, apparently, my image is not inside the main bundle

Set the right bundle, and we’ll found the image.

Code with Objective-C

What if I want to write with Objective-C.

Write something

Import header files

And yet can’t find the class

You need to set the header file to be public, the default value is project

Now it work.

Export and add framework to another project



I will just copy MyFrameWorkDemo and remove the reference to Framework project.

Open the framework project, drag the framework and bundle to the new project. Then run the new project.

You will probably get this error.

Select you project and set the framework Embed to Embeded&Sign. Everything will work just like before.

Add the framework project to workspace

I just show you how to develop a framework and integrate to a project. But what if your project need to use CocoaPod, or your framework need to use CocoaPod.


I will make a copy of my demo project and my framework project.


Create a workspace.


Drag the demo project and the framework project into the workspace


Same as before, drag the framework


Drag the bundle. And everything will work fine. But look at the path of the bundle, if I give my project to you, you probably can’t run the project without error. This is not the elegant way.

Copy bundle when building

Now I will remove this bundle here. And add a run script.

yes | cp -R -L -fi "${BUILT_PRODUCTS_DIR}/MyFrameworkBundle.bundle" "${BUILT_PRODUCTS_DIR}/${CONTENTS_FOLDER_PATH}/"

Now try again, everything should work.

Add other frameworks through Cocoapod


Open your project in terminal, and try pod init I will add SDWebImage, so inside my podfile I write this

platform :ios, '9.0'
use_frameworks!
# remove warning [!] [Xcodeproj] Generated duplicate UUIDs
install!'cocoapods',:deterministic_uuids=>false

# Add specific workspace if you have multi sub projects
workspace 'MyFrameWorkDemo.xcworkspace'

# reuse pods
def pods
  pod 'SDWebImage', '~> 5.0'
end

target 'SampleFramework' do
  # find the project
  project '../SampleFrameworkForPod/SampleFramework.xcodeproj'
  pods
end

target 'MyFrameWorkDemo' do
  pods
end


run pod install, close your project and open again, you will find Pod is integrate to you workspace.

import and try the SDWebImage inside my framework

So far so good. How about create my owe cocoapod and share my framework?

Create your Cocoapod

I will make another copy of the demo project and the framework project.

Now go to SampleFrameworkForPod and create a pod config file

pod spec create SampleFramework


You will get this file *.podspec, open it and config you project. For more reference Podspec Syntax Reference

Open your project and remove all the config that relate to last pod setting. And the link to framework.

Create a new pod file inside the new podfile, just

 pod "SampleFramework", { :path => "../SampleFrameworkForPod"}


pod install and open the new generate MyFrameWorkDemo.xcworkspace


You should run the project without error, but wait


When you click jump to framework, you will see this error.


The bundle is added to different location, you need to update you bundle path just like the code above.


Remove this two files and pod install again, or the code udpate wouldn’t work. Then run the project again, it should run without error.


Reference: