By wrapping your controls into framework static libraries for use, you can share controls with others without exposing all the implementation details of your code. In addition, you can learn more about the daily use of other people’s libraries through your own encapsulation process, or you can modularize a fixed business into a static library when implementing modularization.

Framework static library creation

In iOS, there are two types of static libraries:. A: a pure binary file that requires. H files to work with. Framework: contains binary and. H files, as well as resource files

In Xcode, we can create static libraries by adding targets to existing control projects. The first one is to create the. Framework and the second one is to create the

Here we need to change the Mach-o type toStatic LibraryBecause we are making a static library

Select Build Settings -> Architectures -> Build Active Architecture Only -> Release and select NO. No indicates the CUP architecture supported by all emulator devices. (The Debug version is also recommended to be selected when used by others.)

After creation, a project will be generated, where.h is used to store the header files that need to be exposed to others in our project, mainly using < package name/header file name. H >, because in the actual use of the framework will need to search for header files from the care.

After importing in.h, you also need to import inBuild Phases - HeadersPut the corresponding header file inPublicIn the

We write a log method in the test file we added

Select emulator to compile, then right-click framework In Products to see Show In Finder The debug-ophonesimulator package was used by the simulator in Debug mode, and the relese simulator package and the real machine package were used by the relese simulator and the real machine package, because we used fRAM Ework packages are all one.

Merges two types of framework

There are two methods, one via script and the other via the LIPo command line, the second of which is described here

Lipo started as a MAC application that was designed to run on both PowerPC and Intel platforms. MacOSX is a tool for handling general-purpose programs in MacOSX. There are three common methods:

  • View the CPU architectures supported by static libraries
Lipo - info libname. A. (or libname. Framework/libname)Copy the code
  • Merge static libraries
Lipo-create Static inventory store path 1 Static inventory store path 2... -output Indicates the storage path after consolidationCopy the code

For the framework, you only need to merge.a files

  • Static library split
Lipo Source file path n/A THIN CPU architecture name N/A output File path after splittingCopy the code

Replace the combined.a file with the.A file in the original package, and you have a simple framework package

Framework static libraries added. Bundle file resources

When the framework needs to rely on image Resources, you can create a bundle to store image Resources, and then add dependencies to the Copy bundle Resources

Create a targets for the bundle

Set up the

  • Build Settings Modify Base SDK->Latest iOS

  • Build Phases adds the xiB and image Resources required to Copy Bundle Resources

  • Combine High Resolution Artwork or COMBINE_HIDPI_IMAGES, one of which is the name of OSX and the other is the name of iOS, can be changed to NO to save pictures, otherwise it will be TIFF The build will then generate the required bundle file

Add images to the bundle

Add the dependent

This allows you to reference these image resources in the framework

Framework package dependencies on other third-party libraries

In practice, our framework packages often rely on some third-party libraries, such as AFNetworking,SDWebimage, etc. These libraries are often included in the main project, and if they are added to the framework, it will cause repeated class conflicts. There are two ways to deal with it

  • Change the class names of all the classes in the reference library and add our own class headers

This approach is suitable for libraries that are smaller or have fewer classes

  • These libraries are added by reference only

We can only reference the library when we add it, and then reference the corresponding header file in the framework to ensure that the build succeeds and that the framework does not pack the corresponding class file. However, if you already have a dependency, you need to add the corresponding library in the main project. It is important to note that if you use it as an SDK for a third party, you need to determine the version of the dependent library in the framework, because some library version changes may cause different corresponding files. Such as AFNetworking between 2.x and 3.x versions

Do not check Add To Terget

Demo

I wrote a demo of alertView control, put it on Github, address here

I hope you can point out the problems in the article and study together