简介:通过一个简单登录SDK,介绍Swift和OC混编下如何打包和调用SDK
github地址:https://github.com/wuxingxing8912/LNLoginSDK
一、创建framework静态库
第一步、新建工程,选择Cocoa Touch Framework。
![](https://img-blog.csdnimg.cn/img_convert/db27f74ec5c81f111d7e380546560619.webp?x-oss-process=image/format,png)
第二步、导入需要打包的源文件。
![](https://img-blog.csdnimg.cn/img_convert/127ff27ee0e1f6411217146458ea89e2.webp?x-oss-process=image/format,png)
第三步、配置工程
(1)、修改暴露头文件
![](https://img-blog.csdnimg.cn/img_convert/098a75f59d00710a39ee17224b439b9d.webp?x-oss-process=image/format,png)
与纯OC库不同的是,混编库还需要在“项目名.h”(默认生成)下导入拖入的头文件(头文件内容与项目中桥接文件内容相同),原因在下面的填坑记录中会讲到。
![](https://img-blog.csdnimg.cn/img_convert/88103f7de795eb58c0121c29080a90f3.webp?x-oss-process=image/format,png)
(2)、设置编译模式: 在 Product -> Scheme -> Edit Scheme 下,将Build Configuration改为Release。
![](https://img-blog.csdnimg.cn/img_convert/8ec8fb0eeff1a9fdcfa7e397e621c0b1.webp?x-oss-process=image/format,png)
![](https://img-blog.csdnimg.cn/img_convert/f9dccd7de6853e380cc81d231a82158e.webp?x-oss-process=image/format,png)
(3)、设置编译出来的静态库包含的指令集,选中No,如果选Yes,则只包含当前选中编译设备的指令集
![](https://img-blog.csdnimg.cn/img_convert/81ba42793e3c2a1136a860eaffca5b15.webp?x-oss-process=image/format,png)
(4)、编译得到.framework文件,注意需要真机和模拟器各编译一次。
![](https://img-blog.csdnimg.cn/img_convert/9c935fc99040ab88971119855e5670d8.webp?x-oss-process=image/format,png)
二、打包资源文件
第一步、创建Bundle(注意Bundle在macOS选项下)。
![](https://img-blog.csdnimg.cn/img_convert/89fd3fd4a4114655f5e8e8d82faac943.webp?x-oss-process=image/format,png)
第二步、设置Base SDK为iOS SDK。
![](https://img-blog.csdnimg.cn/img_convert/4cf64a31f9b55984ff3bc2c988217088.webp?x-oss-process=image/format,png)
第三步、编译得到.bundle文件,注意需要真机和模拟器各编译一次。
![](https://img-blog.csdnimg.cn/img_convert/3bda436467bc907763f87fba1c1ccf17.webp?x-oss-process=image/format,png)
第四步、修改.framework中使用Bundle中资源的地方
![](https://img-blog.csdnimg.cn/img_convert/270c757375da615991a23358d4de993d.webp?x-oss-process=image/format,png)
三、调用
Swift工程调用:
第一步,将.framework和.bundle文件拷贝到工程根目录下(调用Demo里使用的是模拟器生成的库文件)
![](https://img-blog.csdnimg.cn/img_convert/f359f7026341a15be4679405d85e668b.webp?x-oss-process=image/format,png)
第二步,分别将.framework和.bundle文件拖到Xcode项目跟目录下,在弹窗中选中Copy items if needed。
![](https://img-blog.csdnimg.cn/img_convert/0d4677e961776dca7f3fb5253e8c4a64.webp?x-oss-process=image/format,png)
第三步、配置工程:在General->Embedded Binaries下加入.framework。
![](https://img-blog.csdnimg.cn/img_convert/5fd40b6e682487c100aed46cfaa043e5.webp?x-oss-process=image/format,png)
第四步、在Build Phases -> Copy Files(没有则点击左上角+号添加) 下,将Destination选择为Frameworks,点击+号将.framework加进来。
![](https://img-blog.csdnimg.cn/img_convert/dc7e11ab8c1ebf0c2204902e6377ac56.webp?x-oss-process=image/format,png)
第五步、在代码中使用:
步骤为:导入头文件 -> 添加并实现代理方法 -> 创建对象 -> 调用方法
注:ViewController必须带Navigation Controller(见Main.Storyboard),否则不会跳转。
![](https://img-blog.csdnimg.cn/img_convert/d7ee282b5b457cee5e74f0cfb50a0fe0.webp?x-oss-process=image/format,png)
OC工程调用:
OC调用方法的前三步与Swift一致
第四步: 在Build Settings -> Build Options 下,将Always Embed Swift Standard Libraries设置为Yes。
![](https://img-blog.csdnimg.cn/img_convert/cd38ee2623b2cb94256738b26252f74d.webp?x-oss-process=image/format,png)
第五步、在代码中使用:
步骤为:导入头文件 -> 添加并实现代理方法 -> 创建单例 -> 调用方法
注:ViewController必须带Navigation Controller(见Main.Storyboard),否则不会跳转。
![](https://img-blog.csdnimg.cn/img_convert/e8f40510282702b0df48cf1ee1f0c9a1.webp?x-oss-process=image/format,png)
![](https://img-blog.csdnimg.cn/img_convert/8cc5108a31a8c9e5418b71c7e1c556f0.webp?x-oss-process=image/format,png)
报错记录:
1、如果没有添加Copy Files会报错:
![](https://img-blog.csdnimg.cn/img_convert/79d79c5104ba38dff9a874d39a17d1c3.webp?x-oss-process=image/format,png)
2、Bundle初始化失败,会报错,原因可能是Bundle使用的资源文件不存在或Bundle名称错误:
![](https://img-blog.csdnimg.cn/img_convert/f800665653c661fd464c3ce105691838.webp?x-oss-process=image/format,png)
4、填坑记录:
(1)、Swift不支持.a静态库
![](https://img-blog.csdnimg.cn/img_convert/945d7c58119191b541122939007885c4.webp?x-oss-process=image/format,png)
(2)、在framework下不允许添加桥接文件,解决办法是在配置好后还需要在“项目名.h”(默认生成)下导入该头文件。
![](https://img-blog.csdnimg.cn/img_convert/f4299cfb2aaa5b86366fe8cac31ff024.webp?x-oss-process=image/format,png)
(3)、Bundle与Framework名称不一样时会报这个错误,改成一样就行了,至于为什么,我还没找到原因,求大神指教。(我这里都是LNLoginKit)
![](https://img-blog.csdnimg.cn/img_convert/7ce5e6a8612c3aa679998495a108d541.webp?x-oss-process=image/format,png)
待处理:
1、合并framework,参考:http://blog.sina.com.cn/s/blog_71e456db0101d8pb.html
合并后的库不可用,不知道为什么,求大神指教。
![](https://img-blog.csdnimg.cn/img_convert/36453a7d38b6eee3e68866665ac04460.webp?x-oss-process=image/format,png)
2、虽说是Swift和OC混编SDK,但目前只支持项目中使用了Swift调用OC的工程,暂不支持OC调用Swift的工程打包Framework,因为项目桥接头文件“produceName-Bridging-Header.h”文件是隐藏的,无法暴露出来,因此也就不可能引用,如果哪位大神有办法解决,请回复分享,万分感谢。
参考:
http://www.cnblogs.com/huntaiji/p/3490911.html
http://blog.csdn.net/tianshats/article/details/52045974