Universal Custom Framework Creation and Using it in Your Project

Rishabh Jain Jul 20th, 2019

In this blog we will go through the creation of custom framework in iOS and how to fix issues regarding exporting of framework. Before iOS 8 we were using static library. The Framework project are same as the other project you create in Xcode, the difference is that you can only compile/build your Framework but you can not actually run them in the same project.
Following are the main issues with static library:

  • From the security point of view, developer can get the source code from the static library.
  • Libraries only has executable code, we can not carry assets along with a static library.
  • Static library do not support swift,it means you can not create a cocoa touch static library using swift. So if you decide to use a Cocoa touch static library, you need to use Objective-C.

Advantages of framework over static library:

  • Framework are more secure than the static library as developers can not decompile the framework or get the source code from framework.
  • Framework is a bundle(Directory structure) that contains libraries as well as sub directories of headers and other resources.
  • Framework are only supported from iOS 8 onwards,where static library, and we can creating framework in both language Objective-C as well as Swift.

Let’s see how to create a framework in Swift:

  • Step 1: Open Xcode , File-> New -> Project. Select Framework and Library and choose cocoa touch framework, then click next
  • Step 2: Now Enter Framework name and click next.
  • Step 3: And choose your project location and create the project.
  • Step 4: Select your project, Choose target -> Project Name -> Select build setting tab. Always “Set Enable Bitcode” to “Yes”, before building a Framework. So far we have created our first framework. Now it’s time to export the framework.
    Issues regarding exporting framework: The platform in which you are creating the framework, its architecture is dependent on that platform. For example: If the framework project is build in “Simulator”,the framework run especially on the simulator, not on the Device. Or if the framework project builds in “Device”, the framework run specially on the Device. If you are an iOS developer,then you want a Universal framework that runs on both simulators as well as Devices. There is a solution for this problem, To avoid this problem we have to create a universal Framework, which can run on both simulator as well as device. You have to follow the following steps to create a universal framework: Build the framework project for both “Simulator” and “Device”

Framework would be generated in the Product folder.

Framework would be generated in the product directory of the project. Which has two separate folders of the unique Frameworks which would run on Simulator and Device independently.

Following is a “Run Script” which generate the Universal Framework for both “simulator” as well as “device”. Select Project Target -> Edit Schema -> Archieve -> Post Action -> Press + -> New Run Script Action.

And paste the following script: UNIVERSAL_OUTPUTFOLDER=/Users/${USER}/Desktop/Frameworks/ # Make sure the output directory exists mkdir -p “${UNIVERSAL_OUTPUTFOLDER}” # Next, work out if we’re in SIM or DEVICE xcodebuild -target “${PROJECT_NAME}” -configuration ${CONFIGURATION} -sdk iphonesimulator ONLY_ACTIVE_ARCH=NO BUILD_DIR=”${BUILD_DIR}” BUILD_ROOT=”${BUILD_ROOT}” clean build xcodebuild -target “${PROJECT_NAME}” ONLY_ACTIVE_ARCH=NO -configuration ${CONFIGURATION} -sdk iphoneos BUILD_DIR=”${BUILD_DIR}” BUILD_ROOT=”${BUILD_ROOT}” clean build

# Step 2. Copy the framework structure (from iphoneos build) to the universal folder cp -R “${BUILD_DIR}/${CONFIGURATION}-iphoneos/${PROJECT_NAME}.framework” “${UNIVERSAL_OUTPUTFOLDER}/”

# Step 3. Copy Swift modules from iphonesimulator build (if it exists) to the copied framework directory BUILD_PRODUCTS=”${SYMROOT}/../../../../Products” cp -R “${BUILD_PRODUCTS}/Debug-iphonesimulator/${PROJECT_NAME}.framework/Modules/${PROJECT_NAME}.swiftmodule/.” “${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/Modules/${PROJECT_NAME}.swiftmodule”

# Step 4. Create universal binary file using lipo and place the combined executable in the copied framework directory lipo -create -output “${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework/${PROJECT_NAME}” “${BUILD_PRODUCTS}/Debug-iphonesimulator/${PROJECT_NAME}.framework/${PROJECT_NAME}” “${BUILD_DIR}/${CONFIGURATION}-iphoneos/${PROJECT_NAME}.framework/${PROJECT_NAME}”

# Step 5. Convenience step to copy the framework to the project’s directory cp -R “${UNIVERSAL_OUTPUTFOLDER}/${PROJECT_NAME}.framework” “${PROJECT_DIR}” # Step 6. Convenience step to open the project’s directory in Finder open “${PROJECT_DIR}” This run script executed after the Archive is complete.And the Universal framework would be generated and opened in project directory itself. So far we have successfully exported the framework we have created, now we will use in our project.

Using framework in your project:Step1: File -> New -> project. Select iOS and choose “Single view application” and click “Next”.

2: Now set all the project dependencies: Include the framework in your newly created project, by drag and drop the framework in your project. While coping the framework in project explorer, check “copy item if needed”.

3: Now select the Project, Choose Target -> Project Name -> Select General -> Scroll to “Embedded Binaries”. Press “+” and add the framework.

After successful integration of the framework in your project you can use it. You can find the sample framework here.

Rishabh Jain - Director, Product Development, Enveu
Rishabh Jain is one of the co-founders and heads the Product Development at Enveu. A tech evangelist and deep tech advocate, he has been working in the Technology space, being part of many Big Data and IoT initiatives for over 10 years.

get a demo today

Take control of your digital media strategy.
Contact us for a no-obligation demo of the Experience
Cloud, tailor-made for you!