https://agilewarrior.wordpress.com/2016/04/04/how-to-add-xcode-subproject/

Create a blank iOS project that you’d like to use a static library in.

Then create a new static library project of type Cocoa Touch Static Library

Screen Shot 2016-12-22 at 7.57.41 AM.png

Screen Shot 2016-12-22 at 7.58.29 AM.png

Define some method in your static library class that you would like to call from your other project.

Screen Shot 2016-12-22 at 7.59.49 AM.png

Screen Shot 2016-12-22 at 8.00.13 AM.png

If you build this now you will see a libStaticLibrary.a file

Screen Shot 2016-12-22 at 8.02.30 AM.png

And if you right click a view in finder you’ll see

Screen Shot 2016-12-22 at 8.04.47 AM.png

There are two parts. The header file is what you other project will need to compile. The lib*.a file contains the executable.

Note however though that this library is currently only build for the Debug-simulator. That means it won’t work on a real device.

  • x86_64 (i386) architecture is required for running the 64bit simulator.
  • arm64 architecture is required for running the 64bit device (iPhone 5s, iPhone 6, iPhone 6 Plus, iPad Air, iPad mini with Retina display).

To make it universal we add a Aggregator target to our static library project.

Screen Shot 2016-12-22 at 8.10.13 AM.png

Screen Shot 2016-12-22 at 8.10.46 AM.png

And then add the following build script by selecting Project -> UniversalLib target, ‘+’ sign

Skärmavbild 2016-12-23 kl. 9.03.22 fm.png

Screen Shot 2016-12-22 at 8.25.01 AM.png

Screen Shot 2016-12-22 at 8.25.23 AM.png

And then copy/paste the following script into there.

# define output folder environment variable
UNIVERSAL_OUTPUTFOLDER=${BUILD_DIR}/${CONFIGURATION}-universal

# Step 1. Build Device and Simulator versions
xcodebuild -target ${PROJECT_NAME} ONLY_ACTIVE_ARCH=NO -configuration ${CONFIGURATION} -sdk iphoneos  BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}"
xcodebuild -target ${PROJECT_NAME} -configuration ${CONFIGURATION} -sdk iphonesimulator -arch x86_64 -arch i386 -arch armv7 -arch armv7s -arch arm64 BUILD_DIR="${BUILD_DIR}" BUILD_ROOT="${BUILD_ROOT}"

# make sure the output directory exists
mkdir -p "${UNIVERSAL_OUTPUTFOLDER}"

# Step 2. Create universal binary file using lipo
lipo -create -output "${UNIVERSAL_OUTPUTFOLDER}/lib${PROJECT_NAME}.a" "${BUILD_DIR}/${CONFIGURATION}-iphoneos/lib${PROJECT_NAME}.a" "${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/lib${PROJECT_NAME}.a"

echo "Universal library can be found here:"
echo ${UNIVERSAL_OUTPUTFOLDER}/lib${PROJECT_NAME}.a

# Last touch. copy the header files. Just for convenience
cp -R "${BUILD_DIR}/${CONFIGURATION}-iphoneos/include" "${UNIVERSAL_OUTPUTFOLDER}/"

This script builds the project and creates a universal library using a tool called ‘lipo’. Run this UniversalLib target

Screen Shot 2016-12-22 at 8.28.29 AM.png

Then go back to the finder on the libStaticLibrary.a we opened earlier and you should not see a universal folder there containing your universal library.

Screen Shot 2016-12-22 at 8.35.38 AM.png

This is libStaticLibrary.a and StaticLibrary.h are what you need in your other project to use this library.

Go to the project where you want to use the static library, and control drag these into a directory there.

Screen Shot 2016-12-22 at 8.45.06 AM.png

Use your newly imported static library somewhere in your code:

Screen Shot 2016-12-22 at 8.47.55 AM.png

And now try to Run. It should now work.

Screen Shot 2016-12-22 at 8.50.59 AM.png

Then whenever you add a new file, just add it to the project target Build Phases -> Copy Files

Screen Shot 2017-01-11 at 9.26.56 AM.png

Screen Shot 2017-01-11 at 9.27.00 AM.png

That’s one way to import your static library. The other way is to import it as a subproject.
https://agilewarrior.wordpress.com/2016/04/04/how-to-add-xcode-subproject/

Export your public headers (optional)

Close your static library project (else you won’t be able to drag it into your main project).

If you want to import your static library as a subproject, you need to publicly expose your library headers. Do that by going to UniversalLib (the aggregator) -> Build Settings -> Public Headers Folder Path + and enter the name of your library

Skärmavbild 2016-12-23 kl. 9.15.37 fm.png
Skärmavbild 2016-12-23 kl. 9.16.18 fm.png

Then when you include this subproject in your parent project goto Build Settings -> User Header Search Paths and add $(CONFIGURATION_BUILD_DIR) (non-recursive) so it knows where to find them.

Skärmavbild 2016-12-23 kl. 7.59.50 fm.png

Note 1: If you do this, you will need to import your library header with the angle brackets and not the double quotes, like so

Skärmavbild 2016-12-23 kl. 11.11.46 fm.png

Setup other linker flags (optional)

Because objective-c categories don’t be default link in static libraries, turn on -ObjC flag to share your internal categories with external clients.

To use Objective-C library add -ObjC to Other linker flags of parent project under Build Settings.

Skärmavbild 2016-12-28 kl. 12.15.09 em.png

Add new class to library

When ever you want to add a new class to your library and expose it to the outside world add the header file to the CopyFiles section under Build Phases

Screen Shot 2017-01-23 at 8.26.11 AM.png

Add then run the build aggregator

Screen Shot 2017-01-23 at 8.27.07 AM.png

And then import the header as regular if internal to the library

Screen Shot 2017-01-23 at 8.28.03 AM.png

Or square bracket if external to library

Screen Shot 2017-01-23 at 8.28.36 AM.png

 

Subproject arrow not expanding and showing up in parent

You won’t be able to add you static library if you have it open in another xcode window. Close it and it will show up correctly.

Links that help

https://www.raywenderlich.com/41377/creating-a-static-library-in-ios-tutorial
http://www.technetexperts.com/mobile/creating-static-library-in-ios-app-development/

Advertisements