preface

The frameworks and libraries covered below are for iOS only and are not guaranteed to work on other platforms.

Recently, due to the business needs of the company, it is required to package a payment SDK, which needs to use wechat Pay and Alipay. The Framework made before does not rely on other third-party libraries, so it is easier to make. This time it is different. At the beginning, I wanted to integrate the SDK of Alipay and wechat Payment, but it took me a day to find out that there were many mistakes in my previous idea, which was not feasible at all. But finally, I succeeded in encapsulation, and here is my experience to share, so that students who need to avoid detdettions. Do your homework on what static and dynamic libraries are.

Credit: LeonLei’s blog http://www.gaoshilei.com

Static library and dynamic library detailed introduction

We usually introduce a third party SDK in the project more or less, even if you don’t introduce a third party SDK, at least reference the system Framework? In fact, these SDK and Framework belong to the library, the library is divided into static library and dynamic library, we usually import the third-party SDK has some Framework, some. Here are the differences between static libraries, dynamic libraries, Framework and.a and.dylib/.tbd

Static library and dynamic library

A Library is a piece of compiled binary code with a header file that can be used by others. There are two ways to use a Library:

  • Some code needs to be used by others, but we don’t want others to see the source code, so we need to wrap it in a library, exposing only the initial files.
  • For some code that will not undergo major changes, for example, many modules commonly used by large companies will be compiled into libraries, which can save compilation time and make code management very convenient.

Since libraries are already compiled binaries, you only need link to compile them. Since link is mentioned, there are different forms, static and dynamic, corresponding to static libraries and dynamic libraries.

1. Static library

The third-party SDKS we use are basically static libraries. Static libraries have the following characteristics:

  • When the App project is compiled, a copy will be compiled into the target application, which is equivalent to embedding the static library, so the resulting App binary will become larger.
  • At the time of use, you need to manually import other class libraries that the static library depends on.(For example, coremotion. framework is used in an SDK, which needs to be imported manually. Some SDKS need to link more than ten system libraries, this time very disgusting, can only be manually added one by one, this is a static library is a great inconvenience.
  • An application importing a static library can reduce its dependence on the outside world. If a third-party dynamic library is imported, the application will crash if the dynamic library cannot be found. For example, lib Not Found is frequently found on Linux.
  • A great advantage of static libraries is to reduce the coupling, because static libraries can not contain other static libraries, when using it to import its dependent libraries, to ensure that each static library is independent, do not repeat reference.

2. Dynamic libraries

This is the kind of library that we use most, uiKit. framework and Fundation. Framework are all dynamic libraries, all.dylib and.tbd ends are dynamic libraries. Several characteristics of dynamic library:

  • Dynamic libraries are also called shared libraries because they are stored in iOS and are not copied to your application when you package it. They are dynamically loaded from iOS when needed. Features that are loaded at compile time also allow us to replace libraries at any time without recompiling the code.
  • These libraries are common to all applications. In other words, they save the size of the application installation package. This is an important difference between static libraries, because static libraries have to be copied every time they are used, which is very wasteful.
  • Dynamic libraries can include static libraries directly at the time of creation, and can automatically link the required dependent libraries.
  • When using a dynamic library, you don’t need to link to the library again. Note that when importing the Embedded Binaries dynamic library, you need to import the Embedded Binaries dynamic library. Otherwise, an error message “Image not found” will be displayed. At this point, the dynamic library is copied to the target program for compilation just like the static library. Apple also calls this Framework Embedded Framework

About the dynamic library to be clear, we made our own dynamic library and the difference between the system dynamic library, we made our own dynamic library introduced into the App project need embed into the project, that is, to copy to the target program, this is a bit unlike the characteristics of the dynamic library, Apple do so is to consider security issues! Can as normal, I don’t know, look up a large amount of data is copied to copy not clear, I guess is not on, because the general third party SDK are also in the form of a static library, I guess one important reason is that the iOS application is running in the inside of the sandbox, cannot be Shared among different application code, And dynamic downloading of code is definitely forbidden by Apple, so dynamic libraries are moot. Of course, there may be other factors, welcome to exchange learning!

.framework. A. Dylib /.tbd

1. Framework

Framework is a Framework consisting of Headers, binary files, and.bundle files, as well as info.plist and Modules. The latter two files record the version of the Framework and are usually deleted without discussion

  • Headers contains the Headers that we exposed when we made the Framework, and all the exposed.h’s are in there.
  • The core of the entire Framework, all code is compiled into such a heap of binary files. Note that the added dependency libraries are not compiled in the Framework, and you need to re-link other dependencies to use them.
  • .bundle

    The resource files are all packaged up here. When creating the Framework, you should not put images directly into the project, otherwise the images will appear one by one in the project. You need to create a new bundle to put the images into the project, and this bundle provides the image resources of the entire SDK.

    ** Note: ** images cannot be used after being placed in the bundle[UIImage ImageWithName:]Read the picture. You need to find the bundle before you get the picture.

There is a misconception to be corrected here

Many people think that the Framework of the system is a dynamic library, we made the Framework is a static library.

The Framework can be either a static library or a dynamic library, depending on whether it’s compiled into a dynamic library or a static library. The Framework is not a library at all, it’s a way for Apple to package libraries for developers. The Framework includes Mach-O files, header files, and resource bundles. You don’t need to do this manually. You can also use Xcode to create dynamic libraries for the Framework. So summary: The Framework is a package of libraries, both dynamic and static.

2.. a static library

This kind of static library is basically similar to Framework, but the difference is that it needs to provide header files when it is packaged into. A file, which is more troublesome to use than Framework (for example, wechat Pay SDK uses. A, but the difference is that Alipay SDK is packaged in the form of Framework). .a This is not convenient packaging, and the Framework is compiled to expose the headers are already in place.

Dylib /.tbd dynamic library

This kind of dynamic library we also often use, basically are provided by the system, generally can not make their own, even if you make use of other ways, also certainly can not be put on the shelf, there is nothing good to talk about here.

Ii. Framework production

The process of making dynamic libraries is basically the same as that of static libraries, including the exposure of header files, etc. The only difference is the compilation form of Mach-O files. This section describes how to create the Framework by Xcode. This Framework static library relies on other third-party static libraries (Framework and.a).

1> New project


2> Import all files to be packaged and other third-party static library normal import files to be packaged can be used, when importing third-party static library, do not select to add to target, if added to target to delete third-party static library (just import, do not add to target).












3> Modify the project nature. The membership of the project needs to be changed to public; otherwise, the exposure of header files will be abnormal

4> Expose header files Expose header files for external use, all compiled files are in Project, need to right click to add to public

This is the most important step. It determines whether we will create a Static Library or a Dynamic Library. The default is Dynamic Library

If you have a dylib dynamic library that starts with lib, you should get an error


Select Add Other, press shift + Command + G in the pop-up window to bring up the Finder’s Go window, type /usr/lib, and add the dylib library


8> Create a new folder, copy the static library made out and put it in, and copy the third-party static library to the same folder. At this time, just provide this folder to the outside world for use. This is the test demo I wrote to verify whether the packaged SDK can be used normally






If you need to make a Dynamic Library, just change the form of Mach-O to Dynamic Library in step 5, as in other steps

If you have any questions, please leave a message in the message area, or email to me, exchange and learn!