Swift Framework — Ch: 1 . Develop a Swift Framework

Swift Frameworks are Just Awesome!

Find this chapter in Spanish by Daniel Isaac.

WORE — Write Once Run Everywhere, Is now used for Swift along with Java.

Indeed Swift became a popular programming language and evolving day-by-day.

Developing a Swift Framework has it’s own Advantages as follows.

  1. Hide your code implementation.
  2. As .framework files are already compiled, it will reduce recompilation and save your time while developing.
  3. It is what Apple uses, Ex: UIKit.framework.

So what are you looking for?, let’s start developing your own Swift framework now.

Tools used : XCode 9.2+, Swift 4+

Step 1:- Setup Framework Project

  • Create new XCode project.
  • Click on Cocoa Touch Framework
Choose Cocoa Touch Framework
  • Name your project, we have used AlamoWater sounds similar to Alamofire. (Make sure to choose language — Swift)
  • Click AlamoWater folder inside your target and press ⌘ + N.
  • Choose File Name AlamoWater and Subclass of: NSObject

Step 2:- Write some Code

Add following code inside AlameWater.swift.

Note:- Make sure your class and methods are open.

open class AlamoWater: NSObject {
public static let shared = AlamoWater()

   open func hello(){
      debugPrint("Hello from AlamoWater!")
   }
}

Step 3:- To create framework for both iOS Device and Simulator add new Aggregate target to your project.

Add Aggregate target
  • And add Run Script to it.
  • Note:- In Run Script, Make sure to change AlamoWater with your project name.
#!/bin/sh
UNIVERSAL_OUTPUTFOLDER=${BUILD_DIR}/${CONFIGURATION}-universal
# make sure the output directory exists
mkdir -p "${UNIVERSAL_OUTPUTFOLDER}"
# Step 1. Build Device and Simulator versions
xcodebuild -target "AlamoWater" ONLY_ACTIVE_ARCH=NO -configuration ${CONFIGURATION} -sdk iphoneos  BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}" clean build
xcodebuild -target "AlamoWater" -configuration ${CONFIGURATION} -sdk iphonesimulator ONLY_ACTIVE_ARCH=NO 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/AlamoWater.framework" "${UNIVERSAL_OUTPUTFOLDER}/"
# Step 3. Copy Swift modules from iphonesimulator build (if it exists) to the copied framework directory
SIMULATOR_SWIFT_MODULES_DIR="${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/AlamoWater.framework/Modules/AlamoWater.swiftmodule/."
if [ -d "${SIMULATOR_SWIFT_MODULES_DIR}" ]; then
cp -R "${SIMULATOR_SWIFT_MODULES_DIR}" "${UNIVERSAL_OUTPUTFOLDER}/AlamoWater.framework/Modules/AlamoWater.swiftmodule"
fi
# Step 4. Create universal binary file using lipo and place the combined executable in the copied framework directory
lipo -create -output "${UNIVERSAL_OUTPUTFOLDER}/AlamoWater.framework/AlamoWater" "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/AlamoWater.framework/AlamoWater" "${BUILD_DIR}/${CONFIGURATION}-iphoneos/AlamoWater.framework/AlamoWater"
# Step 5. Convenience step to copy the framework to the project's directory
cp -R "${UNIVERSAL_OUTPUTFOLDER}/AlamoWater.framework" "${PROJECT_DIR}"
# Step 6. Convenience step to open the project's directory in Finder
open "${PROJECT_DIR}"

Step 4:- Build your first framework by Choosing UniversalAlamoWater target pressing ⌘ + B

After build completed,You will see Finder opening with Your Framework in that.

Happy Happy :)

Step 5:- Add this framework to your any project and use it.

import UIKit
import AlamoWater
class ViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        AlamoWater.shared.hello()
    }
}

Note:- If you are getting following error,

… Reason: image not found

Make sure your framework is added in both Embedded Binaries and Linked Frameworks and Libraries

In case you are getting any other issues contact me directly.

Get Full SourceCode on Ch: 1 — GitHub.


What’s next?
Swift Framework — Ch: 2. Using third party framework inside custom Swift framework

Share it if you liked it. 📢

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.