Recently wanted to make some of their own utility classes into a static library, the web search on the framework and. A some of the relevant information, but written or not comprehensive, or not detailed, I summed up and personally practice to write this article. Due to the length of the project linked to the other blog iOS project, the main engineering and framework engineering or. A static library engineering joint adjustment
I. Introduction and differences between the framework and. A two static libraries
. A is a pure binary file, and the. Framework has a resource file in addition to the binaries.
. A files cannot be used directly, at least with. h files, and. Framework files can be used directly.
. A +. H + sourcefile =. Framework.
. A is just a static library. The framework can be either a static library or a dynamic library. For example, the framework of a system is a dynamic library.
Static Library: When the link is completely copied to the executable file, multiple copies of the redundant copy are used.
Dynamic Library: Link is not copied, the program is dynamically loaded into memory by the system, for program calls, the system is loaded only once, multiple programs are shared, saving memory.
However, Apple does not allow developers to have their own dynamic library.
Ii. Framework production and use
1. Create a new framework project
2. Add some of our useful classes to come in
3. When the file is just pulled in, for example, only APPBaseSDK.h is placed in public by default, and we have to move the. h file that needs to be exposed to the outside in project to public.
4. Then set the compilation mode, open the Xcode menu product--->scheme--->edit Scheme, change to release mode, because the final package is to use the release mode
5. Set the minimum supported version
6. Set the instruction set contained in the compiled static library
Simulator: iphone4s~5:i386 iphone5s~6plus:x86_64
Real machine: Iphone3gs~4s:armv7 iphone5~5c:armv7s iphone5s~6plus:arm64
If build Active Architecture only is set to Yes, then the compiled static library contains only the instruction set of the current device.
For example: If we choose the iphone 5 Simulator compiled, the compiled static library can only use the Iphone4s~5 simulator run program, with Iphone5s~6plus, will be reported to find x86_64 APPBASESDK library.
Set to No, all instruction sets are packaged and merged. Therefore, the static library has a disadvantage is that the static library package is much larger than the source.
7. Finally modify the generated Mach-o format
8. Compile and build the static library
Compile, you need to compile with the emulator and the real machine, so that the Appbasesdk.framework static library in the products directory will become black, right-click the show in Finder, you can enter the products directory.
9. Merging Static library files
To make the static library available to both the real machine and the emulator, you need to merge the two static libraries. The framework static library merges not the framework, but a binary file under the framework, that is, the file to be merged in the tag. Lipo-create the absolute path of the binary file under the first framework the absolute path of the binary file under the second framework-output eventually generates the merged binary file path (I put it on the desktop).
The commands used to open the terminal are as follows:
lipo-create/users/zhanglinfeng/library/developer/xcode/deriveddata/appbasesdk-dpqdspcdgwsrxgdihiaxpqpkvali/ build/products/release-iphoneos/appbasesdk.framework/appbasesdk/users/zhanglinfeng/library/developer/xcode/ deriveddata/appbasesdk-dpqdspcdgwsrxgdihiaxpqpkvali/build/products/release-iphonesimulator/ Appbasesdk.framework/appbasesdk-output/users/zhanglinfeng/desktop/appbasesdk
If you find it troublesome to hit the file path, you can drag the file into the terminal and type the path to the file. If the error suggestion will be in Release-iphoneos and Release-iphonesimulator in the Appbasesdk.framework deleted, re-use the simulator and the real machine to compile and try again.
Either the real machine or the simulator randomly select a framework that replaces the binary files in the framework appbasesdk (that is, the files to be merged) with the files that were just merged (just built on the desktop). Well, this framework is what we need.
10. Using the framework
Drag the framework into a new Useframeworktest project, and I'll drag it down to the Useframeworktest folder under the arrows, and if you want to drag it to another directory, change the framework Search Paths , I'll introduce Search Paths path-related knowledge later
Import the more commonly used header files in the framework into APPBaseSDK.h (such as circled) so that only #import <appbasesdk/appbasesdk.h> are needed outside, and you can refer to the files in the circle. If you do not want to import too many files into APPBaseSDK.h, it is like this to refer to #import <APPBaseSDK/MBProgressHUD+Easy.h> (although can use but will report a warning, So let's import it into the APPBaseSDK.h.
Use code, such as the normal call to the Chinese phonetic method and print the results
Note: If you want to use the category method in the framework, you need to set the other Linker flags to-OBJC(note the case, some of the data are in the wrong case, and the Pit is dead). Introduced the-OBJC flag, its role is to load all the objects in the static library and object-related files are loaded in this can solve the problem, but in the 64-bit Mac system or iOS system, the linker has a bug, Causes a static library that contains only categories to be unable to load the file using the-OBJC flag. The workaround is to use the-all_load or-force_load flag, which is to load all the files in the static library, but All_load works on all the libraries, and the-force_load must specify the specific file later.
Third,. A static library of the production and use
1. Create a static library project, the project named Basesdk, the resulting. A file name becomes LIBBASESDK.
2. Delete the automatically generated file BaseSDK.h BASESDK.M
3. Add your utility class file
4. Add Headers Phase
5. Add the exposed header file to the outside, add it to the public to move to the public (not move to the public is right, but the 11th step below does not appear. h file, need to find from the source of the library)
6. Then set the compilation mode, open the Xcode menu product--->scheme--->edit Scheme, change to release mode, because the final package is to use the release mode
7. Set Build Active Architecture only
Simulator: iphone4s~5:i386 iphone5s~6plus:x86_64
Real machine: Iphone3gs~4s:armv7 iphone5~5c:armv7s iphone5s~6plus:arm64
If build Active Architecture only is set to Yes, then the compiled. A static library contains only the instruction set of the current device.
For example: if we choose to compile the iphone 5 simulator, the compiled. A static library can only run with the Iphone4s~5 simulator, with Iphone5s~6plus, will be reported to find x86_64 APPBASESDK library.
Set to No, all instruction sets are packaged and merged. Therefore, the static library has a disadvantage is that the static library package is much larger than the source.
8. Set the minimum supported version
9. Compile and build the static library
Compile, you need to compile with the emulator and the real machine, so that the LIBBASESDK.A static library in the products directory will become black, right-click the show in Finder, you can enter the products directory.
10. Merge emulator and real machine static library file libbasesdk.a, open Terminal command as follows
lipo-create/users/zhanglinfeng/library/developer/xcode/deriveddata/basesdk-cexmrzesjuswutaldkedwjpnpxnk/build/ products/release-iphoneos/libbasesdk.a/users/zhanglinfeng/library/developer/xcode/deriveddata/ basesdk-cexmrzesjuswutaldkedwjpnpxnk/build/products/release-iphonesimulator/libbasesdk.a-output/users/ zhanglinfeng/desktop/libbasesdk.a
11. Use the. A static library
Drag the static library into the new project, and I'll drag it down to the Usea folder under the arrows (if you want to drag to another directory, you will change the library search Paths, I'll introduce the search Paths path-related knowledge ), will be exposed to the outside. h files are also dragged into the project,
Note: If there is no. h file in the include. Then pick out some of the. h files that need to be exposed from the library's source code.
The import header file is ready for use, such as
Note: If you want to use the category method in the static library, you need to set the other Linker flags to-OBJC(note the case, some of the data are in the wrong case, and the hole is dead). Introduced the-OBJC flag, its role is to load all the objects in the static library and object-related files are loaded in this can solve the problem, but in the 64-bit Mac system or iOS system, the linker has a bug, Causes a static library that contains only categories to be unable to load the file using the-OBJC flag. The workaround is to use the-all_load or-force_load flag, which is to load all the files in the static library, but All_load works on all the libraries, and the-force_load must specify the specific file later.
Iv. about using the library when search Paths
Maybe someone will wonder if you don't have to set up search Paths , and if you follow my steps and drag the static library to the project's appropriate directory, then the default setting is OK.
If you put the static library into the path you like, you will have to set the search paths based on the location of the static library in the folder .
Indicates the folder path
"./" and "$ (project_dir)" means the current project is in the same folder, is a relative path, relative to the project in the computer's location, will automatically locate the absolute path on the current computer. If you write an absolute path, the project changes the folder location, or switch to another computer, the path is error. “.. /"Indicates the path of the previous folder in the folder where the project is currently located. The meaning of the two parameters is set by the path, non-recursive is not recursive lookup, and recursive is searched recursively. Search Paths Specific examples My other blog has iOS engineering, Master Engineering and framework engineering, or. A static library project linking
IOS latest framework and. A static library production and use full solution