IOS package static library (source code contains other static libraries and open source library)

#

Because the company recently to provide a functional module to third parties to use, so you need to be involved in the source code into a static library (.A file), but… How to pack it, never do ah! Google one morning, find a lot of articles, writing is very good, but few can have one, let you see will be able to thoroughly understand the. For example, their own source code contains third static library how to do? The use of image resources, Xib resources how to do? How to deal with their own source of the third party library can not affect the use of the same library on the upgrade and use of the third? This is a problem that needs to be considered. Static library on the introduction I do not spread, a step by step to create the.A library:

A #, creating a static library project

This step should be very simple, such as the project name is YJEChatSDK.

IOS package static library (source code contains other static libraries and open source library)
Paste_Image.png

is probably the way it was built:

IOS package static library (source code contains other static libraries and open source library)
Paste_Image.png

The default project to add the class to delete (moved to the wastebasket) on it, the whole project is very clean!

IOS package static library (source code contains other static libraries and open source library)
Paste_Image.png
# two, for the construction of the necessary settings

1, support the system here to choose IOS Oh (although the default is)…

IOS package static library (source code contains other static libraries and open source library)
Paste_Image.png

2, the Build Active Only option, when it is set to Yes, is to compile the debug faster, at this time it only compiles the current version of the architecture Architecture. When set to No, all versions are compiled. Here we want to set NO.

IOS package static library (source code contains other static libraries and open source library)
Paste_Image.png

3, if you use the source code category (category), it is necessary to add -ObjC or Flags in Other Linker -all_load.

IOS package static library (source code contains other static libraries and open source library)
Paste_Image.png

There are some conflicts between the standard static library implementation of Unix and the dynamic characteristics of Objective-C: Objective-C does not define a link symbol for each function (or method), which creates a link symbol for each class. So when using class extension when existing classes in a static library, the linker does not know how to put the method of original method and category together, will cause the method you call in the category, “selector not recognized”, which is to find the method definition error. In order to solve this problem, the introduction of the -ObjC logo, it is the role of the static library and all the objects associated with the file load.

This can solve the problem, but in the 64 bit Mac system or iOS system, the linker has a bug, will lead to contain only static library categories cannot use the -ObjC flag to load the file, the workaround is to use -all_load.

4, select release

IOS package static library (source code contains other static libraries and open source library)
Paste_Image.png
IOS package static library (source code contains other static libraries and open source library)
Paste_Image.png

So far the basic settings for the project is finished, of course, according to their own needs may be other settings.

# three, add the code file

Note, when the package does not contain static library resource files, even if we will resource file (.Png file or Xib file) copy to the static library project, but these resources are not added to the target, that is not included in this compilation results in resources, so if someone calls you make this a static library, all the resources (images, Xib) are missing.

In view of this situation, we will code and resource files are considered separately, first is the package code file:
1, add your own code and put their own private library
source code and the private library (not including the third party open source library, open source library because anyone can download and use, no need to also should not be packed in engineering).

If your source static library or private library and use third party, packing time and not a static library includes a static library, so you only need to put the third party static library header files into our project is good, without the need for the.A file also added. It should be noted that, in the end, we should put our.A library and the third party of the.A library together into the project.

IOS package static library (source code contains other static libraries and open source library)
Paste_Image.png

2, add on the open source library
source code if we rely on some open source libraries, such as AFNetWorking, Mansory and so on, they must be packed in, but this way, easy to bring problems such as conflicts, open source library, version not compatible.

So my approach is to add only the header files of these open source libraries, and then tell the user that our static library is dependent on these open source libraries, so that they can download the appropriate framework. The benefits of doing so is convenient, third party users voluntarily upgrade their open source library without worrying about with the static library in the open source library cause conflicts, and even if users upgrade open source library version, also generally does not change the header files inside the interface (high version always compatible low version), so it will not affect the static library our.

IOS package static library (source code contains other static libraries and open source library)
Paste_Image.png

3, exposed the corresponding interface (header file), for the use of the third party
we packed static library is certainly to be used, so the need to expose the design of the head file for others to use. All source files that need to be compiled in a static project are included in the Compile Source, as shown in the following figure:

IOS package static library (source code contains other static libraries and open source library)
Paste_Image.png

The header files that need to be exposed are added to the Copy Files option, as shown in the following figure:

IOS package static library (source code contains other static libraries and open source library)
Paste_Image.png

So far, the preparation of the package is basically completed.

Four, # generate static compiler

According to the need, if you want to use a static library in the simulator, the simulator on the choice of compile time, used in the prototype, on the choice of the prototype compiler.

IOS package static library (source code contains other static libraries and open source library)
Paste_Image.png
IOS package static library (source code contains other static libraries and open source library)
Paste_Image.png

Here I am in the simulator and real machine are running again, get two.A files, prototype and simulator:

IOS package static library (source code contains other static libraries and open source library)
Paste_Image.png

The static library for others to use, at the same time to the prototype version and the simulator version, to the two file is certainly not convenient, so the two versions merge into a.A file, so that regardless of the prototype or simulator can run.

Open the terminal, you can look at the.A Library under the support of the framework, the input lipo -info static library path, static library path here directly drag the file on the good.

IOS package static library (source code contains other static libraries and open source library)
Paste_Image.png

Obviously, armv7 and arm64 32 bit and 64 bit i386 and x86_64 real machine, said the 32 bit and 64 bit simulator (MAC architecture). The following command will use prototype version and version:
lipo -create simulator with a static library path 1 static library path 2 -output /Users/yangjie/Desktop/YJEChatSDK.a

IOS package static library (source code contains other static libraries and open source library)
Paste_Image.png

Get the final YJEChatSDK.a file.

# processing resource file (picture.Png and Xib files)

* first consider only the picture
if the code uses a number of image resources, how to correctly read these pictures? My approach is (I do not know if there are other ways): a new folder on the desktop, will be used to put all the pictures, and then change the name of the file: file name.Bundle. For example, I changed to YJsdkBundle.Bundle. This is the use of static libraries, the Bundle file will be put directly into the project on it.

IOS package static library (source code contains other static libraries and open source library)
Paste_Image.png

However, this is not the case, because the static library code still can not find the path to the image resources, so it can not be properly loaded. We need to go back to the code and add two macro definitions in the code:

Define BUNDLE_PATH mainBundle] resourcePath] stringByAppendingPathComponent:@ "YJsdkBundle.Bundle"] "[[[NSBundle"
Define YJImageNamed (imageName) ([UIImage imageNamed:[NSString stringWithFormat:@%@/%@ ", BUNDLE_PATH, imageName]])

The method of all loading pictures of [UIImage replace imageNamed:] we define macros, such as I am here is YJImageNamed (picture name), note that the picture name should correspond with the picture in the Bundle file name (suffix @2x or @3x without). No sub folders here in the Bundle file, so the macro definition, “stringWithFormat:@%@/%@”, BUNDLE_PATH imageName, so if you write Bundle files and subfolders, should write:
stringWithFormat:@ “%@/ folder name /%@, BUNDLE_PATH, imageName

In this way, the picture can be loaded correctly!

* about Xib file
for Xib file, I did not personally try, so… There is no right to speak, we can Google. (^__^)

After packing, the use of compiler connection error:

IOS package static library (source code contains other static libraries and open source library)
Paste_Image.png
IOS package static library (source code contains other static libraries and open source library)
Paste_Image.png

Similar to this strange error, I encountered, the reason is, 1, did not introduce the necessary system framework. 2, the static library has already included some files, the use of the time, and the introduction of these files, resulting in duplicate error.