Create Static Library in swift

In this tutorial, I wanna show you how create static library for your reusable code.

Why do you need to Library? This way helps to you for reduce your reusing codes. You can use for network requests, extensions etc (your reuse codes in each your project). Let’s get started.

First step click: File > New > Project. You have to choose `Static Library` on here and hit next.

I’ll create it for my network requests, so called it “MyNetwork” and make sure was choose swift below side and hit next. Save it where you want.

Okay, we created our library and you can see files. Open MyNetwork.swift file. You’ll see empty “MyNetwork” class only and let’s start to changing MyNetwork class.

I was got MyNetwork code struct from Majid’s gist (

In now our library is ready. I was created with public key because, we’ll add it to another project. We can build it now. And I was created NetworkError.swift and added this code

Select any simulator and press Command + B.

Okay our library was ready. Then we was going to create a new project for add library into it.

Adding Library to Project

So, I created new project, firstly we need to add compiled library’s files to our new project. For this right click on the project name and click Show in Finder

Then create lib folder for all libraries with this root.

Okay and later go back MyNetwork Library again and right click on libNetwork.a file and Show in Finder

Now drag and drop libNetwork’s files inside lib folder

Later open Project and right click on it. Click to “Add files to {your project name}”

Make sure selected Create Groups and click to Add.

Okay, we added it successfully.

Next step I going to Select project target and General tab to make sure we added library.

You can see our libNetwork.a in the bottom area. Next open Build Phases and expand Link Binary with Libraries and make sure our library was added to here if you can’t see it in here click + button and add it.

So, last as a last step click to Build Settings and check All and Combined and type “search paths” in the right search side. And we must copy path from Library Search Paths and add it to Import Path in the below side.

To do that double click and copy it.

And do the same thing for this. Click + and paste it.

That’s all, we completed all of process. And in now we can import it to our project and use it.

Okay so we can use it. I’ll use this api for test my Library. So create PostNetwork.swift

And paste this codes inside PostNetwork.swift

Okay let’s explain above code. PostModel Struct is for our network model and it must be confirm Request protocol. I created dataLoader and use its request function and that’s all. Our network request only contains this line of codes :)


Static libraries cannot hold a resource and for that create a Bundle with our resources (we don’t created in here, because we worked only code, didn’t use xib, assets .etc)

I hope, I was able to help you.

See you next article, don’t forget smile :)

iOS Developer