Create Framewok under iOS (top)

Source: Internet
Author: User

In iOS development, sometimes it is necessary to wrap up some tools to make the SDK available to customers, and the process can be divided into two types. One is to make a static library. A file, which is made into a framework file. The former is simple to operate, but it is cumbersome to provide to the customer and requires a header file. The latter is easy to use and can be imported directly, but the production is more troublesome.

Apple does not support the Dynamic Library framework approach before IOS8, so the framework is essentially a static library file. If there is any understanding and collation of the wrong place, you are welcome to point out the problem.

This article mainly records the methods for creating the framework. There are two ways to create the framework, one for Xcode6 before, and one for the production of Xcode6. This blog first explains the latest version of Xcode7 's approach to making the framework.

Xcode6 has provided a way to make dynamic libraries. Here is also the case of Xcode7, the operation of the two steps are the same. Create a new project and select the Framework & Library options under iOS:



Once created, select the targets you created, search for mach-o in buildsetting, and set it to the static Library:



It then begins by writing a class to make the framework, providing another project for testing, and when you create a new class file, you need to expose the header file of the newly created class in the default ZYXTestFramework.h, or else the header file will appear in the other project. Missing XXXXX "Warning.



Write a test method in the newly created class, print the passed string, and the header file exposes the method:


When you are finished, select the build phase option under targets and you will need to expose the header file of the newly created class to the developer calling using the framework. There are three options public,private,projects under headers. Perhaps the middle of the private name will create some confusion, private does not mean that it is private, it is exposed to something, projects is a private file (the explanation is seen from an online tutorial, is not too clear at present, In general, however, the public and projects two are used. Move the ZYXFrameworkTest.h to public.





Now it's ready to compile, but here's the explanation: we're making the framework for an iphone with more than one architecture, the general simulator uses x86 64 and i386, and the real machine is mostly arm64 and armv7. Armv7s Many SDKs are not specific to compiling him, like Facebook, I see that their supported architectures do not include armv7s. Some tutorials on the web say that there are two options that need to be changed, which is not really necessary for this blog post.


At compile time, set to the release state for the framework generation. Here, sir. To support the release version of the simulator, select Editscheme and change the run state of debug to release.



Run, generate the framework. You can view the schema supported by the generated framework, open the generated framework's location in the Finder, drag to the terminal, and use the Lipo command to view the supported schemas. Where the Zyxtestframework file is essentially a static. a file.



You can see that the supported schemas are i386 x86 64. Then generate a Framewok for the real machine, because there is no real machine at hand, no way. The build step is the same as the above, as long as you replace the real machine device.

After the final build, you need to merge two files to support 4 schemas, which, when merging, is essentially static. A zyxtestframework file. Execute lipo-create (zyxtestframework path generated by emulator) under terminal (the path generated by the real machine)-outfile the path and name after merging


The above build operations can also be executed directly using scripts, creating a new target in Xcode and selecting aggregate. The target can be used to execute a script build project.


When you're done, click the + sign under build phase, select New copy Run scripts, and add a script



The script reads as follows:

Set-e

# Sets the target folders and the final framework product.

# if the project name and Framework of the Target if the name is not the same, you need to customize Fmkname

# For example : Fmk_name = "Myframework"

Fmk_name=${project_name}

# Install Dir'll is the final output to the framework.

# The following line create it at the root folder of the current project.

Install_dir=${srcroot}/products/${fmk_name}.framework

# working dir would be a deleted after the framework 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}" ]

Then

RM-RF "${install_dir}"

Fi

mkdir-p "${install_dir}"

Cp-r "${device_dir}/" "${install_dir}/"

# Uses The Lipo Tool to merge both binary files (i386 + armv6/armv7) into one Universal final product.

lipo-create "${device_dir}/${fmk_name}" "${simulator_dir}/${fmk_name}" -output"${install_dir}/${fmk_name}"

Rm-r "${wrk_dir}"


Open "${install_dir}"

Note: When using this script " xcodebuild-configuration "Release" -target "${fmk_name}" -sdk Iphoneos Clean Build "This script directly performs a real machine generation, so just like using a real-machine debug, you need a signing certificate. Otherwise the build will fail.


The target needs to be adjusted during the build and the run operation is performed on aggregate.  Eventually a products catalog is created in the current project directory, where there will be a composite framework. But there will be a _codesignature folder, which should be the real machine debug signature generated folder. Because I don't see this folder in the framework of the Facebook SDK, I don't know if deleting this _codesignature will have any effect. The user will also be able to sign the framework when using the framework, and if anyone can help, thank you very much.


Put the resulting framework into the test project, import the header file, and test it. Because there is no cell phone, so directly with the simulator test, this is the above problem is not verified the reason. The final test results are as follows: (as if the image can not be uploaded, import header files and calls are gone, too big)



You can see that the API is finally successfully called into the framework. This is the Xcode7 generation framework method, I personally feel that eventually still the library, should not be rejected by Apple, and Facebook compared to find that there is a _codesignature folder, this app real machine signature folder after deletion do not know if there is no impact.

The above blog, I implore you to have the ability of the people can teach the doubts. Thanks again and again.


Create Framewok under iOS (top)

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.