美文网首页
cocoapod私有化总结

cocoapod私有化总结

作者: 慧惠 | 来源:发表于2017-03-21 11:43 被阅读488次

    一:首先要知道一些terminal常用的命令:

    pod lib lint --verbose (带详细输出的编译执行.podspec文件)

    pwd (列出当前终端所在的目录)

    tree 文件夹名 -L 2 《打印出指定文件夹下的目录树》

    a、操作git主机的的命令:
    gitremote rm origin (移除当前主机)

    gitremote -v (查看当前主机)

    git remote add origin 网址 (添加origin的主机,主机的网址为命令上的"网址")

    b、升级cocoapod的命令:
    A:gem source -l (查看根源)

    B:pod repo update (将cocoapod远端的spec文件同步到本地)
    :cocoapod有一个远端仓库盛放着一堆的spec文件。然后本地的cocoapod有一个类似命令集的工具,和一个本地的spec文件仓库。
    a、如果 出现这个错误:

    [!] CocoaPods was not able to update the `master-1` repo. If this is an unexpected issue and persists you can inspect it running `pod repo update --verbose`
    

    a.1、先删除全局缓存:

    $ sudo rm -fr ~/Library/Caches/CocoaPods/
    $ sudo rm -fr ~/.cocoapods/repos/master/
    

    a.2、再把当前 Pods 目录清空:

    $ sudo rm -fr Pods/
    

    a.3、再下载cocoapods:

    $ sudo gem install cocoapods
    

    a.3.1、出现错误:

    ERROR:  While executing gem ... (Errno::EPERM)
        Operation not permitted - /usr/bin/pod
    

    执行:

    sudo gem install -n /usr/local/bin cocoapods (升级cocoapod工具的命令)<br>
    

    a.4、再同步:

    $ pod setup 
    

    二:创建私有pod库的步骤与命令:

    1、在gitlab上面创建一个项目'HHlibrary',项目链接为'http://git.gitlab.com/HHlibrary.git'

    2、$ pod repo add HHlibrary http://git.gitlab.com/HHlibrary.git(在~/.cocoapods/repos目录下创建私有Spec Repo,不限在哪个目录执行)

    注意

    pod lib create ProjectName其实使用了默认参数,补全的话pod lib create ProjectName --template-url=https://github.com/CocoaPods/pod-template.git,

    3、$ pod lib create HHlibrary(终端进入选择放项目的文件夹,然后执行)
    注释:之后他会问你四个问题,

    1.是否需要一个例子工程;

    2.选择一个测试框架;

    3.是否基于View测试;

    4.类的前缀;

    4个问题的具体介绍可以去看官方文档,
    我这里选择的是1.yes;2.Specta/Expecta;3.yes;4.PTL。
    问完这4个问题他会自动执行pod install命令创建项目并生成依赖。

    4、tree 文件夹名 -L 2 《打印出指定文件夹下的目录树》

    5、将组件的文件都放入Pod/Classes文件夹下

    6、进入Example文件夹执行'pod update'命令

    7、用xcode打开在Example文件夹中的demo项目,在'Manager Sheme'选择'HHlibrary'

    8、如果组件中有第三方库,设置'library Search paths'

    9、选择'Generic ios Device',Build通过

    10、在gitlab上给HHlibrary添加ssh pubkey,然后cd到PodTestLibrary目录

    $ git add .
    $ git commit -s -m "Initial Commit of Library"
    $ git remote add origin git@coding.net:wtlucky/podTestLibrary.git           #添加远端仓库
    $ git push origin master     #提交到远端仓库
    
    
    //因为podspec文件中获取Git版本控制的项目还需要tag号,所以我们要打上一个tag,
    $ git tag -m "first release" "0.1.0"
    $ git push --tags     #推送tag到远端仓库
    
    



    11、开始编辑podspec文件

    Pod::Spec.new do |s|
      s.name             = 'HHlibrary'
      s.version          = '0.1.0'
      s.summary          = 'HHlibrary'
    
      s.description      = <<-DESC
      Testing Private Podspec.
    TODO: Add long description of the pod here.
                           DESC
    
      s.homepage         = 'http://hh/HHlibrary.git'
      # s.screenshots     = 'www.example.com/screenshots_1', 'www.example.com/screenshots_2'
      s.license          = { :type => 'MIT', :file => 'LICENSE' }
      s.author           = { 'HFavour' => 'hh@qq.com' }
      s.source           = { :git => 'http://hh/HHlibrary.git', :tag => "0.1.0" }
      # s.social_media_url = 'https://twitter.com/<TWITTER_USERNAME>'
    
      s.ios.deployment_target = '8.0'
      s.platform = :ios,'7.0'
      s.requires_arc = true
      s.pod_target_xcconfig = { 'CLANG_ENABLE_OBJC_WEAK' => 'YES'}
    
      s.source_files = 'HHlibrary/Classes/**/*'
      
      s.resource_bundles = {
        'HHlibrary' => ['HHlibrary/Assets/*.xib']
      }
      # 你没用budle包着的话,那就一样处于mainBundle里面,如果你包了一层bundle的话,那就在初始化的时候,指定对应的bundle就行了
    
      # s.public_header_files = 'Pod/Classes/**/*.h'
      s.frameworks = 'CFNetwork', 'Foundation','UIKit'
      # s.dependency 'AFNetworking', '~> 2.3'
    
    
      #配置工程中的mrc文件
      non_arc_files = 'HHlibrary/Classes/SMTPLibrary/*'
      s.exclude_files = non_arc_files#在工程中首先排除一下
      s.subspec 'no-arc' do |sp|#一下就是子设置,为需要添加mrc标识的文件进行设置
      sp.source_files = non_arc_files
      sp.requires_arc = false
      s.frameworks = 'CFNetwork', 'Foundation','UIKit'
      end
    end
    
    

    12、$ pod lib lint --verbose(验证podspec是否可用)

    
     -> HHlibrary (0.1.0)
     
    HHlibrary passed validation
    
    //当你看到时,说明验证通过了,不过这只是这个podspec文件是合格的,不一定说明这个Pod是可以用的,我们需要在本地做一下验证.
    
    

    13、本地测试podspec文件

    //我们可以创建一个新的项目,在这个项目的Podfile文件中直接指定刚才创建编辑好的podspec文件,看是否可用。 在Podfile中我们可以这样编辑。
    platform :ios, '7.0'//是.podspec文件里面的s.platform
    pod 'PodTestLibrary', :podspec => '~/code/Cocoapods/podTest/HHlibrary/HHlibrary.podspec'  #指定podspec文件
    //然后执行pod install命令安装依赖,打开项目工程,可以看到库文件都被加载到Pods子项目中了
    

    14、提交podspec到Spec Repo中

    $ pod repo push HHlibrary HHlibrary.podspec  #前面是本地Repo名字 后面是podspec名字
    //完成之后这个组件库就添加到我们的私有Spec Repo中了,可以进入到~/.cocoapods/repos/WTSpecs目录下查看
    
    

    note1:编写.podspec文件需要注意的地方。
    a、s.frameworks = "UIKit","Foundation" 《注明使用到的framework》

    b、s.vendored_frameworks = '以当前.podSpec文件所在的文件夹为起始文件路径 的 相对路径' 《添加手动拖入的framework的相对路径》

    c、s.libraries = "mtasdk","z", "sqlite3" 《注明使用到的.lib文件,并且文件名应当去掉lib前缀和.xx后缀》

    d、s.vendored_libraries = 'PPStaticAnalysisToolLibrary/Classes/MTA/libmtasdk.a' 添加手动拖入的lib文件的相对路径》

    e、.podspec文件中使用的路径都是相对路径,且起始路径就是当前文件夹

    note2、在文件夹Class下添加第三方依赖库需要注意的地方。
    a、注意在文件夹Class下添加后,还要在拖入xcode时选择target为lib

    b、在TAEGETS为lib的'Build Setting'中的'library Search paths',注意设置好相对路径'$(PROJECT_DIR)/xx/xx/'

    c、注意在xcode中添加第三方库需要依赖的系统库

    note3、获取cocoapod ASSert文件夹底下的xib文件的方法。

        NSBundle *frameworkbundle = [NSBundle bundleForClass:[MailViewController class]];//获取cocoapod所在的bundle的位置
        NSBundle *bundle = [NSBundle bundleWithPath:[frameworkbundle pathForResource:@"HHlibrary" ofType:@"bundle"]];//一般Assert文件夹下的资源所在的位置就是frameworkbundle底下
        self.view = [[bundle loadNibNamed:@"MailViewController" owner:self options:nil] lastObject];
    

    三:pod私有库使用指南:

    1、在profile文件中配置《platform :ios, '9.0'》

    2、在profile文件中引入pod

    示例:

    pod 'HHlibrary',:git => 'http://hh/HHlibrary.git'

    3、在终端执行 'pod install' 或者 'pod update --no-repo-update'

    相关文章

      网友评论

          本文标题:cocoapod私有化总结

          本文链接:https://www.haomeiwen.com/subject/ooownttx.html