美文网首页知识积累征服iOSiOS自动化
cocoapods系列教程---模块化设计

cocoapods系列教程---模块化设计

作者: youngyunxing | 来源:发表于2016-09-18 18:50 被阅读897次

    cocoapods系列教程---安装与使用
    cocoapods系列教程---Podfile文件
    cocoapods系列教程---Podfile.lock文件 和 Spec Repo
    cocoapods系列教程---spec文件
    cocoapods系列教程---原理篇
    cocoapods系列教程---让自己的开源框架支持cocoapods
    cocoapods系列教程---模块化设计
    cocoapods系列教程---创建私有pods

    虽然是个简单的功能,但是找了很久才找到,也是看了别人的代码才弄明白的~
    先看下常规的目录结构(执行完pod install 之后):

    Paste_Image.png

    明显看到没有任何层次,只有一个文件夹,再看一下AFNetWorking的目录结构:

    Paste_Image.png

    可以看出是有子文件夹的,结构十分清晰.那]这是怎么做到的呢?

    通过PodSpec的subspec可以使一个项目能模块化输出功能 ,一个简单例子:

    spec.source_files = 'SGExtension/SGExtension.h'
    
    spec.subspec 'Category' do |ss|
    ss.source_files = 'SGExtension/Category/{*.h,*.m}';
    ss.library = 'z'
    end
    
    spec.subspec 'Tools' do |ss|
    ss.source_files = 'SGExtension/Tools/{*.h,*.m}';
    end
    
    

    使用之后的效果图:

    Paste_Image.png

    可以参考这篇文章,很详细,下面摘出重要部分.

    Pod::Spec.new do |s|
      s.name         = "iOS_Util"
      s.version      = "0.10.0"
      s.summary      = "Some iOS Util"
    
      s.license      = 'MIT'
    
      s.author       = { "文通" => "wentong@taobao.com" }
    
      s.platform     = :ios, '6.1'
    
      s.ios.deployment_target = '4.3'
    
      s.subspec 'Common' do |cos|
        cos.source_files = 'iOS_Util/Common/*.{h,m}'
        cos.public_header_files = 'iOS_Util/Common/*.h'
      end
    
      s.subspec 'Core' do |cs|
        cs.source_files = 'iOS_Util/Core/*.{h,m}'
        cs.public_header_files = 'iOS_Util/Core/*.h'
        cs.dependency 'libextobjc', '0.2.5'
      end
    
      s.subspec 'Json' do |js|
        js.source_files = 'iOS_Util/Json/*.{h,m}'
        js.public_header_files = 'iOS_Util/Json/*.h'
        js.dependency 'iOS_Util/Core'
      end
    
      s.subspec 'Bean' do |bs|
        bs.source_files = 'iOS_Util/Bean/*.{h,m}'
        bs.public_header_files = 'iOS_Util/Bean/*.h'
        bs.dependency 'iOS_Util/Core'
      end
    
      s.subspec 'DB' do |ds|
        ds.source_files = 'iOS_Util/DB/*.{h,m}'
        ds.public_header_files = 'iOS_Util/DB/*.h'
        ds.dependency 'FMDB/standard' ,'~> 2.1'
        ds.dependency 'iOS_Util/Common'
        ds.dependency 'iOS_Util/Core'
      end
    
      s.subspec 'WebP' do |ws|
        ws.source_files = 'iOS_Util/WebP/*.{h,m}'
        ws.public_header_files = 'iOS_Util/WebP/*.h'
        ws.dependency 'libwebp' ,'~> 0.3.0-rc7'
        ws.frameworks = 'CoreGraphics'
      end
    
      s.subspec 'Location' do |ls|
        ls.source_files = 'iOS_Util/Location/*.{h,m}'
        ls.public_header_files = 'iOS_Util/Location/*.h'
        ls.dependency 'iOS_Util/Common'
        ls.dependency 'iOS_Util/DB'
        ls.frameworks = 'CoreLocation' ,'MapKit'
        ls.resource = 'iOS_Util/Location/chinaDivision.sqlite'
      end
    
      s.subspec 'AMR' do |as|
        as.source_files = 'iOS_Util/AMR/**/*.{h,m,mm}'
        as.public_header_files = 'iOS_Util/AMR/**/*.h'
        as.preserve_paths = "iOS_Util/AMR/**"
        as.library   = 'opencore-amrnb','opencore-amrwb'
        as.xcconfig  = { 'LIBRARY_SEARCH_PATHS' => '"$(PODS_ROOT)/iOS_Util/iOS_Util/AMR/lib"' }
      end
    
     #可以看到通过subspec可以区分出不同的模块,而且模块间也能依赖
    
      s.subspec 'Cache' do |cas|
        cas.source_files = 'iOS_Util/Cache/*.{h,m,mm}'
        cas.public_header_files = 'iOS_Util/Cache/*.h'
        cas.dependency 'iOS_Util/Common'
      end
    
      s.subspec 'Preference' do |ps|
        ps.source_files = 'iOS_Util/Preference/*.{h,m,mm}'
        ps.public_header_files = 'iOS_Util/Preference/*.h'
        ps.dependency 'iOS_Util/Json'
      end
    
      s.requires_arc = true
    
    end
    
      
    

    这样还有个好:在实际项目中我们就可以选择使用整个组件库或者是组件库的某一个部分了,对应的Podfile中添加的内容为

    platform :ios, '7.0'
    
    pod 'iOS_Util/AMR', '1.0.0'  #使用某一个部分
    pod 'PodTestLibrary', '1.0.0'   #使用整个库
    
    

    附:AFNetworking的podspec文件地址:https://github.com/AFNetworking/AFNetworking/blob/master/AFNetworking.podspec

    相关文章

      网友评论

        本文标题:cocoapods系列教程---模块化设计

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