Xcode5打包静态库

今天研究了一下怎么打包静态库,从网上查了很多资料,但目前大多数都是说在Xcode4上怎么打包静态库的,所以今天我用Xcode5打包了静态库,其实都差不多呢。

打包的步骤如下:

1、创建一个静态库项目,如下图:

Xcode5打包静态库

2、在这个项目中我创建了一个UIViewController类,上面就一个UIWebView,加载百度的地址,代码如下:

WebViewController.h文件

  1. #import<UIKit/UIKit.h>
  2. @interfaceWebViewController:UIViewController<UIWebViewDelegate>
  3. @end

WebViewController.m文件

  1. #import"WebViewController.h"
  2. #defineCURRENT_DEVICE_SYSTEMVERSION[[UIDevicecurrentDevice]systemVersion]
  3. #defineiOS7_VERSIONS_LATTER([CURRENT_DEVICE_SYSTEMVERSIONfloatValue]>=7.0)
  4. @interfaceWebViewController()
  5. @property(nonatomic,retain)UIWebView*webView;
  6. @end
  7. @implementationWebViewController
  8. -(id)initWithNibName:(NSString*)nibNameOrNilbundle:(NSBundle*)nibBundleOrNil
  9. {
  10. self=[superinitWithNibName:nibNameOrNilbundle:nibBundleOrNil];
  11. if(self){
  12. //Custominitialization
  13. }
  14. returnself;
  15. }
  16. -(void)viewDidLoad
  17. {
  18. [superviewDidLoad];
  19. if(iOS7_VERSIONS_LATTER){
  20. [selfsetNeedsStatusBarAppearanceUpdate];
  21. self.edgesForExtendedLayout=UIRectEdgeBottom;
  22. self.extendedLayoutIncludesOpaqueBars=NO;
  23. self.automaticallyAdjustsScrollViewInsets=NO;
  24. }
  25. self.webView=[[[UIWebViewalloc]initWithFrame:self.view.frame]autorelease];
  26. self.webView.delegate=self;
  27. [self.viewaddSubview:self.webView];
  28. [selfloadWebPageWithString:@"http://www.baidu.com"];
  29. }
  30. -(void)didReceiveMemoryWarning
  31. {
  32. [superdidReceiveMemoryWarning];
  33. //Disposeofanyresourcesthatcanberecreated.
  34. }
  35. -(void)dealloc
  36. {
  37. self.webView=nil;
  38. [superdealloc];
  39. }
  40. -(void)loadWebPageWithString:(NSString*)urlString
  41. {
  42. NSURL*url=[NSURLURLWithString:urlString];
  43. NSLog(@"%@",urlString);
  44. NSURLRequest*request=[NSURLRequestrequestWithURL:url];
  45. [self.webViewloadRequest:request];
  46. }

3、如果你的这个静态库是给其他developer用,你就给他们Release版本的,在Edit Scheme中设置,如下图:

Xcode5打包静态库

Xcode5打包静态库


4、静态库的制作方法可分为两种:第一种为在真机上使用的静态库,第二种为在模拟器中使用的静态库。他们的区别如下:

当制作在真机上使用的静态库时,选择iOS Device,编译,你会得到一个支持真机的静态库。

Xcode5打包静态库

右击Products目录下的libWebViewSDK.a文件open in finder,找到这个文件所在的目录,选择Release-iphoneos目录下的libWebViewSDK.a文件。

Xcode5打包静态库


当制作在模拟器上运行的静态库时,选择iPhone Simulator,编译,你会得到一个支持模拟器的静态库。

Xcode5打包静态库

右击Products目录下的libWebViewSDK.a文件open in finder,找到这个文件所在的目录,选择Release-iphoneosimulator目录下的libWebViewSDK.a文件。

5、静态库的使用

把此静态库和此库中必要的头文件加到你要使用此库的项目中,如我建立了一个WebImageTest的项目,然后我把相应的静态库libWebViewSDK.a(真机版或模拟器版的)和前面写的WebViewController.h文件加入到此项目中

Xcode5打包静态库

调用此静态库的代码如下:

  1. -(BOOL)application:(UIApplication*)applicationdidFinishLaunchingWithOptions:(NSDictionary*)launchOptions
  2. {
  3. self.window=[[UIWindowalloc]initWithFrame:[[UIScreenmainScreen]bounds]];
  4. //Overridepointforcustomizationafterapplicationlaunch.
  5. WebViewController*webVC=[[WebViewControlleralloc]init];
  6. self.window.rootViewController=webVC;
  7. [webVCrelease];
  8. self.window.backgroundColor=[UIColorwhiteColor];
  9. [self.windowmakeKeyAndVisible];
  10. returnYES;
  11. }
在ios7模拟器上运行的效果图如下:

Xcode5打包静态库