Current location - Health Preservation Learning Network - Slimming men and women - Several methods of adding "dynamic" framework to Xcode project
Several methods of adding "dynamic" framework to Xcode project
How to add a dynamic library to an Xcode project?

First of all, we know that for ". A static library and a static frame, we can directly drag related files into the project and check Copy. If necessary, there are no other additional settings.

Adding a "dynamic frame" is a bit troublesome, mainly in the following ways.

PS: The "adding dynamic library" mentioned here refers to the third-party dynamic library, not the dynamic library that comes with systems such as UIKit.framework, Foundation.framework or libc++. Generally, it is very simple to directly add their dependencies->; Click the plus sign in linked frames and libraries to search and add.

Select the target corresponding to app in Xcode project, and then select it in General->; Click the plus sign under Embedded Binaries, as shown in figure 1, select Add Other… in the pop-up window, and finally select the "dynamic frame" you want to add in the Finder, and check Copy if necessary. It should be noted that you can't drag. The frame file is directly put into the embedded binary file in the Finder, otherwise you will get an error.

For more details about manually adding dynamic libraries and solutions to problems, please refer to Apple's official tutorial: Embedding Frames in Apps.

But! This method seems to be very convenient, but there is actually a pit: as we mentioned in the last episode, a general dynamic binary file will contain many processor architectures, such as i386, x86_64, armv7, armv7s, arm64 and so on. Xcode then directly copies the dynamic binary file to. Ipa package when compiling links. Unused architectures will not be filtered out like linked static libraries, and Apple is not allowed to upload packages containing i386, x86_64 and other emulator architectures to the App Store Connect background, which will lead to errors. Therefore, when we play the official release package, we often need to manually remove these invalid architectures through lipo commands or writing scripts. (Unless your development project is just real machine debugging and you don't intend to run it on the simulator, and the added dynamic library just doesn't contain i386 and x86_64).

For the third-party library integrated by Cartage, executing the carthage update command after adding dependencies in Cartfile file will help us to generate "dynamic frameworks", such as AFNetworking.framework, SDWebImage.framework, etc. , and then drag them to General->; Linked frames and libraries, and then configure related replication scripts and commands. For details, please refer to the Carthage Quick Start Tutorial.

A key operation here is to add a new run script phase in the Xcode project construction phase, and execute the following commands in the script:

As shown in Figure 2:

The function of this command is probably to help us automatically delete i386 and x86_64 when packaging and copying dynamic libraries.

Similarly, when the dynamic library is integrated through CocoaPods, a Shell script will be automatically added so that we can complete this work in the project, as shown in the [CP] Embedded Pods framework in Figure 3.

You can check the contents of the Pods-xxx-frameworks.sh script by yourself, and there is a function strip _ invalid _ arches () in it, which is used to remove the useless processor architecture when packaging, as shown in Figure 4:

So we can integrate the dynamic framework developed by ourselves or provided by others into the project through CocoaPods: create a private git library of Pods (I believe everyone is already familiar with it) and add relevant dynamics. Frame file to git library, and then the writing method of its Podspec file is roughly as shown in Figure 5. Finally, you can install Pod in your project.

Finally, let's think about a problem: there seems to be no difference between "static framework" and "dynamic framework" in use, and it is more complicated to add "dynamic framework" to the project. Under what circumstances will dynamic libraries be used in the development of iOS/macOS?