PodXcode 高级玩法

iOS 制作本地pod库

2020-09-21  本文已影响0人  Accepted_

准备好静态库工程。实现好逻辑模块。例如下图中我们实现了一个计算的工具类。


静态库工程

在静态库工程文件夹里创建.podspec文件,文件名最好和静态库工程同名。

 $ pod spec create XYCalculateTool
创建好.podspec文件

选择一种打开方式,我用的Sublime Test。各个参数介绍如下

Pod::Spec.new do |s|

  # ―――  Spec Metadata  ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #pod库名称、版本号、简述
  s.name         = "XYCalculateTool"
  s.version      = "0.0.1"
  s.summary      = "A short description of XYCalculateTool"

  # 标签(帮助搜索),写在两个DESC中
 # s.description  = <<-DESC
 #                  DESC

  s.homepage     = "http://EXAMPLE/XYCalculateTool.podspec"
  # s.screenshots  = "www.example.com/screenshots_1.gif", "www.example.com/screenshots_2.gif"


  # ――― License  ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  # 
  #  Popular ones are 'MIT', 'BSD' and 'Apache License, Version 2.0'.
  #

  s.license      = "MIT"
  # s.license      = { :type => "MIT", :file => "FILE_LICENSE" }


  # ――― Author ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  作者相关信息

  s.author             = { "XY" => "123456789@qq.com" }
  # Or just: s.author    = "XY"
  # s.authors            = { "XY" => "123456789@qq.com" }
  # s.social_media_url   = "http://twitter.com/XYZ"

  # ――― Platform ――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  # 运行平台和最低支持系统

  # s.platform     = :ios
  # s.platform     = :ios, "5.0"

  #  多平台的配置如下
  # s.ios.deployment_target = "5.0"
  # s.osx.deployment_target = "10.7"
  # s.watchos.deployment_target = "2.0"
  # s.tvos.deployment_target = "9.0"


  # ――― Source Location ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  Specify the location from where the source should be retrieved.
  #  Supports git, hg, bzr, svn and HTTP.
  #

  s.source       = { :git => "http://XYCalculateTool.podspec.git", :tag => "#{s.version}" }


  # ――― Source Code ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  代码文件配置
  #

  s.source_files  = "Classes", "XYCalculateTool/**/*.{h,m}"
  # s.exclude_files = "Classes/Exclude"

  # s.public_header_files = "Classes/**/*.h"


  # ――― Resources ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  资源文件
  # 单个
  # s.resource  = "icon.png"
  # 后缀匹配
  # s.resources = "Resources/*.png"
  # bundle后缀匹配
  # s.resources = "Resources/*.{bundle}"

  # s.preserve_paths = "FilesToSave", "MoreFilesToSave"


  # ――― Project Linking ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #  需要依赖的.framework和.a(系统静态库或自己封装的都可以)

  # s.framework  = "SomeFramework"
  # s.frameworks = "SomeFramework", "AnotherFramework"

  # s.library   = "iconv"
  # s.libraries = "iconv", "xml2"


  # ――― Project Settings ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #  项目配置,比如Header Search Path等
  #  If your library depends on compiler flags you can set them in the xcconfig hash
  #  where they will only apply to your library. If you depend on other Podspecs
  #  you can include multiple dependencies to ensure it works.

  # s.requires_arc = true

  # s.xcconfig = { "HEADER_SEARCH_PATHS" => "$(SDKROOT)/usr/include/libxml2" }
  # s.dependency "JSONKit", "~> 1.4"

end
我的代码文件配置如下: 配置代码文件所在路径

然后把静态库工程文件夹复制进项目的一个地方。(我直接复制在了项目文件夹下)


把静态库工程复制进项目

创建Podfile文件

$ cd <project-root-dir>
$ pod init

打开Podfile文件

# Uncomment the next line to define a global platform for your project
# platform :ios, '9.0'

target 'XYPodTestDemo' do
  #在target ... do后面加这句,path要写Podfile文件和静态库工程中的podspec文件相对路径
  pod "XYCalculateTool", :path => "XYPodTestDemo/XYCalculateTool"

end

然后安装pod

$ pod install
Pod installation complete!表示安装成功

如果出现错误可能是podspec路径不对或者podspec配置有问题,可以根据错误提示修改podspec。

打开XYPodTestDemo.xcworkspace,先导入头文件

#import <XYCalculateTool.h>

然后就可以使用Pod库中的方法了。

已经能够联想pod中的方法了

参考资料:
iOS,制作属于自己cocoapods,(framework,bundle)

上一篇下一篇

猜你喜欢

热点阅读