iOS 制作Framework后上传CocoaPods

此篇文章将介绍如何制作一个依赖三方库的Framework;

制作工具:
Mac
Xcode
VSCode
Carthage (Carthage 是由swift语言编写的包管理工具)

准备工作

安装homebrew

/bin/zsh -c "$(curl -fsSL https://gitee.com/cunkai/HomebrewCN/raw/master/Homebrew.sh)"

安装Carthage:

brew update
brew install carthage

在Xcode中创建SDK

创建Framework工程

创建podfile

创建podfile文件,写入类似pod 'RxSwift'等三方库,在终端执行pod install

podfile

编辑SDK核心代码

写入核心代码 Emm.....

构建Framework

cd 到工程目录下,执行命令开始打包

carthage build --no-skip-current

完成后在工程目录下会新增一个文件夹Carthage

文件夹

到此,一个framework已经打包📦 好了。此时你想直接导入到其他工程中使用,那么可能会报错,如果直接使用这个framework,需要导入依赖库,当然这么做会有些不友好,所以我们可以将framework上传至Cocoapods仓库,并指定依赖库,这样在其他项目中使用我们封装好的库只需要轻轻 pod 一下即可。

下面我们将介绍下如何将framework上传至Cocoapods仓库

将framework上传至Cocoapods仓库

我们先介绍下将一个framework上传至Cocoapod仓库需要几个步骤:

  • 创建Git仓库
  • 创建podspec文件
  • 验证podspec文件
  • 上传项目并打上Tag
  • 发布至Cocoapods
创建Git仓库
  • 创建Git仓库,可以选择github,gitlab,码云等等;
  • 创建时选择License类型为MIT License的文件;
  • Clone至本地,将framework添加到本地仓库中;
创建podspec文件
  • 新建一个 podspec文件, 文件名要取好,文件名对应的就是库的名称,如果重复了是无法上传库的;(名称建议和库的名字保持一致)
// fileName 文件名 (你需要创建的名字)
pod spec create fileName
  • 编辑podspec文件,打开文件之后填入对应的信息即可;
    整个文件配置如下:
Pod::Spec.new do |spec|

  # ―――  Spec Metadata  ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  These will help people to find your library, and whilst it
  #  can feel like a chore to fill in it's definitely to your advantage. The
  #  summary should be tweet-length, and the description more in depth.
  #

  spec.name         = "CHC_SDKTest"
  spec.version      = "0.0.1"
  spec.summary      = "A short description of CHC_SDKTest."

  # This description is used to generate tags and improve search results.
  #   * Think: What does it do? Why did you write it? What is the focus?
  #   * Try to keep it short, snappy and to the point.
  #   * Write the description between the DESC delimiters below.
  #   * Finally, don't worry about the indent, CocoaPods strips it!
  spec.description  = "测试sdk,你值得拥有"

  spec.homepage     = "https://gitee.com/caohanchao_076/sdktest"
  # spec.screenshots  = "www.example.com/screenshots_1.gif", "www.example.com/screenshots_2.gif"


  # ―――  Spec License  ――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  Licensing your code is important. See https://choosealicense.com for more info.
  #  CocoaPods will detect a license file if there is a named LICENSE*
  #  Popular ones are 'MIT', 'BSD' and 'Apache License, Version 2.0'.
  #

  # spec.license      = "MIT (example)"
  spec.license      = { :type => "MIT", :file => "LICENSE" }

  # ――― Author Metadata  ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  Specify the authors of the library, with email addresses. Email addresses
  #  of the authors are extracted from the SCM log. E.g. $ git log. CocoaPods also
  #  accepts just a name if you'd rather not provide an email address.
  #
  #  Specify a social_media_url where others can refer to, for example a twitter
  #  profile URL.
  #

  spec.author             = { "caohanchao" => "627203960@qq.com" }
  # Or just: spec.author    = "caohanchao"
  # spec.authors            = { "caohanchao" => "627203960@qq.com" }
  # spec.social_media_url   = "https://twitter.com/caohanchao"

  # ――― Platform Specifics ――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  If this Pod runs only on iOS or OS X, then specify the platform and
  #  the deployment target. You can optionally include the target after the platform.
  #

  # spec.platform     = :ios
  spec.platform     = :ios, "9.0"

  #  When using multiple platforms
  # spec.ios.deployment_target = "5.0"
  # spec.osx.deployment_target = "10.7"
  # spec.watchos.deployment_target = "2.0"
  # spec.tvos.deployment_target = "9.0"


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

  spec.source       = { :git => "https://gitee.com/caohanchao_076/sdktest.git", :tag => "#{spec.version}" }


  # ――― Source Code ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  CocoaPods is smart about how it includes source code. For source files
  #  giving a folder will include any swift, h, m, mm, c & cpp files.
  #  For header files it will include any header in the folder.
  #  Not including the public_header_files will make all headers public.
  #

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

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


  # ――― Resources ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  A list of resources included with the Pod. These are copied into the
  #  target bundle with a build phase script. Anything else will be cleaned.
  #  You can preserve files from being cleaned, please don't preserve
  #  non-essential files like tests, examples and documentation.
  #

  # spec.resource  = "icon.png"
  # spec.resources = "Resources/*.png"

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


  # ――― Project Linking ―――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  Link your library with frameworks, or libraries. Libraries do not include
  #  the lib prefix of their name.
  #

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

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


  # ――― Project Settings ――――――――――――――――――――――――――――――――――――――――――――――――――――――――― #
  #
  #  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.

  # spec.requires_arc = true

  # spec.xcconfig = { "HEADER_SEARCH_PATHS" => "$(SDKROOT)/usr/include/libxml2" }
  # spec.dependency "JSONKit", "~> 1.4"
  spec.dependency "Alamofire"
end
验证podspec文件
  • 终端cd到 podspec所在目录下,执行以下命令
// 验证方法一,  网络校验
pod spec lint 项目名.podspec

// 验证方法二,本地校验
pod lib lint --allow-warnings --verbose
上传项目并打上Tag
  • 将改动的文件和项目提交上传至远端仓库;
  • 打上tag版本,有图形化界面可以图形化界面操作,没有使用命令行执行以下命令;(由于Cocoapods需要依赖项目中的tag,所以tag版本需要和podspec文件中的version保持一致)
// 为 git 提交打上 tag
git tag "0.0.1" 
// 将 tag 推送到远程仓库
git push --tags 
发布至Cocoapods
  • 打开终端 cdpodspec 所在文件目录下,输入以下命令来发布到 CocoaPods
pod trunk push *.podspec --allow-warnings

看到这个 🚀 XXXX (0.0.1) successfully published, 就说明发布成功了。

Tips:

以上是使用Carthage包管理工具来打包,当然我们也可以使用CocoaPods的插件cocoapods-packager来完成类库的打包,或者使用手动编译打包,但是这个过程会相对繁琐。下面我会讲述下如何使用CocoaPods的插件:

安装打包插件

sudo gem install cocoapods-packager

开始打包

pod package [name].podspec --force

默认是framework文件;如果需要打成.a文件,命令后添加--library;
如果需要强制覆盖,命令后添加--force;

参考:
iOS SDK开发
iOS 安装homebrew
利用 Carthage 将项目打包 Framework 并依赖 CocoaPods 第三方库的方法
SSH公钥

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 217,277评论 6 503
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,689评论 3 393
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 163,624评论 0 353
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 58,356评论 1 293
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 67,402评论 6 392
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 51,292评论 1 301
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 40,135评论 3 418
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,992评论 0 275
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,429评论 1 314
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,636评论 3 334
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,785评论 1 348
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,492评论 5 345
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 41,092评论 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,723评论 0 22
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,858评论 1 269
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,891评论 2 370
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,713评论 2 354

推荐阅读更多精彩内容