Creating a reusable frame framework in iOS
In iOS development, we often use some of our packaged management classes, framework classes, method classes, and so on, when we implement these files, we may also rely on some third-party libraries or system libraries. It would be a waste of our energy to import the associated stuff every time we reuse the code, and even to compile the arc and MRC settings. In addition, if the project requires more than one person to cooperate, you may not want your source code burst in front of everyone, this time, we can use the static library or dynamic library way to our code packaging, easy to reuse. The method of making a static library is described in an old blog: http://my.oschina.net/u/2340880/blog/398887. Dynamic libraries are more efficient and more packaged than static library files, where dynamic libraries are mainly discussed.
XCODE6 supports the creation of dynamic libraries in Xcode, and the process is simple.
To create a new project, select the framework:
After that we write our code in, for example we create a MyObject class:
@interface myobject:nsobject-(void) myLog; @end @implementation myobject-(void) mylog{NSLog (@ "framework");} @end
similar to the static library, if we do not do any processing, the package of library files can only be used in the simulator or only on the real machine, in order to facilitate our debugging, we can add a script command, yes, build a framework that supports both the simulator and the real machine:
New target:
Select aggregate:
After that, we click the plus sign in Target's build phases:
Add a run Script:
Add the following script to the inside:
set -eset +u# avoid recursively calling this script.if [[ $SF _ Master_script_running ]]thenexit 0fiset -uexport sf_master_script_running=1sf_target_name= ${project_name}sf_executable_path= "${sf_target_name}.framework/${sf_target_name}" SF_WRAPPER_NAME= "${SF_TARGET_ Name}.framework "if [[ " $SDK _name " =~ ([a-za-z]+) ]]thensf_sdk_platform=${bash_rematch [1]} elseecho "could not find platform name from sdk_name: $SDK _name" exit 1fiif [[ "$SDK _name" =~ ([0-9]+.*$) ]]thensf_sdk_version=${bash_rematch[1]} elseecho "could not find sdk version from sdk_name: $SDK _name" exit 1fiif [[ "$SF _sdk_platform" = "Iphoneos" ]]thensf_other_platform= iphonesimulatorelsesf_other_platform=iphoneosfiif [[ "$BUILT _products_dir" =~ (. *) $SF _sdk_ Platform$ ]]thensf_other_buiLt_products_dir= "${bash_rematch[1]}${sf_other_platform}" elseecho "Could not find platform name from build products directory: $BUILT _products_dir "exit 1firm - rf buildproductsmkdir buildproducts# build the other platform.xcrun xcodebuild -project "${project_file_path}" -target "${target_name}" -configuration "${configuration}" -sdk ${sf_other_platform}${sf_sdk_version} build_dir= "${BUILD_DIR}" objroot= "${objroot}" build_root= "${build_root}" symroot= "${symroot}" $ACTION # smash the two static libraries into one fat binary and store it in the .frameworkxcrun lipo -create "${built_products_dir}/$PRODUCT _ name.framework/$PRODUCT _name " " ${sf_other_built_products_dir}/$PRODUCT _name.framework/$PRODUCT _name " -output "${project_dir}/buildproducts/$PRODUCT _name "cp -rf ${built_products_dir}/$PRODUCT _name.framework ${project_dir}/ buildproductsmv ${project_dir}/buildproducts/$PRODUCT _name ${project_dir}/buildproducts/$PRODUCT _ Name.framework
Next, we need to expose the interface file to the outside world and move it to public:
After we run the program, it is important to note that if you want to support 64-bit, you need to set in the compilation options, as follows:
At this point, our framework library file is finished, select our project in Xcode window->projects, click the small arrow to enter the folder:
We can find our framework file in Build->product and we can use it when we assign it.
Let's test it, create a new project, import the static library just made, such as the next header file, call method, can be used.
#import <MyFramework/MyObject.h> MyObject * obj = [[MyObject alloc]init]; [obj MyLog];
Two tips:
First, if you run a program similar to Reason:image not found! crash information, the possible reason is that some files in the dynamic library file your project has been included in the build phases to change the required to optional.
Second an excellent and complete framework may contain quite a few files, including the framework of its own and other third parties, it will be inconvenient to put all the header files in a headers folder, we can classify the files in folders, Simply double-click the point into the Frameword file, create a new folder directly in the Headers folder, and then categorize the file, as follows:
In this way, our framework will be clearer and more divergent, and we can continue to nest the framework inside to further optimize the code of the project.
Creating a reusable frame framework in iOS