设为首页 加入收藏

TOP

使用CocoaPods创建自己的私有库-iOS组件化第一步(一)
2019-08-26 07:03:37 】 浏览:67
Tags:使用 CocoaPods 创建 自己 私有 -iOS 组件 第一

目前iOS组件化常用的解决方案是Pod+路由+持续集成,通常架构设计完成后第一步就是将原来工程里的模块按照架构图分解为一个个独立的pod工程(组件),今天我们就来看看如何创建一个Pod私有库。

新建:pod lib create

假设我们需要创建的库名为TestLib,下面我们使用Pod官方提供的创建模板:

首先进入我们的工作目录,如workspace,输入命令

pod lib create TestLib

创建过程中需要填写几个问题,如下图所示,按个人所需填写:

创建完成以后工程会自动打开,Xcode目录和实际路径有一定区别,截图如下:

解决这个问题也很简单,将文件夹作为Group拖动到Xcode中即可:(如果Xcode工程中本身已经包含Classes目录,可以忽略这一步)

然后删除ReplaceMe.swift文件,在Class目录创建一个名为TestClass的swift文件:

在TestClass中定义ClassA和ClassB,注意其中一个是public的

import Foundation

public class ClassA {   //这是public类,可被外部工程访问
    public let name = "ClassA"
    let age = 18
}

class ClassB {  //不是public类,不能被外部工程访问
    let name = "ClassB"
}

重要:为了让改动生效,一定要重启Xcode一次,然后在Example工程下执行(有时Xcode不会更新Pod代码,需要重启)

pod install

就可以在代码中调用新写的类,注意到只能调用public修饰的属性

到这里使用Pod新建一个私有库就完成了。

验证: pod lib lint (podspec配置文件说明)

新建完成后,我们还需要验证,需要修改配置文件,通过下面的截图路径找到新建的私有库的配置文件:

或者在Xcode里的:

文件内容:

#
# Be sure to run `pod lib lint TestLib.podspec' to ensure this is a
# valid spec before submitting.
#
# Any lines starting with a # are optional, but their use is encouraged
# To learn more about a Podspec see https://guides.cocoapods.org/syntax/podspec.html
#

Pod::Spec.new do |s|
  
  # 名称、版本号、概述
  s.name             = 'TestLib'
  s.version          = '0.1.0'
  s.summary          = 'A short description of TestLib.'

# 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!

# 详细描述
  s.description      = <<-DESC
TODO: Add long description of the pod here.
                       DESC

  # 主页、截图、license证书、作者信息、源代码地址、社交地址
  s.homepage         = 'https://github.com/xxx/TestLib'
  # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'xxx' => 'xxx@xxx.com' }
  s.source           = { :git => 'https://github.com/xxx/TestLib.git', :tag => s.version.to_s }
  # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'

  # iOS版本
  s.ios.deployment_target = '8.0'

  # 源码所在路径
  s.source_files = 'TestLib/Classes/**/*'
  
  # 资源文件所在地址
  # s.resource_bundles = {
  #   'TestLib' => ['TestLib/Assets/*.png']
  # }

  # 对外公开的h文件地址,swift一般用不到
  # s.public_header_files = 'Pod/Classes/**/*.h'
  
  # 包含的系统framework
  # s.frameworks = 'UIKit', 'MapKit'
  
  # 包含的第三方pod
  # s.dependency 'AFNetworking', '~> 2.3'
end

更详细的介绍可以访问官网https://guides.cocoapods.org/syntax/podspec.html

配置好以后我们需要做一次验证,在工程目录下使用命令

pod lib lint

初次验证可能遇到的几个问题:

  • Could not find a `ios` simulator (valid values: ). Ensure that Xcode -> Window -> Devices has at least one `ios` simulator listed or otherwise add one.
    这个问题是pod依赖的组件fourflusher与xcode版本不匹配造成的,可以使用如下命令更新
1.sudo gem uninstall fourflusher
2.sudo gem install fourflusher

必要的话还需要更新pod

sudo gem update cocoapods
  • [!] TestLib did not pass validation, due to 3 warnings (but you can use `--allow-warnings` to ignore them).
    pod验证发现3个及以上的warning就会报这个错,如果只是验证一下工程,能确保对外发布之前能修复,可以使用--allow-warnings

pod lib lint --allow-warnings

如果验证通过,会看到TestLib passed validation.,到这一步既完成验证

版本:iOS和Swift管理

通过pod官方模板做出来的工程iOS版本为8.0,Swift版本为4.0,我们有时需要根据需要修改版本号,需要在spec文件中添加:

  # iOS版本
  s.ios.deployment_target = '9.0'

  # Swift版本
  s.swift_versions = '5.0'

首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇正确设置越狱版ios的终端编码--命.. 下一篇【Objective-C】Objective-C语言..

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目