Create a dynamic framework for iOS in Swift

Have you ever written some Swift code in iOS that you’ve thought, this could be way useful in other projects? One solution is to set up a Cocoa Touch framework – edit your source code in one place, compile your framework and it is automatically updated in all of your projects. Awesome! We’re going to do just that in this post.

But then perhaps you would like to share this amazing framework with the world? Well, why don’t you make a CocoaPod out of it and upload it to the master repository at cocoapods.org? The next post will turning your framework into a CocoaPod.

Create a Cocoa Touch dynamic framework

First step is to create a Cocoa Touch framework. Previously, developers were only able to create static libraries for iOS – since Xcode 6 we are now able to create dynamic frameworks, a much more straight forward way of creating a framework.

Have you ever been working in SpriteKit and wished you had access to some great easing functions? (think Robert Penner, Greensock, etc) I migrated an old Objective C framework to Swift some time ago that does just that, and I think it’s a great candidate to turn into a dynamic framework and upload to CocoaPods. So join me on the journey! Here are the steps you need to take.

First of all – you’ll need to create a project and select Cocoa Touch Framework. Screenshot 2016-02-10 10.14.34.png

Be sure to select Create Git Repository on My Mac to make it easy to upload this project to Github.

Add the Swift files from the project you want to convert to a framework. You need to convert your classes and functions to have public access.

Now – if you just want to upload the code to CocoaPods, that’s it for building your dynamic framework. If you want to use the built frameworks there’s more to do.

Unfortunately Apple hasn’t yet resolved some issues with Xcode and universal frameworks. The best resource I’ve found for resolving this(until Apple does!) is here. Follow the steps for “Creating a Framework”. The process seems to change every time Apple updates Xcode so make sure you’re reading the latest version of the blog post. It’s a shame we need to go through this laborious process, but that’s how it is for the moment.

If you follow the steps correctly you should have a framework for Debugging and Release in your project’s _Archive folder.

When you’re done, test the framework by following the steps in the same post for “Adding Frameworks to an App.”

When you’re done, push your project up to a repo on Github. You can find my SpriteKitEasingSwift project at this point on Github here.

In the next post, I’m going to upload this repo to CocoaPods.

Interactive developer, Author - iOS development with Swift - book coming 2017 https://manning.com/books/ios-development-with-swift

Tagged with:
Posted in Swift
One comment on “Create a dynamic framework for iOS in Swift

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: