How to build an iOS Framework and setup it with Continuous Integration?
Learn how to create an iOS framework and utilize Travis CI to ensure the framework solid.
As your iOS project grows bigger and bigger, you may plan to separate your code by functionality and share some reusable code with other projects or developers. If that’s exactly what you want to do, building a framework is definitely a great choice for you. In this tutorial, I will show you how to use Xcode to build an iOS framework. In addition to that, in order to keep your framework reliable and robust, testing is an essential part. So you will also learn how to setup your framework with continuous integration(CI) tool, Travis CI.
What will you learn?
- How to create a Cocoa Touch Framework in Xcode.
- How to add Unit Tests for your framework.
- How to setup CI for your framework with Travis CI & Github.
At the time of writing, I am using the following Softwares:
- Xcode 10.2
- Swift 5
Getting Started
Launch Xcode, create a new project and select “Cocoa Touch Framework”.
Let’s name our framework SwiftPlayWithString
, and check the box of Include Unit Tests
. Press Next button.
After you hit the create button, this will create a project for you to build a framework. The workspace should look like the following:
The project will have a class with the name as product name. And then In the project navigator, right click on the SwiftPlayWithString
target folder and select New File
.
Choose Swift as a template file, press Next button and name the file as SwiftPlayWithString
Nice job, that’s all how you setup a basic framework from an empty Xcode project!
Connect to Github
Github is one of the best hosting service for open-source projects, and we are going to link our project to it.
Let’s create a new repository SwiftPlayWithStringFramework
in GitHub. Just keep other settings as default and click on Create repository, an empty Github repository is now created.
In next screen, you will see the following information to link our existing Xcode project.
Open the terminal, go to the SwiftPlayWithString Xcode project folder, and type in the following commands:
> git init
> git remote add origin https://github.com/{USERNAME}/SwiftPlayWithStringFramework.git
> git add .
> git commit -m "Initial project setup"
> git push -u origin master
And we have uploaded our source codes to Github currently!
Implement the Framework
Now we are ready to write some codes in our SwiftPlayWithString framework. Our framework is pretty simple. In the SwiftPlayWithString.swift
file, there are two functions that handle string. One is to reverse a string, and the other is to capitalize first letter for a sentence.
Save the file and build the SwiftPlayWithString
target. Just click on the Build
button or use the keyboard shortcut ⌘R Command-R
.
Write Unit Tests
Now we already implemented our SwiftPlayWithString framework. Let’s move on writing unit tests to make sure the framework robust. At first, let us test the reversedString()
method. Here is the test code in theSwiftPlayWithStringTests
file.
Before start testing, click the SwiftPlayWithString Scheme → Click Edit Scheme
In the editing scheme area, select Test (Debug) → select Options → check Gather Coverage for
box → choose some target
→ hit +
button in Targets section → Add SwiftPlayWithString
framework and SwiftPlayWithStringTests
in Targets.
Now we can start testing. Just hit the start (play) icon at the left of class SwiftPlayWithStringTests
, and the test will begin running.
The test case should be successful. Then we switch to the reports navigator and check the code coverage.
As you can see, the code coverage of this test is only 29.4% . The reason is that we only test the reversedString()
function in this testing. So let’s write another test case for the capitalizeFirstLetter()
function as well.
Run the test again and the test should be passed as well. Now our code coverage is improved to 100%! That’s awesome : )
In order to enable our continuous integration later, we need to create new scheme for our test. Click new scheme, select SwiftPlayWithStringTests
as target and add it.
Our unit test is done. Take a deep breath~
Configure Travis CI
It is crucial that every code change to our framework triggers a full execution of test. In other words, whenever we push a new commit to Github, we should test our framework and make sure all test cases are successful. This entire process is what we call Continuous Integration (CI). The main idea is to validate the correctness code changes and to quickly identify integration errors.
Travis CI is a hosted, distributed continuous integration service used to build and test software projects hosted at GitHub
We will leverage Travis CI to help us setup continuous integration and run test cases in this tutorial. Let’s start off signing in Travis CI with your Github account to let Travis sync up your Github account, and enable our SwiftPlayWithStringFramework repository in account settings.
Next, we go back to Github repository to create a new .travis.yml
file in our project folder.
Add script in .travis.yml
to tell Travis CI what to do, and then push this commit to the master branch.
Now you should see a received test request in your Travis account, and it will start running. We are almost there!
Once the build jobs are completed, you can see the test report for the commit you submitted. And our test is passed perfectly!
From now on, when you push a new commit on GitHub, it will automatically execute our test for this commit. If there is a test failed, it will send you a mail immediately. So you are able to track the quality of your framework whenever there are any changes. Weee~!!
Import the Framework
Let’s now try to build an application using our framework. Under the project, add a new Target and chooseSingle View App
. Give a name to the target and click Finish button.
Next, go to the General part of this target and add our SwiftPlayWithString
framework into Linked Frameworks and Libraries list.
Back to the project navigator, you should have a folder with the name as the Target name. By default it will create a ViewController class for you to begin building stuff on your application. Now we are able to import SwiftPlayWithString in this file and use the method in the framework!
Wrapping Up.
I hope this tutorial is helpful to you. If you have any questions about the tutorial, please leave your comment below and let me know.
If you want to achieve more, I really recommend that you turn the framework into a CocoaPod and publish it like a third party framework.
That’s all! Happy coding ~ :D