开发IOS程序时我们使用的是Objective-c语言,Objective-c语言有.h .m 文件组成。静态库可以将
如何制作我们自己的静态库呢?
1.打开Xcode创建一个新的工程,选择Framework&Library, 再选择 Cocoa
2.我们添加一个弹出提示框的方法,简化弹出的代码,将主要的码代放到这个类实现。
//
// MyAlertView.h
// MyAlertView
//
// Created by yujy on 15/6/1.
// Copyright (c) 2015年 yujy. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface MyAlertView : UIAlertView
+ (void)showMesage:(NSString *)mesageString;
@end
//
// MyAlertView.m
// MyAlertView
//
// Created by yujy on 15/6/1.
// Copyright (c) 2015年 yujy. All rights reserved.
//
#import "MyAlertView.h"
@implementation MyAlertView
+ (void)showMesage:(NSString *)mesageString
{
UIAlertView *alertView=[[UIAlertView alloc] initWithTitle:nil
message:mesageString
delegate:nil
cancelButtonTitle:@"确定"
otherButtonTitles:nil];
[alertView show];
}
@end
3、生成静态库:
创健好方法后,现在我们开始制作静态库。静态库的制作方法可分为两种:第一种为在真机上使用的静态库,第二种为在模拟器中使用的静态库。这两种方法制作起来有点小区别,一定要注意。
我们开始从模拟器入手。
如图,先选择模拟器iPhone 6.0, 然后Build工程,构建工程完毕后libSDKLib.a静态库文件就生成了出来。
注意未编译前为红色(这里我已经编译成功了,所以已经是黑色啦):
这里一定要注意注意啦:看注意1,现在我们选择的模拟器是iPhone 6 ,再看 注意 2 ,如果 Build Active Architecture Only 的值 为YES, 则 它只编译当前的architecture版本。
关于这个属性,直接从网上转载过来(关于Build Active Architecture Only属性)
这个属性设置为yes,是为了debug的时候编译速度更快,它只编译当前的architecture版本。
而设置为no时,会编译所有的版本。
这个是设备对应的architecture:
armv6:iPhone 2G/3G,iPod 1G/2G
armv7:iPhone 3GS/4/4s,iPod 3G/4G,iPad 1G/2G/3G
armv7s:iPhone5, iPod5
编译出的版本是向下兼容的,比如你设置此值为yes,用iphone4编译出来的是armv7版本的,iphone5也可以运行,但是armv6的设备就不能运行。
目前IOS的指令集
armv6 iPhone、iPhone2、iPhone3G、第一代和第二代iPod Touch
armv7 iPhone4、iPhone4S
armv7s iPhone5、iPhone5C
arm64 iPhone5S
所以 这里我们需要兼容所有版本,需要将其设置为NO.
4.
打开我们的工程文件找到刚才生成的文件,这样在模拟器中使用的静态库就生成好了。
选择window->projects,弹出以下页面:
点击打开文件夹
5.静态库的调用
调用静态库的方法如下:创建一个新的工程,调用我们刚才生成好的静态库,将我们生成的静态库和生成静态库中用到的类的头文件导入到新的工程中。
调用方法如下:
#import "ViewController.h"
#import "MyAlertView.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[MyAlertView showMesage:@"ceshi"];
}
- (void)didReceiveMemoryWarning {
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
运行结果:
6. 生成真机模式下的静态库
我们再次回到制作静态库的工程当中,如图打开工程后编译环境选择自己的真机,然后步骤同上即可完成。
7.合并.a文件
模拟器静态库只能运行在模拟器上而真机静态库也只能运行在真机上,这样就比较麻烦,通过下面的步骤将两个静态库合成一个,这样在模拟器和真机上都能用了。
这一步在终端进行
命令如下:
lipo -create /Users/jim/Desktop/MyAlertView/Build/Products/Debug-iphoneos/libMyAlertView.a /Users/jim/Desktop/MyAlertView/Build/Products/Debug-iphonesimulator/libMyAlertView.a -output /Users/jim/Desktop/libMyAlertView.a
将/Users/jim/Desktop/MyAlertView/Build/Products/Debug-iphoneos/libMyAlertView.a /Users/jim/Desktop/MyAlertView/Build/Products/Debug-iphonesimulator/libMyAlertView.a 替换成你的路径。
将/Users/jim/Desktop/libMyAlertView.a替换成合并后的路径。
这样合并后的静态库就可以在模拟器和真机上运行了。
运行该命令中可能出现以下错误:
xcrun: error: active developer path ("/Users/yujy/Downloads/开发工具/Xcode6.2.app/Contents/Developer/") does not exist, use `xcode-select --switch path/to/Xcode.app` to specify the Xcode that you wish to use for command line developer tools (or see `man xcode-select`)
这是因为我将运行了一段时间的的Xcode.app拖放到Applications中去了。
执行以下命令即可:
sudo xcode-select -switch /Applications/Xcode.app/Contents/Developer
http://blog.sina.com.cn/s/blog_9693f61a0101dgwc.html
http://code4app.com/requirement/54b7363e933bf0353c8bc3fd
http://blog.youkuaiyun.com/freedom2028/article/details/8811318