IOS SDK encapsulation and bundle File Processing during encapsulation, iosbundle

Source: Internet
Author: User

IOS SDK encapsulation and bundle File Processing during encapsulation, iosbundle

The main purpose of this tutorial is to explain how to create and use an SDK in your iOS project, also known as the. a file.

Environment: xcode 9.0

Create a static library project

Open Xcode, click File \ New \ Project,

Select iOS \ Framework and Library \ Cocoa Touch Static Library to create a Static Library project.

Name the project SDKTest and save the project to an empty directory.

When you create a static library project, Xcode automatically adds. h and. m.

You do not need to implement the file, so right-click SDKTest. m and choose delete to delete it.

Then the project can be used. It is time to add some features to your library.

The key of this tutorial is to teach you how to create an SDKTest.,

Instead of building a UI control, you can use the control encapsulated by yourself.

Drag your control from the Finder to the SDKTest directory of Xcode.

Here I use someone else's encapsulated calendar that I perform secondary encapsulation.

Note that you must check the following three options when dragging the control to the project:

Copy items if needed

Create gruoups

Add to targets

Another thing you need to do for the header file of your control is to add it to the main header file SDKTest. h of the library.

With the help of this header file, developers only need to import a header file to use your library,

The following code is the same, instead of selecting an imported part as needed.

Next, generate a static library.

Static libraries are divided into mobile phones and Simulators (mobile phones can only be called, and Simulators can only be called by simulators ).

First, build the static library of the simulator. (Click the triangle button to compile the library. I wrote it as a running library in the picture )!

Then show in Finder drag the. a file to the desktop,

In the same sense as the. a file of a real machine, you can change the name to facilitate the synthesis of a. a file.

Then open the terminal and execute the following command, so we won't copy it here,

Click it to remember it. a merged. a file is generated on the desktop during running.

The. a file and all the. H files related to it are stored in a new folder.

Now, you can test it with this folder. Drag it to the project you want to apply

In this way, you can reference the SDKTest. h file to use the previous control, which is actually very simple.

I encountered a problem here. This error is reported when I use it,

This can be solved as follows: simply remove the corresponding Use Safe Area Layout Guide and select it.

Thanks for this article: http://blog.csdn.net/objectivepla/article/details/51211541

However, this is only for controls that do not reference images and xib,

What if the SDK You Want To package contains xib? Such resource files need to be processed separately.

Here you need to knowBundle

Bundle is a directory that contains the resources used by the program.

These resources contain images, sounds, compiled code, and nib files (bundle is also called plug-in). Corresponding bundle,

Cocoa provides the NSBundle class.

To create a simple bundle file, you only need to create a local folder and add the suffix @ ". bundle" to the folder.

However, we need to manually convert the xib file into a nib file.

For example, the command is as follows:

ibtool --errors --warnings --output-format human-readable-text --compile /Users/Chenfy/Desktop/cc.zip/CCViewController.nib /Users/Chenfy/Desktop/cc.zip/CCViewController.xib 

In this way, you can get the nib file.

Of course, a complicated bundle file is created using Xcode.

Add required resource files

Add the resource file you need to compile in the bundle. At least the Base SDK is iOS and mac is selected by default.

For other settings, refer to Baidu

Get the. budle file is the same as get the. a file, compile it, and then show in Finder

You can modify the file name in the same way as that in the SDK.

The bundle to be used can be used after being integrated into the project.

Note that bundle is static and does not compile resource files.

Therefore, to use resources in the bundle, you need to find the corresponding resource path

Note:

Before packaging the. a file with xib, remember to change the application method of xib to nib. The corresponding image is also

References to. budle files

Use of bundle resource packages:

Drag the generated bundle file to the project to be used;

// Set the file path

NSString * bundlePath = [[NSBundlemainBundle] pathForResource: @ "SourcesBundle" ofType: @ "bundle"];

NSBundle * resourceBundle = [NSBundlebundleWithPath: bundlePath];

// Load the nib file

UINib * nib = [UINibnibWithNibName: @ "Demo" bundle: resourceBundle];

NSArray * viewObjs = [nibinstantiateWithOwner: niloptions: nil];

// Obtain the xib File

UIView * view = viewObjs. lastObject;

View. frame = CGRectMake (20, 50, self. view. bounds. size. width-40, self. view. bounds. size. width-40 );

[Self. view addSubview: view];

  VC Obtains resources in bundle

NSString * bundlePath = [[NSBundle mainBundle] pathForResource: @ "MyBundle" ofType: @ "bundle"];

NSBundle * resourceBundle = [NSBundle bundleWithPath: bundlePath];

UIViewController * vc = [[UIViewController alloc] initWithNibName: @ "vc_name" bundle: resourceBundle];

  Image to obtain resources in bundleUIImageView * imgView = [[UIImageView alloc] initWithFrame: CGRectMake (50, 50, 50, 50)];

UIImage * image = [UIImage imageNamed: @ "MyBundle. bundle/img_collect_success"];

[ImgView setImage: image];

 

Use the following code when loading the cell generated by xib in the bundle to load nib. The most important thing is to obtain nib from the bundle.

NSString * bundlePath = [[NSBundle mainBundle] pathForResource: @ "WofuSDKBundle" ofType: @ "bundle"];

NSBundle * resourceBundle = [NSBundle bundleWithPath: bundlePath];

UINib * nib = [UINib nibWithNibName: @ "Wofucreditcell" bundle: resourceBundle];

[Tab registerNib: nib forCellReuseIdentifier: identifier];

Loading bundle details see this article http://blog.csdn.net/u011146511/article/details/70147693

I encountered a problem here. I always reported an error during cell multiplexing.

The result shows that when there is no reusable cell, it should also be retrieved from. budle. The basic knowledge is not strong,

This is a drawback of xib!

 

 

In addition, I printed whether the budle is successfully loaded, but the result is (not yet loaded). I don't know why,

The final result is a successful loading. If you know it, @ me. Thank you!

Last successful image

 

 

 

 

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.