iOS makes static libraries framework

Previous information overview

The ways of static libraries in iOS are * A or * framework. Personal feeling no matter it is A or framework is nothing more than a way to hide the source code. In many projects, the code with few changes (high cohesion and low coupling code) is often encapsulated and stored in the library in order to increase reusability.

Cut the crap and start writing the framework static library environment for iOS: Mac OS 10.15.4 + Xcode Version 11.5.3

1. new construction

file---new---project---choice Framework & Library---Framework

1.1 the project structure is as follows

2. project configuration

Build settings---Mach-O Type---Static library
Build settings---Link With Standard Libraries---NO
Build settings---Dead Code Stripping---NO
Build settings---Strip Debug Symbol During Copy---NO
Build settings---Strip Style---Non-Global Symbols

3. add compiled file and header file

# Create or import a file, as shown in the figure h documents
#import <Foundation/Foundation.h>


@interface FOSPrintGT : NSObject

- (void)fos_printGT;



# m file
#import "FOSPrintGT.h"

@implementation FOSPrintGT

- (void)fos_printGT{
   NSLog(@"i love tian");


# The header file to be displayed after compilation is stored 
Targets---Build Phases---Headers---Public

# Stored in need of compilation m file
Targets---Build Phases---Headers---Compile Source

Note: fosafercollector H stores the sum of all header files, making it easy to import multiple header files at one time

4. compilation

  • After the above preparations are completed, you can compile. The arm architecture is compiled from the real machine selected by Device, and x86 is selected as the simulator_ 64 architecture
  • The compiled path is in /users/$xxx/library/developer/xcode/deriveddata/fosafercollector hehjvoaeceuuiddcobkfkebsvbdo/build/products

5. use

  • Copy the corresponding static library and import it into the project you want to use
  • Import header file \import <fosafercollector/fosafercollector H>
  • Call the corresponding method

6. compile script

  • Someone said, can I have a static library that can be used by both simulators and real machines? The answer is yes, script It's from github
  • Directly copy the above contents, and then go to Targets---Build Phases---+---New Run Script Phase
  • Paste the code to the position indicated by the arrow below
  • working

7. manual synthesis

  • You said I don't want to use other people's wheels. I want to synthesize by myself. Can I, Sure, look down
# First, you need to obtain the real machine and simulated static libraries respectively, and then put them in the same directory
# View static library schema information
lipo -info xxx.framework

# Merge static libraries
 lipo -create  xxx_iphone.framework  xxx_simulate..framework  -output  xxx_universal.framework

Postscript: there were similar articles many years ago. At that time, the production was very cumbersome, not so simple, and there were many subsequent optimizations, which will also be recorded in the later articles

Tags: iOS Framework github macOS xcode strip foundation

Posted by jbbadaz on Mon, 30 May 2022 13:03:06 +0530