Skip to content

Instantly share code, notes, and snippets.

@liuzhida33
Last active March 26, 2021 09:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save liuzhida33/b61974dd38a99118b88995a54ed6c790 to your computer and use it in GitHub Desktop.
Save liuzhida33/b61974dd38a99118b88995a54ed6c790 to your computer and use it in GitHub Desktop.
iOS 制作基于pod开发第三方framework的二次封装

iOS 制作基于pod开发第三方framework的二次封装

主要记录如何将第三方SDK的.a或framwwork集成到自己的framework中,并构建自己的pod发布到私有库所遇到的问题。

集成

  1. 打开第三方SDK找到二进制文件(以iflyMSC.framework为例)
  2. 将iflyMSC的二进制文件改成.a的后缀名,重命名为libiflyMSC.a。 如果不加lib前缀,那么在后面进行pod lib lint命令时有可能会遇到这样的错误提示: library not found for XXX 在加载静态库的时候,Xcode会查找带有库名称和前缀lib的文件。例如,库名为iflyMSC.a,Xcode将会查找libiflyMSC.a,这时候在加载库的时候就会失败。所以为了能够正确加载到我们的静态库,必须要将静态库命名为libXXX.a。
  3. 打开Xcode新建一个framework工程。
  4. 将刚刚改好的libiflyMSC.a和header下的.h文件一并拖进项目中。可以放进lib文件夹下。
  5. 在framework项目头文件中引入第三方库的头文件
  6. 如果是为了提供Swift项目使用,最好在新建一个.swift文件,否则可能出现Swift项目无法引入该framework,具体原因不明。

制作pod

上述工作做好之后,我们就可以开始修改.podspec文件,进行制作pod了

通过pod lib create生成一份.podspec文件。打开.podspec文件,修改里面的内容。主要修改s.source_files、s.vendored_libraries、s.public_header_files、s.frameworks和s.libraries。其中s.frameworks和s.libraries是讯飞SDK需要依赖的库。我的.podspec文件示例如下:

Pod::Spec.new do |s|

  s.name         = "MSCSDK"
  s.version      = "1.0.0"
  s.summary      = "MSCSDK."

  s.description  = <<-DESC
                    this is MSCSDK
                   DESC

  s.homepage     = "https://code.aliyun.com/zhonglejia/MSCSDK"
 
  s.license      = { :type => "MIT", :file => "LICENSE" }

  s.author             = { "liuzhida33" => "liuzhida33@gmail.com" }
  s.platform     = :ios, "9.0"

  s.swift_version = '4.2.0'

  s.source       = { :git => "https://xxxxxx/MSCSDK.git", :tag => s.version.to_s }

  s.requires_arc = true

  s.source_files  = 'MSCSDK/*.{swift}', 'MSCSDK/lib/*.h', 'MSCSDK/*.h'
  s.public_header_files = 'MSCSDK/lib/*.h'
  s.vendored_libraries  = 'MSCSDK/lib/libiflyMSC.a'

  s.frameworks = 'SystemConfiguration', 'QuartzCore', 'CoreTelephony', 'CoreGraphics', 'UIKit', 'AVFoundation', 'AudioToolbox', 'CoreLocation', 'Foundation'
  s.libraries = 'z', 'c++', 'sqlite3.0'
  s.xcconfig = { 'OTHER_LDFLAGS' => '-ObjC -all_load' }

end

最后,使用pod lib lint命令的时候不要忘记了加上--use-libraries来让验证通过。

@jk-swift
Copy link

大佬 我用Xcode11.4操作,拆包封装完毕后 单元测试找不到命名空间下方法(第一个framework内的c++方法),大佬遇到过类似情况吗

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment