【问题标题】:Include Pod's Bridging-Header to Build Settings of Project's Target?包括 Pod 的桥接头来构建项目目标的设置?
【发布时间】:2015-08-16 23:35:40
【问题描述】:

我创建了一个包含两个文件的objective-c pod:

Source/SomeViewController.h
Source/SomeViewController.m

我还在 pod 中创建了一个桥接头:

Source/Bridging-Header.h

内容:

#import "SomeViewController.h"

我的 podspec 如下所示:

Pod::Spec.new do |s|
  s.name = 'TestLib'
  s.version = '0.0.1'
  s.license = 'MIT'
  s.ios.deployment_target = '7.0' 
  s.source_files = 'Source/*.{h,m}'
  s.requires_arc = true
  s.xcconfig = { 'SWIFT_OBJC_BRIDGING_HEADER' => 'Source/Bridging-Header.h' } 
end 

我创建了一个演示项目 pod init 并插入了我的 pod。然后在pod install 之后,我得到以下输出:

安装 TestLib 0.0.1(原为 0.0.1) 生成 Pods 项目 整合客户项目

[!] The `TestLibProject [Debug]` target overrides the `SWIFT_OBJC_BRIDGING_HEADER` build setting defined in `Pods/Target Support Files/Pods-TestLibProject/Pods-TestLibProject.debug.xcconfig'. This can lead to problems with the CocoaPods installation
    - Use the `$(inherited)` flag, or
    - Remove the build settings from the target.

[!] The `TestLibProject [Release]` target overrides the `SWIFT_OBJC_BRIDGING_HEADER` build setting defined in `Pods/Target Support Files/Pods-TestLibProject/Pods-TestLibProject.release.xcconfig'. This can lead to problems with the CocoaPods installation
    - Use the `$(inherited)` flag, or
    - Remove the build settings from the target.

当我打开 TestLibProject.xcworkspace 文件时,我看到 pod 已正确安装,但 pod 中的桥接头未正确安装。我尝试做的 Swift 项目:

let vc: SomeViewController

这会产生错误,因为未安装 pod 的桥接头。

如何配置 podspec 才能正确安装 pod 的桥接头?

【问题讨论】:

    标签: ios objective-c swift cocoapods bridging-header


    【解决方案1】:

    Podspecs 构建框架,框架不能包含桥接头。如果要将非模块化代码导入 Swift 框架,则需要使用自定义模块映射,而不是 egMyLib/module.modulemap

    framework module MyLib {
        umbrella header "MyLib.h"
    
        // Load an SDK header, e.g. CommonCrypto.h
        header "/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Developer/SDKs/iPhoneOS.sdk/usr/include/CommonCrypto/CommonCrypto.h"
    
        export *
        module * { export * }
    }
    

    在那里,您可以在您的 Xcode 项目中指定自定义模块映射(作为 .xcconfig 文件中的 MODULEMAP_FILE 设置,或作为 模块映射文件 目标的构建设置

    现在,拼图的最后一块:podspec。你需要设置module_map:

    Pod::Spec.new do |s|
      # …
      s.module_map = 'MyLib/module.modulemap'
    end
    

    以上是SQLite.swift如何分发自己,既作为一个通用框架,也作为一个pod。


    编辑: 似乎我错过了原始问题的重点,正如in this thread 所澄清的那样。 OP 希望使用 pod 框架的桥接头自动将自身加载到安装项目的 Swift 代码中。这是不可能的。即使 Swift 框架确实支持桥接头,它们也只能将 Objective-C/C 代码(ie私有框架代码)加载到框架的 Swift 代码中。 p>

    【讨论】:

    • 嗨,我尝试通过设置 s.module_map 设置将模块映射文件添加到我的 pod 规范,但是当我尝试 pod lint 时,我不会解析模块(位于 . h 文件)这很有趣,因为它在 Xcode 中运行良好,你有线索吗?