How to add Dynamic Library to Xcode project.

First of all, for “. A static library “and” static Framework “, just drag the relevant files into the project and check the Copy if needed option. No additional Settings are required.

However, adding dynamic Framework is slightly more troublesome. There are mainly the following ways.

PS: By “adding dynamic libraries” we mean third-party dynamic libraries, not native dynamic libraries like uikit. framework, foundation. framework, or libc++. It’s easy to add their dependencies. Go to General -> Linked Frameworks and Libraries and click on the plus sign to add.

manually

Select the target corresponding to app in Xcode project, then click the plus sign under General -> Embedded Binaries, as shown in Figure 1, and select Add Other in the popup window… In the Finder, select the Dynamic Framework you want to add and check Copy if needed. Note that you cannot drag and drop a. Framework file directly into the Embedded Binaries in the Finder, otherwise an error will be reported.

For more details on adding dynamic libraries manually, see the tutorial on Embedding Frameworks In An App.

But! This may seem convenient, but there is a catch: as we mentioned in the previous episode, dynamic binaries typically contain many processor architectures, such as: I386, x86_64, armv7, armV7s, arm64, etc., then Xcode will copy the dynamic binary files directly into the.ipa package when compiling the link, and will not filter out the unused architecture like link static library. However, Apple does not allow you to upload the packages containing the i386, X86_64 and other emulator architectures to the App Store Connect background, so an error will be reported. Therefore, we often need to remove these Invalid Architectures manually, either through the LIPo command or by writing a script, when releasing the official package. (Unless your development project is only debugged on a real machine, not intended to run in an emulator, and the added dynamic library doesn’t include i386, x86_64)

Integrate with Carthage

Carthage update will generate a “dynamic framework” after adding dependencies to the Cartfile. For example, AFNetworking. Framework, sdwebImage. framework, and so on, drag and drop them to the General -> Linked Frameworks and Libraries directory in the project. See Carthage’s Quick Start tutorial for details.

The key action here is to add a New Run Script Phase to the Build Phases of the Xcode project and execute the following commands in the Script:

/usr/local/bin/carthage copy-frameworks
Copy the code

As shown in Figure 2:

This command will automatically remove i386 and x86_64 from the copy dynamic library when it is packaged.

Integrate using CocoaPods

Similarly, integrating the dynamic library through CocoaPods automatically adds a Shell script to our project to do this, as shown in Figure 3. [CP] Embed Pods Frameworks

Take a look at the Pods-XXX.-nbsworks. sh script, which has a function called strip_invalid_archs() to remove useless processor architectures during packaging, as shown in Figure 4:

Therefore, we can integrate our own or other-provided dynamic framework into our project through CocoaPods: Create a Pods private Git library, add the related dynamic.frameworks file to the git library, and write the Podspec file roughly as shown in Figure 5. Finally, add pod Install to your project.

Finally, let’s consider the following question: “Static framework” and “dynamic framework” seem to be the same in use, and adding “dynamic framework” to the project is tedious, so when do we use dynamic libraries in iOS/macOS development?