Package Bundle file

During development, sometimes SDK needs to be packaged. When packaging the SDK, you need to put the resource files in the project into the bundle for reference.
The Bundle file can be understood as a resource package, which is used to store pictures, audio, text, nib files, etc. it is convenient to reference the resources in the package in other projects.

1, Create bundle file

There are two ways to create a bundle file
The first one: create directly in the project bundle file
command+N create new file

The second is to create a bundle project
command+shift+N create a new project, select macOS, and select bundle to create a bundle project

Modify the corresponding parameter configuration
"Base SDK" -> represents the version of the highest SDK supported by Xcode. It will guide the compiler to use this version of SDK to compile and build applications, mainly involving API s. When you create a bundle, you use Mac OS and need to change it to iOS system
"Build Active Architecture Only", and the compiled architecture is set to "YES"
Set Debug Information Format to DWARF with dSYM File
"OS X Deployment Target" is set to "Compiler Default"
The "Release" mode in "Strip Debug Symbols During Copy" is set to "YES"
"IOS Deployment Target" is set to the minimum system version you need to support. For example, after your minimum support system iOS 8.0 is configured, run it to generate a bundle file

2, Reference file resources

Put the file resources into the bundle package

 Resource file agricultureresource Resources under bundle

 @param name File name
 @param type file type
 @param path Subfolder path
 @return File path
+ (nullable NSString *)pathFileForResource:(nullable NSString *)name type:(nullable NSString *)type path:(nullable NSString *)filepath {
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"AgricultureResource.bundle" withExtension:nil];
    NSBundle *bundle = [NSBundle bundleWithURL:url];
    NSString *path = [bundle pathForResource:name ofType:type inDirectory:filepath];
    return path;

When using, you can directly read the file path

3, Reference of picture resources

 Read the pictures in the bundle

 @param imageName Picture name
 @return Returned picture
+ (nullable UIImage *)AGRI_imageNamed:(NSString *)imageName {
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"AgricultureResource.bundle" withExtension:nil];
    NSBundle *bundle = [NSBundle bundleWithURL:url];
    NSString *name = [@"images" stringByAppendingPathComponent:imageName];
    UIImage *image = [UIImage imageNamed:imageName];
    //Give priority to the pictures in the upper bundle. If not, use the pictures with their own resources
    return image ? image : [UIImage imageNamed:name inBundle:bundle compatibleWithTraitCollection:nil];

4, xib references to file resources

The bundle package is static and does not participate in compilation, which means that the bundle package cannot contain executable files. It is only as a resource, which is parsed into specific binary data.
The xib put into the Bundle file needs to be compiled into a nib file, because the Bundle file will not be compiled after it is put into the project. If you directly put the xib into the Bundle file, you will report a failure to load the nib resource file after starting the project.

How to make xib as nib file:
In the bundle project created, you can package xib into nib files
There are also commands and scripts for making nib on the Internet Nib in iOS development - Bundle file (Xib compiled into nib)
**Note: * * sometimes the nib files generated can only be used in systems after 11.0,
For example, objects-11.0 + nib this sub file.

+ (UINib *)bunldeNibWithName:(NSString *)name {
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"AgricultureResource.bundle" withExtension:nil];
    NSBundle *bundle = [NSBundle bundleWithURL:url];
    // Path in bundle
    NSString *nibName = [@"nib" stringByAppendingPathComponent:name];
    return [UINib nibWithNibName:name bundle:nil];

Note: the nib file in the bundle file can be directly referenced to the picture resources in the same folder

Added by jmboblee on Mon, 07 Feb 2022 13:40:42 +0200