How to add external static library in iOS project | by Sheikhamais | Dec, 2023


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



Source link

Leave a Comment