平时生活和工作中的iOSSwifty Coding组件化

组件化开发之-如何解决Swift/OC-Framenwork/L

2017-06-30  本文已影响481人  一根聪

原创 2017-06-28

关键点

  • Swift依赖第三方Objective-C Framework、Library创建Pod
  • Swift与Objective-C代码混合创建Pod
  • Swift与OC、第三方Framework/Library混合创建Pod

在我的另一篇文章中介绍到了如何创建自己的公有、私有Pod,我以为创建Pod的坑就可以很简单的跳过,但是事情不是这样子的,在使用组件化开发私有组件的时候我又遇到了新问题(由于项目采用的是Objective-C开发):

下面来说一说解决办法:

准备工作

在这里我们需要有一个Development Pod让我们来进行开发、测试,首先我们通过pod lib 来创建一个Pod,使其包含Example,Pods等,当然你也可以自己手动完成这一系列事情。

pod lib create Verify-SwiftOC3rd
Cloning `https://github.com/CocoaPods/pod-template.git` into `Verify-SwiftOC3rd`.
Configuring Verify-SwiftOC3rd template.

------------------------------

To get you started we need to ask a few questions, this should only take a minute.

If this is your first time we recommend running through with the guide:
 - http://guides.cocoapods.org/making/using-pod-lib-create.html
 ( hold cmd and click links to open in a browser. )


What language do you want to use?? [ Swift / ObjC ]
 > Swift

Would you like to include a demo application with your library? [ Yes / No ]
 > Yes

Which testing frameworks will you use? [ Quick / None ]
 > None

Would you like to do view based testing? [ Yes / No ]
 > No

Running pod install on your new library.

上述所有的步骤你只需要按照提示信息完成即可,创建成功后将会自动打开项目,最后你看到的将会是:


Swift与OC互相调用流程

二是Swift只支持动态库(并非完全意义的动态库),而我们代码在Pod之后实际上是一个动态的Framework,Swift是有命名空间的一个概念,这时候你需要做的是在引用时需要写明命名空间。基于上述两点,我们只能在OC的.m文件中引用并且加上命名空间:

Swift与OC 代码、 Framework、Library混合创建Pod

上述两个分别讲解了Swift与OC代码混合创建Pod,Swift与OC Framework、Library混合创建Pod,现在我们来将三者混合创建,这里也就是我们解决了三种:

依照上述两个章节所讲,我的.podspec如下:


Pod::Spec.new do |s|
  s.name             = 'Verify-SwiftOC3rd'
  s.version          = '0.1.4'
  s.summary          = '验证私有组件中使用Swift结合OC第三方.framework、.library库及OC文件组合打包构建私有Pod'

  s.homepage         = 'https://github.com/ApterKingRepo/Verify-SwiftOC3rd'
  s.license          = { :type => 'MIT', :file => 'LICENSE' }
  s.author           = { 'ApterKing' => 'wangcccong@outlook.com' }
  s.source           = { :git => 'https://github.com/ApterKingRepo/Verify-SwiftOC3rd.git', :tag => s.version.to_s }

  s.ios.deployment_target = '8.0'

  s.source_files = 'Verify-SwiftOC3rd/Classes/**/*', 'Verify-SwiftOC3rd/Vendors/*.framework/Headers/**.h'
  s.public_header_files = 'Verify-SwiftOC3rd/Classes/**/*.h', 'Verify-SwiftOC3rd/Vendors/BadiduMapAPI_Base.framework/Headers/BMKBaseComponent.h', 'Verify-SwiftOC3rd/Vendors/BadiduMapAPI_Map.framework/Headers/BMKMapComponent.h'
  s.resources = 'Verify-SwiftOC3rd/Vendors/BaiduMapAPI_Map.framework/Resources/mapapi.bundle'
#s.module_name = 'BaiduMapKit'

  s.frameworks   =  'CoreLocation', 'QuartzCore', 'OpenGLES', 'SystemConfiguration', 'CoreGraphics', 'Security', 'CoreTelephony'
  s.libraries    = 'sqlite3.0', 'stdc++.6.0.9'

  s.vendored_frameworks =  'Verify-SwiftOC3rd/Vendors/*.framework'
  s.vendored_libraries = 'Verify-SwiftOC3rd/Vendors/thirdlibs/*.a'

  s.preserve_paths = 'Verify-SwiftOC3rd/Vendors/*.framework', 'Verify-SwiftOC3rd/Vendors/thirdlibs/*.a'
  s.pod_target_xcconfig = {
#'FRAMEWORK_SEARCH_PATHS' => '${PODS_ROOT}/Verify-SwiftOC3rd/Vendors',
            'HEADER_SEARCH_PATHS' => '$(PODS_ROOT)/Verify-SwiftOC3rd/Vendors/*.framework/Headers',
            'LD_RUNPATH_SEARCH_PATHS' => '$(PODS_ROOT)/Verify-SwiftOC3rd/Vendors/',
            'OTHER_LDFLAGS' => '-ObjC'
  }

  s.prepare_command = <<-EOF

    # 业务Module
    rm -rf Verify-SwiftOC3rd/Classes/Modules
    mkdir Verify-SwiftOC3rd/Classes/Modules
    touch Verify-SwiftOC3rd/Classes/Modules/module.modulemap
    cat <<-EOF > Verify-SwiftOC3rd/Classes/Modules/module.modulemap
    framework module Verify {
      umbrella header "Verify_SwiftOC3rd.h"
      export *
      link "sqlite3.0"
      link "stdc++.6.0.9"
    }
    \EOF


    # 创建Base Module
    rm -rf Verify-SwiftOC3rd/Vendors/BaiduMapAPI_Base.framework/Modules
    mkdir Verify-SwiftOC3rd/Vendors/BaiduMapAPI_Base.framework/Modules
    touch Verify-SwiftOC3rd/Vendors/BaiduMapAPI_Base.framework/Modules/module.modulemap
    cat <<-EOF > Verify-SwiftOC3rd/Vendors/BaiduMapAPI_Base.framework/Modules/module.modulemap
    framework module BaiduMapAPI_Base {
      umbrella header "BMKBaseComponent.h"
      export *
      link "sqlite3.0"
      link "stdc++.6.0.9"
    }
    \EOF

    # 创建Map Module
    rm -rf Verify-SwiftOC3rd/Vendors/BaiduMapAPI_Map.framework/Modules
    mkdir Verify-SwiftOC3rd/Vendors/BaiduMapAPI_Map.framework/Modules
    touch Verify-SwiftOC3rd/Vendors/BaiduMapAPI_Map.framework/Modules/module.modulemap
    cat <<-EOF > Verify-SwiftOC3rd/Vendors/BaiduMapAPI_Map.framework/Modules/module.modulemap
    framework module BaiduMapAPI_Map {
      umbrella header "BMKMapComponent.h"
      export *
      link "sqlite3.0"
      link "stdc++.6.0.9"
    }
    \EOF

  EOF

end

运行结果如下:


我将代码上传到了Verify-SwiftOC3rd,欢迎指正
上一篇下一篇

猜你喜欢

热点阅读