A:

1. What is a library?

Libraries are ways of sharing program code. They are generally divided into static libraries and dynamic libraries.

Static library: complete copy to executable file when linking, multiple use of multiple redundant copies.

Dynamic library: link does not copy, program run by the system dynamically loaded into memory, for program call, the system only loaded once, multiple programs shared, saving memory.

2. Differences between the two forms. Framework

As shown in the figure above, the static library form includes both. A and. Framework forms.

Dynamic libraries are in the form of.dylib and.framework, which includes private.framework and system.framework.

Static libraries and dynamic libraries have private. Framework, but there is a fundamental difference, dynamic library private. Framework, shelves will be organic audit rules, and can not be downloaded dynamically. Static libraries are private. Framework, like a wrapped block of code, without too many restrictions.

3. Differences between. A and. Framework in static libraries

A is a pure binary file. The. Framework has resource files in addition to binary files. A files cannot be used directly, at least with. H files, while. Framework files can be used directly because they contain h files and other files

4. Advantages of static libraries

  • Implement the program modularization, the fixed business modularization into a static library.
  • Easy to share code, which means you can share your code base with others without seeing the implementation of your code.
  • The need to develop a third party SDK, such as a business communication between two companies, makes it impossible to send all the source code to another company. In this case, private content is packaged as a static library, and others can only call the interface without knowing the details of the implementation.

The company’s project needs to develop a set of SDK supporting wechat Pay, Alipay payment and UnionPay payment at the same time, which not only meets the needs of the company’s project, but also needs to be provided to other companies for use.

Integrate the payment SDK of three third parties at the same time, which has a lot of configuration conflicts to be resolved. This article introduces how to develop their own static library through demo. A file, share with you, and summarize the work.

Two: static library implementation

Source code Demo access method

Follow [net development] wechat public account, reply [91] can be received. Net the world method, convenient for you and me to develop, more iOS technology dry goods waiting to receive, all documents will continue to update, welcome to pay attention to grow together!

1. Create a static library project

Open Xcode, click File\New\Project, select iOS\Framework & Library\Cocoa Touch Static Library and create a New Static Library Project.

Name the project FBYSDKDemo and save the project to an empty directory. The static library project consists of the header file fbysdkDemo. h and the implementation file fbysdkDemo. m, which will be compiled into the library itself, as shown below:

In order to make the static library easier to use during development, simply let the user import a header file to access the interface you provide and perform data callbacks through the interface.

2. Import the header file

Import UIKit’s header file, which is needed to create a library. As you create different component classes, you will add them to this file, making sure they are accessible to users of the library.

Open fbysdkDemo. h and import the header file

#import <UIKit/UIKit.h>
Copy the code

Click Build Phases to expand the Link Binary with Libraries section, click + to add a new framework, go to UIKit.framework, and click Add to add it.

A static library, which compiles a set of files in which classes and methods exist as binary data, is useless without combining header files. Static libraries are divided into two classes, one is public, one is private and can only be accessed internally.

Next, you need to add a new phase in the Build column to contain all the header files. In the Build Phases window of Xcode, select Editor\Add Build Phase\Add Headers Build Phase.

Note: If the menu item found above is not clickable, as shown below:

Click on the white area of the Build Phases screen below to get Xcode’s application focus, then try again

Drag fbysdkDemo. h from the project to the Public section under Copy Headers. The goal here is to ensure that users can use the classes or interfaces exposed in the library.

Classes added to the Public section of Copy Headers are Public. There are three groups: Public header files, Private header files can be seen by users, and Project header files are Private. It is recommended that files be placed under Public and Project as much as possible.

3. Add the configuration

To add a configuration, click on the project name, select the FBYSDKDemo static library target, select the Build Setting bar, and search for the Public Header. Double-click the Public Headers Folder Path. Type the following in the pop-up view

include/$(PROJECT_NAME)
Copy the code

Screenshot below:

Because you’re creating static files for others to use, it’s best to disable invalid code and debug symbols and let users choose what works for their project. Search for Dead Code Stripping, Strip Debug Symbol During Copy, and Strip Style in the search box as follows:

  • Dead Code Stripping is set to NO
  • Strip Debug Symbol During Copy All set to NO
  • Set the Strip Style to non-global Symbols

So far, the project has been built. Select the Generic iOS Device as the target and press Command + B to compile. The librwuicontrols. a file in the Product directory in the project navigation bar will change from red to black, indicating that the file now exists. Right click on LibrWuicontrols.a and select Show in Finder, as shown below:

Above you can see the fbysdkDemo.h class exposed to the public. The other implementation classes are in binary form in libfBysdkDemo.a.

4. Function realization

Here to achieve the development of the static library, the functional part of a simple functional demo to example.

Implement the following code in the fbysdkdemo.h header file:

#import <UIKit/UIKit.h> #import <Foundation/Foundation.h> typedef void (^FBYSDKCompletion)(NSString *result); @interface FBYSDKDemo : NSObject /** ** @param urlType Indicates the web page type. The urltype can be iOS or Android (void)urlType:(NSString *)urltype withCompletion:(FBYSDKCompletion)completion; @endCopy the code

Urltype indicates the web page type. Urltype can be iOS or Android. Callback completion is obtained according to the URlType of the corresponding WEB page URL result.

In the implementation file fbysdkDemo. m code is as follows:

#import "FBYSDKDemo.h"

@interface FBYSDKDemo ()

@end
@implementation FBYSDKDemo

- (void)urlType:(NSString *)urltype withCompletion:(FBYSDKCompletion)completion{
    
    if ([urltype isEqualToString:[NSString stringWithFormat:@"iOS"]]) {
        
        if (completion) {
            completion(@"https://juejin.cn/post/6844903541513912333");
        }
        
    }else if ([urltype isEqualToString:[NSString stringWithFormat:@"Android"]]) {
        
        if (completion) {
            completion(@"https://juejin.cn/post/6844903528238940174");
        }
        
    }
    
}
@end
Copy the code

Select Generic iOS Device as the target and press Command + B to compile.

5. Merge static libraries

After compiling and running, right-click libRWUIControls. A and select Show in Finder. LibFBYSDKDemo.

So also select target virtual machine (such as iPhone 7), then compile and run, right click librwuicontrols. a, select Show in Finder to Show libfBysdkDemo. a will run in the virtual machine, if running in the real machine will report an error.

Merge method: open terminal, type lipo-create real library. A path simulator library. A path -output synthesis library name (can copy library. A path, change the name). Press Enter to see the newly synthesized.a file in the simulation library folder, as shown below:

Screenshot of static library file after synthesis is as follows:

Libfbysdkdemo_all. a file is a static library file, a general static library.

Three: static library use

1. Import static libraries

Import the static library and.h header files. Note the following options:

2. Call the function

The source code is as follows:

- (void)blogsBtn:(UIButton *)sender { if (sender.tag == 6000) { [FBYSDKDemo urlType:@"iOS" withCompletion:^(NSString *result) { [self contentURL:result]; }]; }else if (sender.tag == 6001) { [FBYSDKDemo urlType:@"Android" withCompletion:^(NSString *result) { [self contentURL:result]; }]; }}Copy the code

Result is the result obtained through the SDK callback.

3. The DEMO screenshot is as follows

4. This article demo source:

Static library generates demo

Static libraries use Demo

Hope to help you, if there is a problem can be added QQ technical exchange group: 668562416, if there is anything wrong or insufficient place, but also hope readers give more comments or suggestions

If you need to reprint, please contact me, after authorization can be reprinted, thank you

This post has been synchronized to my blog: FBY Zhan Fei