How to Use xcode4 to create a static library
If the original Yusong Momo article is reprinted, please note: Reprinted to my independent domain name blog Yusong Momo program Research Institute
, Original address: http://www.xuanyusong.com/archives/606
Note: Make sure that your xcode4 uses ios5; otherwise, download ios5 to continue reading.
IOS programs are composed of objective-C, but every class in objective-C is divided into. H. M files. Static libraries can encapsulate the classes of these programs into one. file a, third-party applications only need to get this. file a corresponds to the code. the hfile can be encapsulated in a static library. In general, the IOS static library is suitable for creating third-party sdks. If we don't talk much about it, let's go straight into the question.
First, open xcode4 to create a static library project. For example, select framework & library-> cocoa touch static library (create static library) and click "Next" in the lower right corner to continue the creation.
After the project is created, we start to write code. We create a uiview class to display an image and click the view to open a URL.
Create a. h file for the View class
#import <UIKit/UIKit.h>@interface MyView : UIView@end
Create a. m file for the View classThe displayed image resource is the Google logo downloaded through the URL. Click the logo view and the program will open the Baidu homepage.
# Import "myview. H "@ implementation myview-(ID) initwithframe :( cgrect) frame {// initialize the view position self = [Super initwithframe: frame]; If (Self) {} return self ;} -(void) drawrect :( cgrect) rect {// create an image view uiimage * image = [[uiimage alloc] initwithdata: [nsdata datawithcontentsofurl: [nsurl urlwithstring: @ "http://www.google.com.hk/intl/zh-CN/images/logo_cn.png"]; [Image drawinrect: cgrectmake (0, 0, self. frame. size. width, self. frame. size. height)]; [Image release];}-(void) touchesended :( nsset *) touches withevent :( uievent *) event {// click the view to open the webpage [[uiapplication sharedapplication] Openurl: [nsurl urlwithstring: @ "http://www.baidu.com"];} @ end
The code in this static library has been written, and now we start to create a static library. Static libraries can be created in two ways: static libraries used on real machines and static libraries used in simulators. The two methods are slightly different. Please check them out. Don't blink your eyes,Let's start with the simulator.
First, select the simulator iPhone 5.0 simulator. If your xcode4 is not an ios5 version, you will not have this option. Then build the project and libsdklib after the project is built. A static library file is generated again. Right-click the file and find it.
After the file is found, the default value is debug-iphoneos/libsdklib. A. However, this file cannot be used in the simulator and cannot be used in the real machine. You need to select the debug-iphoneosimulator/libsdklib. A file below, drag libsdklib. A and the corresponding myview. h header file into the project to use it.
Next, create a common IOS project, first select the simulator to run iphone5.0 simulator, and then drag libsdklib. A and myview. h to add it to the program.
Run the project to see the effect. In this example, a Google logo is loaded through the URL, and you can click this view to open the Baidu homepage.
The method for calling a static library is as follows:
# Import "viewcontroller. H "# import" myview. H "@ implementation viewcontroller-(void) didreceivememorywarning {[Super didreceivemorywarning]; // release any cached data, images, etc that aren't in use .} # pragma mark-view lifecycle-(void) viewdidload {[Super viewdidload]; // create a static library view myview * myview = [[myview alloc] initwithframe: cgrectmake (0, 0,120,100)]; // Add the static library View to the window [self. view addsubview: myview]; [myview release];}-(void) viewdidunload {[Super viewdidunload]; // release any retained subviews of the main view. // e.g. self. myoutlet = nil;} @ end
Okay. Now we have successfully applied the static library to the simulator. Next we will learn how to apply the static library to the real machine. Let's go back to the static Library Creation Project again, open the project, compile the environment, select iOS device, and then build the component.
Right-click libsdklib. A, and debug-iphoneos/libsdklib. A is the static library that can be edited on the real machine.
Finally, Drag and Drop libsdklib. A and myview. h to add them to the program. Connect to the real machine to run the project and you will see the effect on the real machine. It is worth noting that the simulator and the real machineLibsdklib. A must be strictly separated; otherwise, an error is reported.
Finally, you are welcome to discuss IOS software development with Momo. Continue to maintain your blog today, you know, wow ~~ If you cannot see it clearly, Momo will download the source code of this chapter. I hope you can learn it together ~. Wow ~ Momo is willing to study with you and make progress together ~!!!
: Http://www.xuanyusong.com/archives/606