The iOS encapsulation feature generates. Framework

Source: Internet
Author: User

Objective

If you want to share the controls you have developed with others, one way is to provide the source code files directly. However, this approach is not very elegant. It exposes all implementation details , which you might not want to open up. Also, developers may not want to see all of your code, because they may just want to embed part of your beautiful code into their app.

Another way is to compile your code into a static library that allows other developers to add to their own projects. However, it is very inconvenient for you to publish all of the public header documents together.

You need an easy way to compile your code, which should make your code easy to share and reusable across multiple projects. What you need is a way to package your static library and put all the header files in a single unit so you can immediately add them to your project and use them.

OS x perfectly supports this because Xcode provides a project template that contains the default build target and files that can hold resources like pictures, sounds, fonts, and so on. You can create the framework for iOS, but this is a complicated manual work, and if you follow the tutorial, you'll learn how to cross a roadblock and successfully complete the creation of the framework.

Comparison

You can refer to this article. The difference between a and. framework.a and. Framework.


We can see that. A's package is similar to the. Framework package, as well as the process of merging simulators and real machines, as we can see from the picture above. The difference between a and. Framework is. A+.h+sourefile=.framework. It can be seen that we are directly encapsulated. The framework is actually the best. So let's see how the framework is packaged.

In addition, for the encapsulation of. A, you can refer to how iOS generates. A Files

Goal

This article will be based on Xcode7 to create a simple project, in two ways to teach you how to make a framework of their own, the purpose is easy to learn the production of the framework. This approach makes it easy to share your code, reuse it in multiple projects, and hide implementation Details to control the exposed header files .

Steps

1, open Xcode, new project.

Do not select "Application" and select "Framework & Library". Select First, then next.


2, create the function class.

Here I create a SayHello class that inherits from NSObject

3, realize the function.

Declare the method inside the newly created class and implement it. Here I write a SayHello method so that the later tests are used.


4. Changing Parameters

Under Targets, select the project and change several parameters under Build settings.


5. Increase armv7s

Under architectures, add armv7s and select. Set build Active Architecture only to No.


6. Set headers

drag The header file you want to expose to public, hide it under private or project, and, of course, the hidden header file can no longer be referenced.


You then need to introduce all of the. h that you want to expose in Test.h (must be public, otherwise it cannot be referenced).


Packaging framework

The first of these methods

1. Check the emulator, compile the program

2. Check the test machine, compile the program

3. Locate the framework file in the Finder


Select the framework shown in the diagram and right-click the show in Finder.

Find the test file shown in, one is Debug-iphoneos (real machine), one is under the Debug-iphonesimulator (simulator).


4. Use terminal commands to combine two frameworks into a single emulator and a framework that is available to the real machine.

Open the console input lipo-create Iphoneos under Frameworktest path simulator frameworktest The path-output the new path, thus completing the merger of the emulator and the real machine version, The frameworktest under the new path is your merged file, change the name of the file to the same name as the test that you did not merge, and put it under the Framework folder and replace the original Frameworktest file.

The above said the mess, can not see clearly, here to explain to you, look at the following diagram: Open the terminal , manually enter the red line lipo-create command, Then the Green Line is iphoneos under the Frameworktest path (find Iphoneos under the Frameworktest file, dragged in ), will automatically have a space, Purple Line is simulator under Frameworktest Path (also find simulator under Frameworktest file, dragged in ), will automatically have a space, and then enter-output, and then tap the space , in the introduction of a new path ( dragged into a new path ), and finally hit enter. This completes the merge.


The above command is to merge the frameworktest of the real machine and the simulator into a mynewframeworktest file and store it in the new folder on the desktop.

Here we will encounter an error when merging, which is why we do not know, but will be with our-output folder path side-by-side to generate a. lipo file, this. lipo file we'll talk about this.



Note: After the merge is complete, a file such as the. Lipo format appears.


What is this TM, not supposed to appear a similar? Shouldn't suffixes have nothing? What's the suffix? Lipo, what is this file?!


Our operation is according to the people said to change the name of the synthesized file to replace the original Myframeworktest . Also, remove the suffix. Lipo!

In accordance with the above mentioned, replaced the original.

Then you can proceed to the next step.

5. Save the modified framework by copying it, and this is the framework we made.

The second method of

1, select the project under targets, click on the upper Editor, select Add Target to create a aggregate.


2. Select the aggregate under other and click Next to create.


3, embed the script. Select the aggregate you just created, then select the build phases on the right, click the lower left plus sign, and choose New Run Script Phase


Copy this script in:

# Sets the target folders and the Finalframework product.# if the project name and the framework's target name are different, customize fmkname# for example: Fmk_name = "MyF Ramework "fmk_name=${project_name}# Install dir'll be the final output tothe framework.# the following line create it in The RootFolder of the current project.  install_dir=${srcroot}/products/$      {fmk_name}.framework # working dir'll be deleted after Theframework creation.  wrk_dir=build  device_dir=${wrk_dir}/ release-iphoneos/$                        {fmk_name}.framework   simulator_dir=${wrk_dir}/release-  iphonesimulator/${fmk_name}.framework#-configuration ${ configuration}# clean and Building both architectures.xcodebuild-configuration "Release"-target "${fmk_name}"-sdk Iphoneos clean build  xcodebuild-configuration "Release"-target "${fmk_name}"-sdk Iphonesimulator clean          build# cleaning the oldest.if[-d "${install_dir}"]tHenrm-rf "${install_dir}" Fimkdir-p "${install_dir}" Cp-r "${device_dir}/" "${install_dir}/" # Uses the Lipo Tool to merge Both Binaryfiles (i386 + armv6/armv7) into one      Universal final product.  lipo-create "${device_di R}/${fmk_name} "" ${simulator_dir}/${fmk_name} "-output" $                  & nbsp   {install_dir}/${fmk_name} "Rm-r" ${wrk_dir} "open" ${install_dir} "

Here is a misunderstanding, is to copy the upper part of the script, will be in the effect we expect a few more returns, these several return is fatal, if not delete the carriage return, will report the following error:



The final format is, as far as possible, a carriage return can not be wrong:


Through the first method of " combining the frameworktest of the real machine and the simulator into one " and comparing the scripting language above, we can find that the difference between the two coinage, two methods used in both "Lipo-create xxx" and "-output xxx ", different place is the first method needs our own real machine and simulator mutation again, and we need to drag the framework path, compared to the second method is relatively simple.

4, compile. , Command+b compiled. Here generic iOS device means "iOS universal Device", which is probably the case with both the simulator and the real machine.


5, after the successful compilation will automatically jump out of a finder, save this. Framework, this is the framework we need.


At this point, two approaches to packaging the framework are complete!

Finally, with our framework, pour into another xcode, we open this framework to see that only headers, There are two. h, one of which is the FrameworkDemo.h file we added earlier, and the other is our SayHello.h.


Then introduce the header file:


Since the method we are testing is an instance method, we instantiate an instance object and then we can get the instance object to the appropriate method:


At this point, complete the production and use of the framework.

Summarize

Finally, it is important to note that:

1. The external leakage of the. h file must be guaranteed to be the one you want to leak out. Do not want to leak outside the leak.

2, start packaging, be sure to check the simulator and selected the top of the real machine to compile separately, I think before the home without the real machine when the compiler seems wrong.

3, at the end of the merger of the terminal may be error and generate a. lipo file, do not be afraid, bold to modify the same name without hanging the suffix of the same name file.

4, when the call is clear whether the class method or instance method, convenient to call.

5, in the production of the framework or LIB, if the use of category, then use the program to change FMWK will be crash, at this time need in the project other linker flags add two parameters-objc-all_load. (This is not pro-test)

6, with the picture resources need to package the picture into bundle files, and the framework to copy to the corresponding project.

7, the public class if the reference to the private class, packaging after the external error, cannot find the private class, you can put that private. h (also not tested)

8,namespace conflict. the Static library uses a third-party library, the project also uses the same third-party library, at compile time there will be duplicate symbol error, because there are two copies of the same third-party library. The solution is to add a custom prefix to the third-party library used, including the class name, the delegate protocol, the constant name, and, in particular, the method name of the category to be modified.

9, packaging the static library should try to avoid the introduction of heavyweight third-party libraries, more self-encapsulation .

10, a static library to have its own unique prefix , all class names, constants, and so on must add the same prefix.

11, Real machine + simulator support . (as in 2nd) Xcode will only generate a static library with the current environment (real machine or simulator), so the SDK is not easy to debug when developing other projects. The solution is to generate a generic library from a script, Build_universal_library.sh,via so.

12, documentation . The convenience of the static library is the user directly to use the method you provide, do not pay attention to the specific implementation, inconvenient to see the implementation, problems can not be detected, it is necessary to the SDK version, update history, use, FAQs and other written documents, easy to use, also appears to be the SDK more formal specifications.

13, pictures and other resource files bundle Way package. A simple way to make bundles: Create a new folder, rename it to Yoursdk.bundle, then Show package Contents Open and add a picture. You need to specify the bundle when using the picture: [UIImage imagenamed:@ "Yoursdk.bundle/img.png"]. You can also create bundles using Target, such as IOS Library with resourceshttp://www.galloway.me.uk/tutorials/ios-library-with-resources/.

14, if the SDK is useful to the Category, note the project settings other Linker Flags add-OBJC. (The role of-OBJC is described in the rear)

Add

Compile process:

Steps from C code to executable: source code > Preprocessor > Compiler > Assembler > Machine code > Linker > Executable

In the final step, you need to link the. o file with the C language runtime, which requires the use of the LD command. After a series of processing of the source files, a corresponding. obj file is generated, and then a project will inevitably have many. obj files, and there will be a variety of connections between these files, such as function calls. The thing that the linker does is to link these target files with some of the libraries used to form a complete executable file. The value set by other linker flags is actually the parameter that is added after the LD command executes

The following 3 common parameters are described individually:

-OBJC: When this parameter is added, the linker will load all the Objective-c classes and categories in the static library into the final executable file

-all_load: will let the linker load all the found target files into the executable file, but do not use this parameter arbitrarily! If you use more than one static library file, and then use this parameter, then you are likely to encounter ld:duplicate symbol error, because different library files may have the same target file, so it is recommended to use when encountering-OBJC failure-force_ The load parameter.

-force_load: What you do is exactly the same as-all_load, but-force_load needs to specify the path to the library file to be loaded completely, so you simply load a library file without affecting the rest of the library file's on-demand loading

A later attempt to encapsulate the Bezier appeased demo into the framework may also increase the way the bundle files are generated.

Reference from 1, ios-production framework (latest)

2. ios--Create your own framework

Finally, where the wrong place can give me a message, I will improve in time, thank you.



Han Cat
Links: http://www.jianshu.com/p/87dbf57cfe4a
Source: Pinterest
Copyright belongs to the author. Commercial reprint please contact the author for authorization, non-commercial reprint please specify the source.

The iOS encapsulation feature generates. Framework

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.