Build your own Cocoa Touch Frameworks, in pure Swift

Haroon Baig
5 min readFeb 19, 2015

--

If you are an experienced iDeveloper, you’d have probably heard of a term called Frameworks, more specifically, the ‘Dynamically Linked Frameworks’. Now this is the most intimidating term for most of the folks out there but believe me, its insanely simple to build one. Bear with me. The only thing that you have to remember, is that Apple now allow developers to make dynamically linked Cocoa Touch Frameworks for iOS, to package up your code in a most efficient way. Thats it! (I’m not going into much of the details though, its a pretty deep topic)

Well, I was developing a nice and clean wrapper around the CoreMotion Framework, called the MotionKit, which allows you to retrieve the raw data from the Accelerometer, Gyroscope and Magnetometer in just a few lines of code. I kept it completely open sourced from the day first, and soon I started receiving hunderds of emails from many of the fellow Developers out there, asking for the licensing scheme of MotionKit, so that they could leverage the power of sensors into their apps while making them more engaging and enhancing the overall user experience. I didn’t know this wrapper will gain so much of traction, so I decided to distribute it through CocoaPods and Carthage but wait a minute! Carthage only supports Dynamic Frameworks and CocoaPods hate swift (at the time I’m writing this article). So, the only choice left, was to make a Cocoa Touch Framework, and distribute it through Pods and Carthage.

It was very intimidating when I thought about the distribution of MotionKit through CocoaPods and Carthage, because of the fact that it was purely written in Swift. But, it came out that it was really easy to do so, at least for me. Lets have a look.

A simple Swift file

Its very obvious that you need to have some kind of functionality built right into the Framework that you wanna build. So I’ll start with just a simple Swift file that I made. A very simple one.

class SwiftFrameworks {
init (){
println("Class has been initialised")
}

fun doSomething(){
println("Yeah, it works")
}
}

Long story short, to make it work seamlessly with your Framework, you have to make every method and class initialisers public. Simple enough. The above swift file would then look something like this.

public class SwiftFrameworks {
public init (){
println("Class has been initialised")
}

public fun doSomething(){
println("Yeah, it works")
}
}

Lets name that file, ‘MotionKit.swift’ (Please don’t mind my naming conventions, I’m made that way).

Now head over to the Xcode, and create a new CocoaTouch Framework. I’ll name it MotionKit. Just launch the Xcode ► Start New Project ► Under the iOS, select Framework & Library ► Select Cocoa Touch Framework ► Name it and Press Enter.

Xcode would then give you a template with a boilerplate and guess what? You’re now ready! Your screen should look like the screenshot below. (Note: I named my Framework ‘MotionKit’, yours could be different)

Now you just have to do one, and only one thing, and that is, click on the small + button on the lower left corner of the screen and select ‘Add New Files’, then select that Swift file that we previously created (Or any other file that you want to include in your Framework), press CMD + B and and and, we’re done now.

Test your newly developed Framework

Now, its the time to add that framework to any of your projects to test its functionality. Right click on the ‘MotionKit.framework’ and select ‘Show in Finder’. (The name might be different in your case, like [Whatever.framework] etc).

After that, a window will appear, where your frameworks would be listed in an order.

Now click and drag this .framework file into any of the projects that you want. In my case, I’d drag into my own project, which is MotionKit. Lets quickly do that. Your screen will appear somewhat like this.

In your case, the icon would be like a briefcase (a slight problem on my side though). Having that done, select your parent project and link these binaries to it, like you can see that I added ‘MotionKit.framework’ in the screenshot below.

Now build your app by pressing CMD + B. If it succeeded, that means everything worked out corectly. Now you can import this Framework into your projects just like the regular Frameworks like UIKit and Foundation. In our case, or in my case, I’d do it like this.

import MotionKit //The public class included in your frameworkpublic class ExampleToUseFrameworks{
public init (){
let motionKit = MotionKit()
motionKit.doSomething()
}
}

This is all what it takes to build a very nice Dynamic Framework in Pure Swift. Ping me if you have any Queries, I’d love to hear from you. Till then, Happy coding.

Please don’t forget to visit my repository, the MotionKit. Feel free to fork it, give it a star, clone it or send me a pull request. I’d really appreciate and love your suggestions.

--

--

Haroon Baig

👋 I help companies in the crypto space produce better, insightful, & engaging content that people LOVE! Everything from ECDSA to zk-SNARKs, Bitcoin to Polkadot