把代码打包成静态framework给客户使用

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/denggun12345/article/details/89474756

一般第三方框架(例如个推,友盟等),为了使用方便提供了两种使用方式:

方式1、直接将其sdk,一般是framework形式,拖拽到客户工程中;

方式2、将sdk托管到远程仓库通过cocoapods管理;

这两种方式看似只差一个加载方式的问题,但是如果此框架有图片或xib时,则方式一和方式二加载图片和xib的路径也不相同。下边就来分别聊一聊制作过程和两种方式的区别:

一、制作framework,这里是制作的静态库:为了方便调试我们这里以workspace的方式来制作和调试framework

1、创建workspace:xcode菜单栏   file->new workspace ->New Folder ->testOne ->Oneworkspace.xcworkspace:创建workspace

2、在testOne文件夹下创建framework名为oneframework:注意Add to 和Group都选Oneworkspace。

3、在testOne文件夹下创建project名为oneproject:注意Add to 和Group都选Oneworkspace,同上图。

(如果workaspace联编不太明白,详细请看:https://blog.csdn.net/denggun12345/article/details/86152128

这样该创建的已经创建完了,截图如下:

4、制作oneframework:

将图片和xib分别制作成bundle文件,并拖到oneframework内:

 4.1、图片制作成bundle:将要打成bundle的图片放在同一文件夹下,并直接将文件后缀变为.bundle,此例图片bundle为cc.bundle

 4.2、注意xib打成bundle放到framework时,需要将xib转换成nib格式:

以YHTestSevenCell.xib为例:用如下命令转换为nib:

ibtool --errors --warnings --output-format human-readable-text --compile YHTestSevenCell.nib YHTestSevenCell.xib。

同理所有xib转换为nib之后将所有nib放在同一文件夹下生成cell.bundle文件。

5、测试oneframework:将oneframework添加到oneproject中测试:

在oneproject的Build Phases中点“+”->New Copy Files Phases 中添加此framework

target选中oneproject运行,即可查看结果。

这时产生的oneframework可以当做sdk,拖拽到目标工程中使用。

如果要将oneframework制作成cocoapods管理,则需要按做如下更多操作:

6、coacoapods管理三方库的正常流程如下:https://blog.csdn.net/denggun12345/article/details/84984606

因为这里要托管的是framework,

6.1、podspec有些点不太一样:

  s.vendored_frameworks = 'oneframework.framework'

  s.resources    = ["oneframework.framework/cc.bundle","oneframework.framework/cell.bundle"] //多个bundle

6.2、文件夹的结构也不同:

通过这种文件结构和spec文件配置,pod到本地后的文件结构如下(bundle都被暴露出来):

7、修改png和xib加载路径:

在6之前的图片加载路径如下,拖拽版:

{    //将静态库l拖到工程下这么取图片

    NSString *bunldePath = [[NSBundle mainBundle] pathForResource:@"oneframework.framework/cc" ofType:@"bundle"];

    NSBundle *imageBundle = [NSBundle bundleWithPath:bunldePath];

    NSString *path = [imageBundle pathForResource:@"Enterprise_bots_woyaotikuan" ofType:@"png"];

    [headBtn setImage:[UIImage imageWithContentsOfFile:path] forState:UIControlStateNormal];

 }

现在cocoapods改为,cocoapods版:

{    //用cocoapods管理framework,s.resource = 的方式添加的bundle,用上边的方式取不到图片,尝试用下边的方法,可行

        NSString *bunldePath = [[NSBundle mainBundle] pathForResource:@"cc" ofType:@"bundle"];

        NSBundle *imageBundle = [NSBundle bundleWithPath:bunldePath];

        [headBtn setImage:[UIImage imageNamed:@"Enterprise_bots_woyaotikuan" inBundle:imageBundle compatibleWithTraitCollection:nil] forState:UIControlStateNormal];

    }

在6之前的xib加载路径如下,拖拽版:

{    //将静态库l拖到工程下这么取nib

        NSString *bunldePath = [[NSBundle mainBundle] pathForResource:@"oneframework.framework/cell" ofType:@"bundle"];

        NSBundle *bundel = [NSBundle bundleWithPath:bunldePath];

        NSLog(@"bundlePath:%@",bundel.bundlePath);

        [self.tableView registerNib:[UINib nibWithNibName:@"YHTestEightCell" bundle:bundel] forCellReuseIdentifier:@"eightCell"];

        [self.tableView registerNib:[UINib nibWithNibName:@"YHTestSevenCell" bundle:bundel] forCellReuseIdentifier:@"sevenCell"];

 }

现在cocoapods改为,cocoapods版:

{   //用cocoapods管理framework,s.resource = 的方式添加的bundle,用上边的方式取不到图片,尝试用下边的方法,可行

        NSString *bunldePath = [[NSBundle mainBundle] pathForResource:@"cell" ofType:@"bundle"];

        NSBundle *bundel = [NSBundle bundleWithPath:bunldePath];

        NSLog(@"bundlePath:%@",bundel.bundlePath);

        [self.tableView registerNib:[UINib nibWithNibName:@"YHTestEightCell" bundle:bundel] forCellReuseIdentifier:@"eightCell"];

        [self.tableView registerNib:[UINib nibWithNibName:@"YHTestSevenCell" bundle:bundel] forCellReuseIdentifier:@"sevenCell"];

    }

8、验证远程私有framework:

创建新工程,以cocoapods方式,测试远程framework。注意:直接将远程库pod到本地就可以直接使用,不用再embeded 也不用在build phases中做任何添加

索引库:https://gitee.com/denggun/YHTestOneSpecGroup/tree/master/testOneSpec

本地路径: /Users/yangyangzi/Desktop/Task/cocoapods本地测试工程/cocoapodsAndFramework 

猜你喜欢

转载自blog.csdn.net/denggun12345/article/details/89474756