iOS使用.a类型的静态库
1.新建一个新工程
2.引入SDKDemoBundle.bundle资源文件
注意:不过SDKDemoBundle.bundle资源文件并不被编译进.a文件,只是以后找SDKDemoBundle.bundle资源文件方便。可以通过比较添加和删除SDKDemoBundle.bundle资源文件生成的libSDKStaticDemo.a大小知道他们一样大。
当然也可以参考如何访问组件的bundle资源来使用.bundle资源。
3.引入 include 文件
在之前的静态库文件找到任意的生成文件 引入 include 文件。
由于它要把生成的库中的include 文件加载到项目中,这样拷贝文件夹也确实没有Framework库方便。
4.引入libSDKStaticDemo-Debug.和libSDKStaticDemo-Release.a
最后引入结果如下
5.检查Linked Frameworks and Libraries下是否有你的静态库,若没有会crash。不过默认是会在它下面看到的。当然你用pods导入的在工程的Linked Frameworks and Libraries看不到,不过不影响你的正确使用。这个和动态库需要你手动配置到Embedded Binaries下简单了,真正达到傻瓜式使用。当然更简单的是做一个公有组件
6.增加资源文件SDKDemoBundle.bundle,不增加资源文件,而使用资源文件会crash.
7.增加使用动态库的代码。
ViewController.m
#import "ViewController.h" #import "SDKDemoViewController.h" #import "TestTool.h" @interface ViewController () @property (nonatomic, strong) UIButton *testBtn; @end @implementation ViewController - (void)viewDidLoad { [super viewDidLoad]; // Do any additional setup after loading the view, typically from a nib. UIButton *testBtn = [[UIButton alloc]initWithFrame:CGRectMake(0, 100, 50, 50)]; [testBtn setTitleColor:[UIColor redColor] forState:UIControlStateNormal]; [testBtn setTitle:@"测试" forState:UIControlStateNormal]; [testBtn addTarget:self action:@selector(testAction:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:testBtn]; testBtn.center = self.view.center; self.testBtn = testBtn; } - (void)testAction:(UIButton *)button { [TestTool testLog]; SDKDemoViewController *vc = [[SDKDemoViewController alloc] init]; [self presentViewController:vc animated:YES completion:nil]; } - (void)viewWillAppear:(BOOL)animated { [super viewWillAppear:YES]; [self.view bringSubviewToFront:self.testBtn]; } @end
8.修改环境
修改采用debug模式运行libSDKStaticDemo-Debug.a后面出现R标志,libSDKStaticDemo-Release.a显示A。
反之亦然。
8.调用以及结果
9.执行pod update --verbose --no-repo-update
下载依赖库报错
[!] The Podfile contains framework or static library targets (ELockSDK), for which the Podfile does not contain host targets (targets which embed the framework). If this project is for doing framework development, you can ignore this message. Otherwise, add a target to the Podfile that embeds these frameworks to make this message go away (e.g. a test target). [!] 'Bugly' uses the unencrypted http protocol to transfer the Pod. Please be sure you're in a safe network with only trusted hosts in there. Please reach out to the library author to notify them of this security issue. [!] 'UMengUShare' uses the unencrypted http protocol to transfer the Pod. Please be sure you're in a safe network with only trusted hosts in there. Please reach out to the library author to notify them of this security issue. [!] The `ELockSDK [Debug]` target overrides the `OTHER_LDFLAGS` build setting defined in `Pods/Target Support Files/Pods-ELockSDK/Pods-ELockSDK.debug.xcconfig'. This can lead to problems with the CocoaPods installation - Use the `$(inherited)` flag, or - Remove the build settings from the target.
解决方案:
按照下图设置工程的配置中,搜索OTHER_LDFLAGS,在第一行增加$(inherited)。