需要马上研究的iOS接下来要研究的知识点汽车公司可能用到的知识点

iOS开发SDK总结 - 一个方便开发的SDK工程结构

2020-05-26  本文已影响0人  Hamiltion

开发SDK常常遇到的一些问题

开发环境

下面是我总结的开发步骤

1、创建一个Framework工程

创建一个Framework工程,做SDK相关配置,这里不再赘述。我这里创建的名称是ZHTestSDK

2、在该工程里面创建一个demo工程
#import <ZHTestSDK/ZHTestSDK.h>

ZHImageView *imageView = [[ZHImageView alloc] initWithFrame:CGRectMake(100, 200, 200, 200)];
[self.view addSubview:imageView];

3、使用资源,建立bundle
4、使用bundle中的资源
#import "ZHHelper.h"

@interface ZHHelper()

/// 资源bundle
@property (nonatomic, strong) NSBundle *resourceBundle;

@end

@implementation ZHHelper

/// 单例
+ (instancetype)shared {
    static ZHHelper *_helper = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        _helper = [[ZHHelper alloc] init];
    });
    return _helper;
}

/// 通过资源名获取资源路径
/// @param name 资源名称
/// @param type 资源类型
- (NSString *)resourcePathWithName:(NSString *)name ofType:(NSString *)type {
    return [self.resourceBundle pathForResource:name ofType:type];
}

#pragma mark - setter / getter

- (NSBundle *)resourceBundle {
    if (!_resourceBundle) {
        NSString *bundlePath = [[NSBundle mainBundle] pathForResource:@"ZHTestSDKResource" ofType:@"bundle"];
        _resourceBundle = [NSBundle bundleWithPath:bundlePath];
    }
    return _resourceBundle;
}

@end

5、引用第三方库

我这里采用pod的形式引入第三方库,以引入SDWebImage为例

platform :ios, '9.0'

target 'ZHTestSDK' do
  use_frameworks!
  pod 'SDWebImage'

end

target 'ZHTestSDKDemo' do
  use_frameworks!
  pod 'SDWebImage'
  
end
6、打包

打包时,要注意bundle也是SDK一部分,bundle不要放到Framework内部,他两文件结构平级就可以,否则通过上面的方式会读取不到

7、源码地址

https://github.com/zhengzhanghai/ZHTestSDK

上一篇 下一篇

猜你喜欢

热点阅读