preface

  • Create SDK encapsulation Framework with resource file encapsulation

1. Create a new main project

The main project’s ProjectName is HelloFramework (the SDK’s main project)

Create Framework

Create the Framework in the main project and temporarily name it MyFramework

Project – > Editor – > Add Larget – > Cocoa Touch Framework

3. Configure Framework information

1. Architectures configuration supports the set of directives

  • Project – > Target – > MyFramework – > Build Settings
  • Architectures configuration supports arm64E and ARMV7s instruction sets

As follows:

Note: Arm64 and ARMV7 are configured by default

Instruction set Supported devices
armv6 IPhone, iPhone 3G, iPod 1G, iPod 2G
armv7 IPhone 3GS, iPhone 4, iPod 3G, iPod 4G, iPod 5G, iPad, iPad 2, iPad 3, iPad Mini
armv7s IPhone 5, iPhone 5C, and iPad 4
arm64 IPhone 5S, iPhone 6, iPhone 6P, iPhone 6S, iPhone 6sP, iPhone 7, iPhone 7P, iPad Air, Retina iPad Mini
arm64e IPhone XR and iPhone XS Max

2. Change Build Active Architecture Only to NO. Otherwise, the generated static library will support Only the Architecture of the currently selected device.

Build Active Architecture Only Change the value to NO

Mach-O Type: Static Library or Dynamic Library

Mach-O Type = Static Library

Note: Linked Frameworks and Libraries should be included in the Linked Frameworks and Libraries.

As follows:

4. Set Headers Phase

Target – > MyFramework – > Build Phases – > Headers

  • Public: the H file to be exposed
  • Private: an H file that you do not want to make public
  • Project: Displays all the H files you created in your MyFramework

5. Set the lowest compatible version

As follows:

Create a FrameworkManager file

A method is implemented in the FrameworkManager class

#import <Foundation/Foundation. H > @interface MyFrameworkManager: NSObject + (UIViewController*)creatFrameworkFileViewController; @endCopy the code
/ / m file to realize # import "MyFrameworkManager. H" # import "FrameworkFileViewController. H" @ implementation MyFrameworkManager + (UIViewController*)creatFrameworkFileViewController{ FrameworkFileViewController *vc = [[FrameworkFileViewController alloc] initWithNibName:GetNibName(@"FrameworkFileViewController") bundle:[NSBundle mainBundle]]; NSLog(@"subviews:%@",vc.view.subviews); return vc; } @endCopy the code

7. Import header files

H files that need to be exposed are imported with #import. Remember to import all.h files that need to be exposed, that is, all.h files added to Headers -public above. Otherwise, compiled. Framework references will be warned. As follows:

8. Back to the main project, refer to MyFrameworkManager

/ / / introduction of the header file # import < MyFramework/MyFramework. H > / / main project of UIViewController * vc = [MyFrameworkManager creatFrameworkFileViewController];Copy the code

9. Generate Framework package

Package Framework: There are real machine and emulator. The two generated frameworks are different. (If you need to build one for both the real machine and emulator, build it separately and then merge it together). Select the compiled Device as the real machine according to the following figure, and then press Command + B to start the compilation. After the compilation is successful, right-click the.framework file in the Products folder and click Show in Finder.

Iv. Storage and reference of XIB files and pictures

Note: Resource files are stored in the Bundle file. If you put them in the Framework file, they will appear later when you package and upload themFound an unexpected Mach-O header code: 0x72613c21

Create bundle, place resource file (NIB file, image)

Create a new bundle file named kjFramework.bundle

As follows:

2. Display the package content and put the resources such as pictures into the bundle file

As follows:

The first compiles to a NIB file

1. Compile xiB files into NIB files

  • Open the terminal: CD the XIB directory to be converted
  • Input compilation: ibtool –errors –warnings –output-format human-readable-text –compile ibtool –errors –warnings –output-format human-readable-text –compile FrameworkFileViewController.nib FrameworkFileViewController.xib

When compiled, the following files are generated:

The second generates niB files

1, compile files, Command + B generate Framework files

2, Show in the Finder Framework document, find a FrameworkFileViewController. The nib file

Note: Once the XIB file changes, you need to recompile the NIB file and then replace it

3, read the image from the bundle resource bundle

We import the Bundle file into our framework, and when we use the image, we use the image from the Bundle.

/ / FrameworkFileViewController. M file to realize # import "FrameworkFileViewController. H" @ interface FrameworkFileViewController () @property (unsafe_unretained, nonatomic) IBOutlet UIImageView *imageView; @end @implementation FrameworkFileViewController - (void)viewDidLoad { [super viewDidLoad]; self.imageView.image = [UIImage imageNamed:GetBundleImage(@"tiaotiaosu")]; } - (IBAction)changeImage:(UIButton *)sender { if (_imageView.highlighted) { self.imageView.image = [UIImage imageNamed:GetBundleImage(@"jienigui")]; }else{ self.imageView.highlightedImage = [UIImage imageNamed:GetBundleImage(@"kabisou")]; } self.imageView.highlighted = ! self.imageView.highlighted; } - (IBAction)dismiss:(UIButton *)sender { [self dismissViewControllerAnimated:YES completion:nil]; } @endCopy the code

5. Introduce third-party libraries

1. Select Framework file when pod

2. When using the Framework files, you also need to import the required third-party libraries

Export and documentation of the Framework

1. Switch to Release mode

Product –> Edit Scheme –> Build Configuration

2. Export Framework

(1) if Target is MyFramework, Select the emulator and Generic iOS Device Command + B once (2) in the project directory Products -> right click Framework -> Show in Finder. One is the emulator package.

  • Real package: Release-iphoneos
  • Simulator package: Release-iphonesimulator

Check the support framework: lipo – info/MyFramework path. Framework/MyFramework

Armv7 ARM64 ARMV7s ARM64E Indicates the real machine

I386 X86_64 Indicates that it is a simulator

(3) Replace one of the synthesized MyFramework packages, and then the MyFramework. Framework is what we need

Merger: Lipo – create real machine path/MyFramework framework/path/MyFramework MyFramework simulator framework/MyFramework – the output Real machine path/MyFramework framework/MyFramework

Check the package support framework again: if both are present, the merge is successful

Friendly tips: practice proved to make the SIMULATOR I386, X86_64 is not useful, and later upload will also report errors, let you eliminate the two framework.

Vii. Summary of bugs

1. Error: Invalid Bitcode signature

clang: error: linker command failed with exit code 1 (use -v to see invocation)

  • Cause: The Deployment Target version is lower than the minimum version required by the Framework
  • Solution: Change the Deployment Target version

2, Could not load NIB in bundle

  • Cause: No file was found when loading NIB
  • Solution:

Targets -> Build Phases -> Link Binary With Libraries

Ld: symbol(s) not found for architecture X86_64

  • Cause: X86_64 is missing in the Framework file, which is the simulator Framework
  • The reason why I appear is because I only synthesized the Framework file of the real machine, so in the simulator run time report lack of Framework, but in the real machine can run normally.
  • Generate simulator Framework file and real machine Framework file, and then synthesize the two files.

Ld: symbol(s) not found for architecture X86_64

clang: error: linker command failed with exit code 1 (use -v to see invocation)

  • Cause: The required tripartite library was not introduced
  • Solution: Three-party library needed by POD

As can be seen from the figure lack MJRefresh and CHTCollectionViewWaterfallLayout

5. All object files and libraries for bitcode must be generated from Xcode Archive or Install build for architecture armv7

The third – party static library introduced in the project is debugging without problems, but when packaging, an error is reported

  • Cause: Third-party libraries are not compatible with XCode7later and BitCode is enabled by default
  • Solution:
  • First: Update the Framework file to include Bitcode (armv7).
  • Second: Select the project and set ENABLE_BITCODE to NO in Build Settings

6, Found an unexpected Mach -o header code: 0x72613c21

There was an error when uploading after packing!!

  • The first reason is that the Framework is a Static Library, and I added it to Embedded Binaries.

  • Solution:

  • The first: Delete static Framework files (KJframework.framework) from Embedded Binaries but if you delete them directly you will see the following Linked Frameworks and The Framework file in Libraries is also missing. The Linked Frameworks and Libraries need to be re-added.

  • The second is to re-wrap the Framework files into a Dynamic Library. The Linked Frameworks and Libraries need to be added to the Linked Frameworks and Libraries.


  • Second reason: Add Framework files to Copy Bundle Resources
  • Solution: Remove the Framework files from Copy Bundle Resources. You may not be able to load the Resources you have wrapped in the Framework files, so you need to extract the Resources and install them in bundles.

7, dyld: Library not the loaded: @ rpath/KJFramework framework/KJFramework

  • The reason:
  • Solution: Add the Framework file here

8, “Unsupported Architectures. The executable for yht. Temp_caseinsensitive_rename. The app/Frameworks/VideoCore framework contains unsupported architectures ‘[x86_64, i386]’.”

  • Reason: The self-built SDK contains x86_64 and i386 architecture, which is not allowed in the AppStore
  • Solution: Eliminate x86_64 and i386 architectures
  • TARGETS -> Build Phases -> Select New Run Script Phase -> copy and paste the following code

APP_PATH="${TARGET_BUILD_DIR}/${WRAPPER_NAME}" # This script loops through the frameworks embedded in the application and # removes unused architectures. find "$APP_PATH" -name '*.framework' -type d | while read -r FRAMEWORK do FRAMEWORK_EXECUTABLE_NAME=$(defaults read "$FRAMEWORK/Info.plist" CFBundleExecutable) FRAMEWORK_EXECUTABLE_PATH="$FRAMEWORK/$FRAMEWORK_EXECUTABLE_NAME" echo "Executable is $FRAMEWORK_EXECUTABLE_PATH" EXTRACTED_ARCHS=() for ARCH in $ARCHS do echo "Extracting $ARCH from $FRAMEWORK_EXECUTABLE_NAME" lipo -extract "$ARCH" "$FRAMEWORK_EXECUTABLE_PATH" -o "$FRAMEWORK_EXECUTABLE_PATH-$ARCH" EXTRACTED_ARCHS+=("$FRAMEWORK_EXECUTABLE_PATH-$ARCH")  done echo "Merging extracted architectures: ${ARCHS}" lipo -o "$FRAMEWORK_EXECUTABLE_PATH-merged" -create "${EXTRACTED_ARCHS[@]}" rm "${EXTRACTED_ARCHS[@]}" echo "Replacing original executable with thinned version" rm "$FRAMEWORK_EXECUTABLE_PATH" mv "$FRAMEWORK_EXECUTABLE_PATH-merged" "$FRAMEWORK_EXECUTABLE_PATH" doneCopy the code