5 min read

With the advent of iOS 8, Apple allowed the option of creating dynamic frameworks. In this post, you will learn how to create a dynamic framework from the ground up, and you will use Carthage to add frameworks to your Apps.

Let’s get started!

Creating Xcode project

Open Xcode and create a new project.

Select Frameworks & Library under the iOS menù from the templates and then Cocoa Touch Framework.

Type a name for your framework and select Swift for the language.

Now we will create a framework that helps to store data using NSUserDefaults. We can name it DataStore, which is a generic name, in case we want to expand it in the future to allow for the use of other data stores such as CoreData.

The project is now empty and you have to add your first class, so add a new Swift file and name it DataStore, like the framework name.

You need to create the class:

public enum DataStoreType {
    case UserDefaults
}

public class DataStore {
    private init() {}
    
    public static func save(data: AnyObject, forKey key: String, in store: DataStoreType) {
        switch store {
        case .UserDefaults:
            NSUserDefaults.standardUserDefaults().setObject(data, forKey: key)
        }
    }
    
    public static func read(forKey key: String, in store: DataStoreType) -> AnyObject? {
        switch store {
        case .UserDefaults:
            return NSUserDefaults.standardUserDefaults().objectForKey(key)
        }
    }
    
    public static func delete(forKey key: String, in store: DataStoreType) {
        switch store {
        case .UserDefaults:
            NSUserDefaults.standardUserDefaults().removeObjectForKey(key)
        }
    }
}

Here we have created a DataStoreType enum to allow the expand feature in the future, and the DataStore class with the functions to save, read and delete.

That’s it! You have just created the framework!

How to use the framework

To use the created framework, build it with CMD + B, right-click on the framework in the Products folder in the Xcode project, and click on Show in Finder.

To use it you must drag and dropbox this file in your project. In this case, we will create an example project to show you how to do it.

Add the framework to your App project by adding it in the Embedded Binaries section in the General page of the Xcode project.

Note that if you see it duplicated in the Linked Frameworks and Libraries section, you can remove the first one.

You have just included your framework in the App.

Now we have to use it, so import it (I will import it in the ViewController class for test purposes, but you can include it whenever you want). And let’s use the DataStore framework by saving and reading a String from the NSUserDefaults.

This is the code:

import UIKit
import DataStore

class ViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view, typically from a nib.
        
        DataStore.save("Test", forKey: "Test", in: .UserDefaults)
        
        print(DataStore.read(forKey: "Test", in: .UserDefaults)!)
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
}

Build the App and see the framework do its work! You should see this in the Xcode console:

Test

Now you have created a framework in Swift and you have used it with an App!

Note that the framework created for the iOS Simulator is different from the one created for a device, because is built for a different architetture.

To build a universal framework, you can use Carthage, which is shown in the next section.

Using Carthage

Carthage is a decentralized dependency manager that builds your dependencies and provides you with binary frameworks.

To install it you can download the Carthage.pkg file from GitHub or with Homebrew:

brew update
brew install carthage

Because Carthage is only able to build a framework from Git, we will use Alamofire, a popular HTTP networking library available on GitHub.

Oper the project folder and create a file named Cartfile.

Here is where we will tell Carthage what it has to build and in what version:

github “Alamofire/Alamofire”

We don’t specify a version because this is only a test, but it’s good practice. Here you can see an example, but opening the Terminal App, going into the project folder, and typing:

carthage update

You should see Carthage do some things, but when it has finished, with Finder go to project folder, then Carthage, Build, iOS and there is where the framework is[VP1] .

To add it to the App, we have to do more work than what we have done before.

Drag and drop the framework from the Carthage/Build/iOS folder in the Linked Frameworks and Libraries section on the General setting tab of the Xcode project.

On the Build Phases tab, click on the + icon and choose New Run Script Phase with the following script:

/usr/local/bin/carthage copy-frameworks

Now you can add the paths of the frameworks under Input Files, which in this case is:

$(SRCROOT)/FrameworkTest/Carthage/Build/iOS/Alamofire.framework

This script works around an App Store submission bug triggered by universal binaries and ensures that the necessary bitcode-related files and dSYMs are copied when archiving.

Now you only have to import the frameworks in your Swift file and use it like we did earlier in this post!

Summary

In this post, you learned how to create a custom framework for creating shared code between your apps, along with the creation of a GitHub repository to share your open source framework with the community of developers.

You also learned how to use Carthage for your GitHub repository, or with a popular framework like Alamofire, and how to import it in your Apps.

About The Author

Fabrizio Brancati is a mobile app developer and web developer currently working and living in Milan, Italy, with a passion for innovation and discover new things. He develops with Objective-C for iOS 3 and iPod touch. When Swift came out, he learned it and was so excited that he remade an Objective-C framework available on GitHub in Swift (BFKit / BFKit-Swift). Software development is his driving passion, and he loves when others make use of his software.

LEAVE A REPLY

Please enter your comment!
Please enter your name here