How To Create A Static Library Or Framework For IOS?

IOS App  new 0912
Social sharing

A static library is a package of classes, functions, definitions and resources, which can be packed together and easily shared among projects. This facilitates reuse of code easier as you will have your own set of classes and utility functions.

Here are the steps to create your static library or framework

Contents

Step 1 : Create A New ‘Cocoa Touch Static Library’ Project

  • Choose template as Cocoa Touch Static Library.
  • Start with create new project >> iOS >> Framework & Library >> Cocoa Touch Static Library

choose_template_for_project

Note: The name you entered in product field will be the name of your framework.
Example:  ‘MyCompany’ will generate ‘MyCompany.framework’

Step 2: Create The Primary Framework Header (Recommended)

Usually, framework header is imported this way – <MyCompany/MyCompany.h>

  • Create an ‘.h’ file – ‘MyCompany.h
  • If X-Code creates it for you, just delete the ‘.m’ file and import the inner classes used in ‘.h’ file
  • If you import only ‘MyCompany.h’ (#import <MyCompany/MyCompany.h>) then the rest of the files need not be imported
#import <Foundation/Foundation.h>
  • Now, add your new sources
  • Make the header ‘Public’

Public headers are copied to the .framework and can be imported using the framework

  • Select the header in the project explorer, then expand the Utilities pane (Cmd+Option+0) to modify the scope of header.
  • In the ‘Target Membership’ group select the checkbox next to the ‘.h’ file.
  • Then, change the scope of the header from ‘Project’ to ‘Public’.

You might have to uncheck and check the box to get the dropdown list. This will ensure that the header is copied to the correct location in the copy headers phase.

Step 3: Update The Public Headers Location

  • To avoid copying private headers to the framework check that the public headers are copied to a separate directory, e.g. $(PROJECT_NAME)Headers.
  • Select the project in the file explorer >> select the targets >> ‘Build Settings’ tab.
  • Set ‘Public Headers Folder Path’ to ‘$(PROJECT_NAME)Headers’ for all configurations by searching the ‘Public Headers Folder Path’. This folder must be unique if you are working with multiple Frameworks.

headers_xcode

 

 

 

 

Step 4: Setup ‘Build Settings’

"Dead Code Stripping" => No (for all settings)
"Strip Debug Symbols During Copy" => No (for all settings)
"Strip Style" => Non-Global Symbols (for all settings)

Step 5: Make Sure That Framework Is Used As Dependent Target

  • Generate the basic skeleton of the framework in the static library target. Also include a simple post-build script.
  • Select your target in the file navigator and click the ‘Build Phases’ tab
  • Then, click ‘Add Build Phase’ >> ‘Add Run Script’ and paste the following script in the source portion of the run script build phase.
set–e
mkdir -p "${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.framework/Versions/A/Headers"
# Link the "Current" version to "A"
/bin/ln -sfh A "${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.framework/Versions/Current"
/bin/ln -sfh Versions/Current/Headers "${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.framework/Headers"
/bin/ln -sfh "Versions/Current/${PRODUCT_NAME}" "${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.framework/${PRODUCT_NAME}"
# The -a ensures that the headers maintain the source modification date so that we don't constantly
# cause propagating rebuilds of files that import these headers.
/bin/cp -a "${TARGET_BUILD_DIR}/${PUBLIC_HEADERS_FOLDER_PATH}/" "${BUILT_PRODUCTS_DIR}/${PRODUCT_NAME}.framework/Versions/A/Headers"
mycompany_xcode
  • Build your project

The build products directory is usually in-
(~/Library/Developer/Xcode/DerivedData/<ProjectName>-<gibberish>/Build/Products/…)

You will find the ‘libMyCompany.a’ static library, a headers folder, and a ‘MyComapny.framework’ folder which contains the basic skeleton of your framework.

build_product_directory

SEE ALSO: How to make Static Framework iOS device independent?

I hope you enjoyed this topic, if you have any questions or comments please share below!

Your recently viewed posts:

Jayadev Das - Post Author

Do what you do best in – that’s what I’ve always believed in and that’s what I preach. Over the past 25+ years (yup that’s my expertise ‘n’ experience in the Information Technology domain), I’ve been consulting to small, medium and large companies ‘About Web Technologies, Mobile Future as well as on the good-and-bad of tech. Blogger, International Business Advisor, Web Technology Expert, Sales Guru, Startup Mentor, Insurance Sales Portal Expert & a Tennis Player. And top of all – a complete family man!

    Contact Us

    We’d love to help & work with you




    When do you want to start ?


    Enter your email address to stay up to date with the latest news.
    Holler Box

    Orange Exit pop up

    Subscribe for the latest
    trends in web and
    mobile app development
    Holler Box

    Exit pop up

    Sad to see you leaving early...

    From "Aha" to "Oh shit" we are sharing everything on our journey.
    Enter your email address to stay up to date with the latest news.
    Holler Box