How to add external static library in iOS project

Sheikhamais
4 min readDec 26, 2023

--

Create a new static library project in Xcode

Hi,

I recently tried adding a new static library inside an iOS project and this took me through some learning and steps, so I thought anyone going through some similar problem can get help.

I am using Xcode 15, and there are problems using external library, but lets first see how can we add and use one.

Create a new static library project in Xcode and name ‘MyStaticLibrary’. You may give your name

Now add some class and function so that we can use and test our brand new library

Make sure to mark them public

Now Build the project by hitting CMD + B twice, once by selection the simulator and once by selection any device, screens here:

Now go to the derived data in Xcode and from there go to the build folder inside this MyStaticLibrary project. Ideally the path should be Xcode>Settings>Locations>DerivedData the small arrow in the dialog which will take you to the finder where there is derived data and your project folder should be in that. (ss for reference)

Now inside the build folder there will be a product folder which will have the following two builds which you created earlier by building project:

Now here is the problem, you may either use this static library for builds for the simulator or for the real devices. You may use either of these depending where you are testing your project in device or simulator where this library is going to be added.

Previously when we had Intel based macs which used x86–64 architecture and iOS devices used arm64 architecture it was possible to combine both the .a library binaries in these folders using lipo, but now after the M Macbook series, the simulator generated binary is a combination of x86–64 and arm64 and therefore lipo can’t combine because it needs different base architecture for each file to be combined.

I even tried thinning using lipo and keep the x86–64 for simulator (as M series mac simulator give option to build for Rosetta if the simulator is used in this M Mac) and arm64 for iphones and then combining both but that didn’t worked also and I ran into other problems.

Anyways, I used simulator folder in order to test this project. For iphones the procedure is same, just use the iPhone folder.

Make a new folder and copy files which are inside these folder into your new folder, I named it ‘Static Library’.

After moving here you will now have folder with you static library files with simulator that is ready to be used inside your other project.

I am creating a test project where I will use this library:

For convenience, place the folder you created into your project directory:

Now we have a new project with library placed here

Go to the Project> Build Phases> Link Binary With Libraries and here hit the small plus (+) button. Here select your library .a file which will be inside the Static Library folder:

After adding it, you library is added to your project.

Now we have to set the paths inside Build Settings in order to use this library. Go to the Build Settings and search for ‘paths’. Make sure all tab is selected and in the ‘Library Search Paths’ under Search Paths as well as in the ‘Import Paths’ under Swift Compiler — Search Paths provide the path to your folder which you just added. This step is very important without which we won’t be able to use our library, make sure to add correct paths, SS for reference:

  1. ‘Library Search Paths’ under Search Paths

2. ‘Import Paths’ under Swift Compiler — Search Paths

After adding these you are now able to use this library inside your project.

To test your library inside ViewController add this code and see you public classes in working:

Woha! See you are using your SuperCoolClass from your library.

Thank you :)

--

--

Sheikhamais
Sheikhamais

Responses (1)