Packaging Bundle files

During development, sometimes you need to package the SDK. When packaging the SDK, you need to put the resource files in the project into the bundle for reference.
A Bundle file can be understood as a resource package, used to store images, audio, text, nib files, etc., making it easy 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 a file
directly in the project to create a new file..bundle
command+N
Create bundle file.png

Second type: Create a bundle project,
command+shift+Ncreate a new project, select macOS, select Bundle to create a bundle project
Create bundle project.png

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

Bundle file directory.png

2. Citing file resources

Put file resources into the bundle package

/**
 资源文件    AgricultureResource.bundle下的资源

 @param name 文件名称
 @param type 文件类型
 @param path 子文件夹路径
 @return 文件路径
 */
+ (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;
}

Just read the file path directly when using it

3. Reference of picture resources

/**
 读取bundle里面的图片

 @param imageName 图片名称
 @return 返回的图片
 */
+ (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];
    //优先取上层bundle 里的图片,如果没有,则用自带资源的图片
    return image ? image : [UIImage imageNamed:name inBundle:bundle compatibleWithTraitCollection:nil];
    
}

4. Reference of xib file resources

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

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

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

Note: The nib file in the bundle file can directly reference the image resources in the same folder.

Guess you like

Origin blog.csdn.net/guoxulieying/article/details/117421931
Recommended