将 Objective-c 框架导入 Swift 框架(Google Analytics + Cocoapod) [英] Import Objective-c framework into Swift framework (Google Analytics + Cocoapod)

查看:47
本文介绍了将 Objective-c 框架导入 Swift 框架(Google Analytics + Cocoapod)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将我常用的 Swift 代码集中到一个框架中,并且该代码的一部分使用了 Google Analytics.我将 Google Analytics 作为 Cocoapod 引入,但我无法像从原始项目那样从新框架访问它,因为它是 Objective-C 并且框架中没有桥接头支持 [我使用的是 Swift 1.2].

我通常在桥接标头中使所有这些工作的代码行是:

<块引用>

#import

我到底应该把它放在我的项目中的什么位置,才能让这一切像之前在桥接头中那样工作?

我在 Apple 关于混合 Swift 和Objective-C 是这样的:

<块引用>

从同一框架目标内导入代码

如果您正在编写混合语言框架,您可能需要访问Swift 中的 Objective-C 代码和 Objective-C 中的 Swift 代码.

将 Objective-C 导入 Swift

在相同的框架目标中导入一组 Objective-C 文件您的 Swift 代码,您需要将这些文件导入框架的 Objective-C 伞头.

将 Objective-C 代码从同一个框架导入 Swift

在构建设置下,在打包中,确保定义模块该框架目标的设置设置为是.在你的伞里头文件,导入要公开的每个 Objective-C 头文件迅速.例如:OBJECTIVE-C

导入

导入

导入

我认为最相关的一句话是:

<块引用><块引用>

您需要将这些文件导入框架的 Objective-C 伞头

但是这个文件是什么以及如何创建它?

Apple 的文档之前提到过(在表格中):

<块引用>

Objective-C 代码

导入 Swift

#import "Header.h"

好吧,我尝试创建一个文件Header.h"并导入它,但这不起作用.我不知道他们想说什么.我在构建设置中找不到任何伞".

所以我的问题是,如何在我的 Swift 项目中导入这个文件 (#import <Google/Analytics.h>),以便它可以像我通常在一个普通的项目?

更新:

我开始相信,也许 Objective-c 桥接头是与项目同名的 .h 文件.我现在尝试在那里添加导入语句,我得到的错误是:

<块引用>

<强>!在框架模块JBS"中包含非模块化标头

解决方案

解决方案不像应用程序那么简单.我们必须创建一个模块映射.

看看这个示例存储库.

<块引用>

在 Swift 代码中,我们只能导入所谓的模块.诀窍是定义一个模块,该模块又包含我们需要 Swift 代码访问的所有 ObjC 头文件.

本文的模块图部分可能也能帮到你.

<块引用>

尽管桥接头很方便,但它有一个关键限制——你不能在框架项目中使用它.另一种方法是使用模块.

为此,请在您的项目目录中创建一个以您要使用的库命名的目录.我在 Xcode 支持之外的 shell 中执行此操作,并将其命名为 CommonCrypto.在该目录中,创建一个封装库设置的 module.map 文件.对于 CommonCrypto,module.map 看起来像这样:

module CommonCrypto [系统] {标题/usr/include/CommonCrypto/CommonCrypto.h"出口 *}

现在将新模块添加到 Swift Compiler 下的 Import Paths – 项目设置中的 Search Paths.在模块路径(例如 ${SRCROOT}/CommonCrypto)中使用 ${SRCROOT} 以确保项目无论在何处签出都能正常工作.

这使得在 Swift 文件中只导入 CommonCrypto 成为可能.请注意,您使用此技术构建的任何框架的使用者也必须将该模块添加到他们的 Swift 搜索路径中.

我遵循了上面文章中的步骤,并以这种方式满足了我的需要:

module Muse {标题Muse.framework/Headers/Muse.h"出口 *}

为了安全起见,我删除了 [system] lexon(因为它删除了警告)并将框架放在与 module.map 文件相同的文件夹中.

此外,不要忘记在您的框架目标中包含 libc++.tbd 和其他必需的依赖项(在 General 的 Linked Frameworks and Libraries 部分 标签),如果你需要它们.

I'm trying to centralize my commonly used Swift code into a framework, and part of that code uses Google Analytics. I brought in Google Analytics as a Cocoapod, but I can't access it from the new framework like I did from the original project because it's Objective-C and there's no bridging header support in frameworks [I'm using Swift 1.2].

The line of code I normally have in the bridging header that makes all of this work is:

#import <Google/Analytics.h>

Where exactly do I put this in my project to make this all work like it did before in the bridging header?

What I found in Apple's documentation about mixing Swift and Objective-C is this:

Importing Code from Within the Same Framework Target

If you’re writing a mixed-language framework, you may need to access your Objective-C code from Swift and your Swift code from Objective-C.

Importing Objective-C into Swift

To import a set of Objective-C files in the same framework target as your Swift code, you’ll need to import those files into the Objective-C umbrella header for the framework.

To import Objective-C code into Swift from the same framework

Under Build Settings, in Packaging, make sure the Defines Module setting for that framework target is set to Yes. In your umbrella header file, import every Objective-C header you want to expose to Swift. For example: OBJECTIVE-C

import <XYZ/XYZCustomCell.h>

import <XYZ/XYZCustomView.h>

import <XYZ/XYZCustomViewController.h>

The phrase I take to be most relevant is where it says:

you’ll need to import those files into the Objective-C umbrella header for the framework

But what is this file and how to you create it?

Apple's documentation mentions earlier (in a table):

Objective-C code

Import into Swift

#import "Header.h"

Well, I tried just creating a file "Header.h" and importing it, but that doesn't work. I don't know what they're trying to say. I can't find an "umbrella" anything in the build settings.

So my question is, how can I import this file (#import <Google/Analytics.h>) in my Swift project so that it can see the Google Analytics cocoapod framework like I would normally do in the bridging header of a normal project?

Update:

I've come to believe that perhaps the objective-c bridging header is the .h file of the same name as the project. I've now tried adding the import statement there, and the error I get is:

! Include of non-modular header inside framework module 'JBS'

解决方案

The solution is not as straightforward as for an app. We have to create a modulemap.

Have a look at this example repo.

Inside Swift code we can only import so called modules. The trick is to define a module which in turn contains all the ObjC headers that we need the Swift code to access.

The module map section of this article may also help you.

As convenient as the bridging header is, it has one key limitation—you can’t use it inside a framework project. The alternative is to use a module.

To do this, create a directory in your project directory named after the library you want to use. I did this in the shell, outside Xcode’s auspices, naming it CommonCrypto. Inside the directory, create a module.map file that encapsulates library settings. For CommonCrypto, module.map looks like this:

module CommonCrypto [system] { header "/usr/include/CommonCrypto/CommonCrypto.h" export * }

Now add the new module to Import Paths under Swift Compiler – Search Paths in your project settings. Use ${SRCROOT} in the module path (e.g. ${SRCROOT}/CommonCrypto) to insure that the project works no matter where it’s checked out.

This makes it possible to just import CommonCrypto in your Swift files. Note that consumers of any frameworks you build using this technique are also going to have to add the module to their Swift search paths.

I followed the procedure in the article above and made it work for my need this way:

module Muse { header "Muse.framework/Headers/Muse.h" export * }

I removed the [system] lexon for safety (as it removes warning) and put the framework inside the same folder as the module.map file.

Also, don't forget to include libc++.tbd and other required dependencies in your framework target (in the Linked Frameworks and Libraries section of the General tab) if you need them.

这篇关于将 Objective-c 框架导入 Swift 框架(Google Analytics + Cocoapod)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆