iOS新建cocoapods库

111 阅读1分钟

由于这要链接GitHub,所以你得有个好的环境才行,不然慢且时常报错

1、在GitHub上新建一个Git仓库

image.png

注意:问了避免最后出现一些问题,取的名字最好在GitHub上搜一下,避免名字相同

2、进入查看并拷贝改地址

image.png

3、进入终端,输入Git 指令,将库clone下来

git clone https://github.com/CheneyDeveloper/FirstFramework.git

4、新建 framework 项目放到clone下来的文件夹里面,根据自己的项目需求进行相应的设置。 新建一个Service文件

public class Service {
    public static func doSomethine() -> String {
        return "Service success"
    }
}

5、终端进入FirstFramework 文件下,依次输入Git指令

git add .
git commit -m 'commit'
git push origin main

由于国内访问GitHub的原因,有时可能会报错,多操作几遍就可以了😒 push 成功后在仓库上就可以看见了

image.png

6、进入framework项目文件下,依次输入指令

//打一个tag传入GitHub
git tag 1.0.0
git push --tags
//这样在GitHub上就可以看见1.0.0的tag包

//创建podfile
pod init
//创建pod库的spec文件
pod spec create FirstFramework
//打开spec文件
open FirstFramework.podspec -a xcode

打开podspec文件后,根据项目需求做如下配置

Pod::Spec.new do |spec|
  spec.name         = "name"
  //与tag包相同的版本号
  spec.version      = "1.0.0"
  spec.summary      = "一个简单的介绍"
  spec.description  = "name描述"
  //仓库地址,此处不需要带 .git
  spec.homepage     = "https://github.com/PATH"
  # spec.screenshots  = "www.example.com/screenshots_1.gif", "www.example.com/screenshots_2.gif"
  spec.license      = "MIT"
  # spec.license      = { :type => "MIT", :file => "FILE_LICENSE" }
  spec.author             = { "userName" => "eamil" }
  # Or just: spec.author    = "iOSer"
  # spec.social_media_url   = "https://twitter.com/iOSer"
  # spec.platform     = :ios
  spec.platform     = :ios, "11.0"
  
  # spec.ios.deployment_target = "11.0"
  # spec.osx.deployment_target = "10.7"
  # spec.watchos.deployment_target = "2.0"
  # spec.tvos.deployment_target = "9.0"
  spec.source       = { :git => "https://github.com/PATH.git", :tag => "#{spec.version}" }
  spec.source_files  = "name/**/*"
  #spec.exclude_files = "Classes/Exclude"
  # spec.public_header_files = "Classes/**/*.h"
  # spec.resource  = "icon.png"
  # spec.resources = "Resources/*.png"
  # spec.preserve_paths = "FilesToSave", "MoreFilesToSave"
  # spec.framework  = "SomeFramework"
  # spec.frameworks = "SomeFramework", "AnotherFramework"
  # spec.library   = "iconv"
  # spec.libraries = "iconv", "xml2"
  spec.swift_version = '4.0'
  spec.pod_target_xcconfig = { 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'arm64' }
  spec.user_target_xcconfig = { 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'arm64' }
  # spec.requires_arc = true
  # spec.xcconfig = { "HEADER_SEARCH_PATHS" => "$(SDKROOT)/usr/include/libxml2" }
  # spec.dependency "JSONKit", "~> 1.4"
end

配置完后,执行指令

pod spec lint
//根据报错提示,看后面是加 --verbose   还是 --allow-warnings

说明: (1)--verbose:表示显示全部的日志信息,建议加上这个,方便判断错误信息。 (2)--sources:如果我们在podspec里面依赖到一些私有的库之后,直接进行校验是会报错的提示找不到,这里加上我们的Spec仓库的地址告诉CocoaPods找不到的时候去哪里找。 (3)--allow-warnings:表示允许警告. (4)--use-libraries:表示使用静态库或者是framework,这里主要是解决当我们依赖一些framework库后校验提示找不到库的时候用到。

出现warning 可以不用管,有error就需要去解决,有的error是再执行一遍就可以了!有的则是需要解决,自行看着办😒。结果出现 FirstFramework.podspec passed validation. 则表明podspec验证通过

7、可以验证下这个FirstFramework本地pod库,新建test项目,在pod文件中导入本地pod库

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

target 'NewProject' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!
  pod 'FirstFramework', :path => "../FirstFramework/"
  # Pods for NewProject

end

执行pod install 后,在项目中就可以引用了 image.png

8、cd 到FirstFramework项目文件下 ,执行指令

pod trunk register GitHubEmail 'GitHubUserName'

执行结果

[!] Please verify the session by clicking the link in the verification email that has been sent to **yourgithubemail**

去邮箱里面点击接收到的链接,打开后在回到终端

image.png

执行指令

pod trunk push

没有问题的情况下会出现

image.png

同时邮箱也会收到提示邮件,至此就可以通过pod 的方式导入我们自己的库了。

9、更新版本

修改对应的版本号!提交代码上传对应的tag,执行验证指令 pod spec lint 在执行推送指令 pod trunk push

如果是要上传framework静态库,则需要另外自行打包framework

前面是一样的自行新建一个git仓库,clone到指定文件位置(后面的framework包(不是项目代码)就放到这个位置,然后上传至git仓库,再执行后面的pod相关指令)

framework的podspec文件格式如下

Pod::Spec.new do |spec|
  spec.name         = "name"
  spec.version      = "0.0.1"
  spec.summary      = "A short description of name."
  spec.description  = <<-DESC
                      DESC
  spec.homepage     = "https://github.com/PATH"
  spec.license      = "MIT"
  spec.author             = { "userName" => "email" }
  spec.ios.deployment_target = "11.0"
  spec.source       = { :git => "https://github.com/PATH.git", :tag => "#{spec.version}" }
  spec.requires_arc = true
  spec.vendored_frameworks ='name.framework'
  spec.swift_version = "4.0"
  spec.pod_target_xcconfig = { 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'arm64' }
  spec.user_target_xcconfig = { 'EXCLUDED_ARCHS[sdk=iphonesimulator*]' => 'arm64' }

end

按照下方提示,修改对于的配置项 image.png

image.png

image.png

将如下脚本加入Run Script中

cd ${PROJECT_DIR}

mkdir -p Frameworks_1

cd Frameworks_1

cp -rf ${BUILD_DIR}/${CONFIGURATION}${EFFECTIVE_PLATFORM_NAME}/${PRODUCT_NAME}.framework ./
iphoneosFile=${BUILD_DIR}/${CONFIGURATION}-iphoneos/${PRODUCT_NAME}.framework/${PRODUCT_NAME}
iphonesimulatorFile=${BUILD_DIR}/${CONFIGURATION}-iphonesimulator/${PRODUCT_NAME}.framework/${PRODUCT_NAME}
if [ -f "$iphoneosFile" ] && [ -f "$iphonesimulatorFile" ]; then
lipo -create "$iphoneosFile" "$iphonesimulatorFile" -output ./${PRODUCT_NAME}.framework/${PRODUCT_NAME}
fi

rm -rf ./${PRODUCT_NAME}.framework/_CodeSignature
open "${PROJECT_DIR}/Frameworks_1"

打包时分别运行真机,模拟器。即可得到包含真机模拟器的framework包