Xcode5打包静态库

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

打包的步骤如下:

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


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

WebViewController.h文件

[objc]  view plain copy print ?
  1. #import <UIKit/UIKit.h>  
  2.   
  3. @interface WebViewController : UIViewController<UIWebViewDelegate>  
  4.   
  5. @end  

WebViewController.m文件 

[objc]  view plain copy print ?
  1. #import "WebViewController.h"  
  2.   
  3. #define CURRENT_DEVICE_SYSTEMVERSION [[UIDevice currentDevice] systemVersion]  
  4. #define iOS7_VERSIONS_LATTER ([CURRENT_DEVICE_SYSTEMVERSION floatValue] >= 7.0)  
  5.   
  6. @interface WebViewController ()  
  7.       
  8. @property (nonatomicretainUIWebView *webView;  
  9.       
  10. @end  
  11.   
  12. @implementation WebViewController  
  13.   
  14. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil  
  15. {  
  16.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  
  17.     if (self) {  
  18.         // Custom initialization  
  19.     }  
  20.     return self;  
  21. }  
  22.   
  23. - (void)viewDidLoad  
  24. {  
  25.     [super viewDidLoad];  
  26.       
  27.     if (iOS7_VERSIONS_LATTER) {  
  28.         [self setNeedsStatusBarAppearanceUpdate];  
  29.         self.edgesForExtendedLayout = UIRectEdgeBottom;  
  30.         self.extendedLayoutIncludesOpaqueBars = NO;  
  31.         self.automaticallyAdjustsScrollViewInsets = NO;  
  32.     }  
  33.       
  34.     self.webView = [[[UIWebView alloc] initWithFrame:self.view.frame] autorelease];  
  35.     self.webView.delegate = self;  
  36.     [self.view addSubview:self.webView];  
  37.     [self loadWebPageWithString:@"http://www.baidu.com"];  
  38.   
  39. }  
  40.   
  41. - (void)didReceiveMemoryWarning  
  42. {  
  43.     [super didReceiveMemoryWarning];  
  44.     // Dispose of any resources that can be recreated.  
  45. }  
  46.       
  47. - (void)dealloc  
  48. {  
  49.     self.webView = nil;  
  50.     [super dealloc];  
  51. }  
  52.       
  53. - (void)loadWebPageWithString:(NSString*)urlString  
  54. {  
  55.     NSURL *url =[NSURL URLWithString:urlString];  
  56.     NSLog(@"%@",urlString);  
  57.     NSURLRequest *request =[NSURLRequest requestWithURL:url];  
  58.     [self.webView loadRequest:request];  
  59. }  

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

Xcode5打包静态库_第1张图片

Xcode5打包静态库_第2张图片


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

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

Xcode5打包静态库_第3张图片

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

Xcode5打包静态库_第4张图片


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


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

5、静态库的使用

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


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

[objc]  view plain copy print ?
  1. - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions  
  2. {  
  3.     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];  
  4.     // Override point for customization after application launch.  
  5.     WebViewController *webVC = [[WebViewController alloc] init];  
  6.     self.window.rootViewController = webVC;  
  7.     [webVC release];  
  8.     self.window.backgroundColor = [UIColor whiteColor];  
  9.     [self.window makeKeyAndVisible];  
  10.     return YES;  
  11. }  
在ios7模拟器上运行的效果图如下:

Xcode5打包静态库_第5张图片

你可能感兴趣的:(Xcode5打包静态库)