Create and Publish your own Swift Package.
Swift Package Manager also known as SPM has been around for quite a while now. It's a really cool feature by Apple allowing us to manage, add and create dependency in a very efficient manner.
SPM is integrated with the Swift build system to automate the process of downloading, compiling, and linking dependencies. We already have popular Package Managers like Cocoa Pods, and Carthage but in this article, we will learn how cool SPM is.
Let’s say you are working in a team and your team wants a separate private framework that contains the main feature of the app.
So instead of managing the classes/code inside the app every time and doing back and forth, you can separately create a dependency/framework that just needs to import and you are good to roll.
You can also create a separate public dependency for the community to share cool stuff with them as well.
Without further ado, let’s dive right In !! :D
First of all, we need to create an Xcode Project. After you have created an Xcode Project let's add a new Swift Package Manager to our project.
After you have added a new Swift Package to your project you will have these files now. Now let's get an overview of the files now.
- ReadMe.md: Here is where you document your Package.
- Package.swift contains all the configurations for your Package. Like external dependencies, your targets, etc.
We are not going to add any external dependency to our Package so we can move ahead right now.
Now time to add your code in the ApiManagerPacakge file, in your case you can have a different name file.
If you are following along you can use a similar code. So our APIManager is ready. Now next step.
Now go to Targets -> General -> Framework Libraries and embedded content and tap on the + button.
We have finished creating our Package, lets check if it works fine.
Go to any class and try to import APIManagerPackage.
Now you can use our Custom created Package in your codebase. Just like this.
As this said let's publish our Package.
For publishing, the first step you need to do is to copy your Package Manager in any other directory so we can open it separately.
I copied mine to the desktop. You can copy wherever you like.
Open your SPM using Xcode. This is how it will look.
Now go to the Source control and click New git Repository.
You will get this window, make sure your checkmark is selected and press create.
After that go to the left bottom of your Xcode and you will see a settings icon. Select New “APIManagerPackage” Remote.
As soon as you tap you will get this information window. In my case, I have my GitHub repo settings here.
You can link an account to your Xcode as well from Xcode->Preferences -> Accounts.
So now we can create a remote for our Package in my case I will do a public repo as I will continue working on it ;)!
After you have created a new Repository, you will have to add a tag to your branch like this. You can study more about how to tag your branches in wwdc 2019 session on SPM.
So now our local repo is created branch tags are set up, now to our one final step.
Pushing our code to GitHub :D
As soon as you select Push your Package should be pushed and should be available on GitHub!
Let's check it out.
Our Package is live :D!!!
Now lets check if we can add this Swift Package to our Projects actually?!!
Paste URL in the URL section and you will be able to see your new Package.
And here is we have added our new Package Manager from our Github!!
That's it for this article, I hope it will help you guys. Please let me know if you guys have any questions. Thanks!
Happy Coding.
Sheeraz Ahmed!