pod引用本地库

2019-11-22  本文已影响0人  IT卡农

1.大体描述

项目希望应用App的业务代码分成组件, App内部只保留少量的代码. 所以讲App/目录下: Chat, Common, Information, Mine, Score作为本地库.(也可以在本机的别的目录下,如desktop) (见图1)
其实感觉Common的有些App私有的东西,可以放在App里边,不需要作为库

最后项目的目录结构如下 (见图2)
红框1: 代表app的项目文件, 只有基本的配置文件+AppDelegate
红框2:本地pod库
红框3:远程pod库
注意: 因为导入了本地pod库, 所以自动生成了红框2的"DevelopmentPods"目录

图1.png 图2.png

2.podfile文件

# git库
pod 'AFNetworking', '3.2.1'
pod 'MJRefresh', '3.2.0'
pod 'MJExtension', '3.1.0'
# svn库
pod 'LoginLib', :svn =>'https://xxx/trunk/LoginLib'
pod 'LoginKit', :svn =>'https://xxx/trunk/LoginKit'
# 本地文件
pod 'Common', :path => './xxx/App/Common/'
pod 'Score', :path => './xxx/App/Score/'
pod 'Chat', :path => './xxx/App/Chat/'
pod 'Mine', :path => './xxx/App/Mine/'
pod 'Information', :path => './xxx/App/Information/'

3.podspec文件配置

3.1Chat

  spec.prefix_header_file = 'Chat/Chat.h'
  spec.source_files  = 'Chat', 'Chat/**/*.{h,m}' //代表任何子孙目录的.h.m文件都导入

  spec.resource_bundles = {
    'ChatBundle' => [ 'Chat/Resources/**/*.xcassets',
                      'Chat/Resources/**/*.xib' ]
  }

  spec.frameworks = 'Foundation', 'UIKit', 'CoreLocation', 'CoreGraphics', 'QuartzCore', 'OpenGLES', 'ImageIO', 'SystemConfiguration', 'Security'
  spec.libraries = 'c++', 'z', 'sqlite3', 'icucore'

  spec.dependency 'Common'//Chat依赖Common库
  spec.dependency 'Masonry'

上边podspec的作用, 在项目中的体现(见图3)
注意: 有些文件夹图标左下角没有三角形图标

如图3.png

3.2Common

spec.prefix_header_file = 'Common/Common.h'
  
  spec.source_files  = 'Common/*' //*代表向下一层所有文件, **不但包括子文件也包括孙(孙孙\...)文件
  
  spec.resource_bundles = { 'CommonBundle' => ['Common/Resources/*.xcassets',
                                            'Common/Resources/*.xib',
                                            'Common/Util/UI/KKFont(字体)/src/*.{ttf,OTF}']
  }

  spec.frameworks = 'Foundation', 'UIKit', 'CoreGraphics', 'ImageIO', 'SystemConfiguration', 'Security'
  spec.libraries = 'c++', 'z', 'sqlite3'
 
  spec.dependency 'Bugly'
  spec.dependency 'AMapLocation'
  spec.dependency 'AMapSearch'

####################### subspec ########################
  spec.subspec 'Header' do |header|
    header.source_files = 'Common/Header/**/*.{h,m}'
  end
  
  spec.subspec 'Base' do |base|
    base.source_files = 'Common/Base/**/*.{h,m}'
  end

  spec.subspec 'EventPoint' do |eventPoint |
    eventPoint.source_files = 'Common/EventPoint/**/*.{h,m}'
  end

  spec.subspec 'Util' do |util|
    util.source_files = 'Common/Util/*'
    
    util.subspec 'UI' do |ui|
      ui.source_files = 'Common/Util/UI/**/*.{h,m}'
    end
    
    util.subspec 'Foundation' do |foundation|
      foundation.source_files = 'Common/Util/Foundation/**/*'
      
      foundation.subspec 'NSObjectSafe' do |safe|
        safe.requires_arc = false
        safe.source_files = 'Common/Util/Foundation/NSObjectSafe/**/*.{h,m}'
      end

    end
    
  end
  
  spec.subspec 'Account' do |account|
    account.source_files = 'Common/Account/*.{h,m}'
  end
  
  spec.subspec 'Router' do |router|
    router.source_files = 'Common/Router/*.{h,m}'
  end
  
  spec.subspec 'Configuration' do |configuration|
    configuration.source_files = 'Common/Configuration/*'
    
    configuration.subspec 'Network' do |network|
      network.source_files = 'Common/Configuration/Network/*.{h,m}'
    end
    
    configuration.subspec 'SDK' do |sdk|
      sdk.source_files = 'Common/Configuration/SDK/*.{h,m}'
    end
    
  end

end

podspec中设置了subspec的目录, 都变成了左下角带小三角的样子, 没设置的不带

图4.png

3.3换种方式写Common

 spec.prefix_header_file = 'Common/Common.h'
  
  spec.source_files  = 'Common', 'Common/**/*.{h,m}' //Common下的所有.h.m文件
  
  spec.resource_bundles = { 'CommonBundle' => ['Common/Resources/*.xcassets',
                                            'Common/Resources/*.xib',
                                            'Common/Util/UI/KKFont(字体)/src/*.{ttf,OTF}']
  }

  spec.frameworks = 'Foundation', 'UIKit', 'CoreGraphics', 'ImageIO', 'SystemConfiguration', 'Security'
  spec.libraries = 'c++', 'z', 'sqlite3'
  
  spec.dependency 'Bugly'
  spec.dependency 'AMapLocation'

  ####################### subspec ########################

  spec.subspec 'NSObjectSafe' do |safe|
    safe.requires_arc = false
    safe.source_files = 'Common/**/NSObjectSafe/*.{h,m}'
  end

  ####################### subspec ########################

spec.source_files和subspec还可以这么写
这里的subspec不起子目录作用, 只是指定NSObjectSafe目录下的.h.m文件不要求用ARC

图5.png

3.4总结

1.注意例子中spec.source_files的写法
2.注意subspec的写法, 3.2中在作为远程库时候, 设置了subpec的目录还会以文件夹目录的形式出现(带小三角), 而3.1和3.3的粗暴写法会将主目录(Chat/Common)下的所有.h.m导入进去其中,而没有子目录。
3.requires_arc: 可以对文件作ARC设定

上一篇 下一篇

猜你喜欢

热点阅读