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

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

问题描述

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

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


#import< Google / Analytics.h>


我到底在哪里将其放入项目中



我在 Apple有关将Swift和Objective-C混合使用的文档是这样的:


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



如果您正在编写混合语言框架,则可能需要访问
您的Objective-C合作伙伴来自Swift的de和来自Objective-C的Swift代码。



将Objective-C导入Swift



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



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



在构建设置下的打包中,确保将该框架目标的定义模块
设置设置为是。在您的伞形
头文件中,将要公开的每个Objective-C头导入到
Swift中。例如:OBJECTIVE-C


导入< XYZ / XYZCustomCell.h>


导入< XYZ / XYZCustomView.h>


导入< XYZ / XYZCustomViewController.h>


我所用的最相关的短语是:



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



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



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


Objective-C代码

导入Swift

#import Header.h


我尝试创建一个文件 Header.h并将其导入,但是不起作用。我不知道他们想说什么。我在构建设置中找不到伞。



所以我的问题是,如何导入该文件(#import< Google / Analytics。 h>)在我的Swift项目中,以便可以像在正常项目的桥接标题中一样看到Google Analytics(分析)cocoapod框架?



更新:



我开始相信,objective-c桥接标头是与项目同名的.h文件。我现在尝试在其中添加导入语句,并且得到的错误是:


!在框架模块'JBS'中包含非模块化标头



解决方案

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



看看此示例存储库


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


本文部分也可能对您有所帮助。 p>


由于桥接头很方便,它有一个关键的限制-您不能在框架项目中使用它。替代方法是使用模块。



要执行此操作,请在项目目录中创建一个要使用的库命名的目录。我是在Xcode主持之外的Shell中进行此操作的,并将其命名为CommonCrypto。在目录内,创建一个封装库设置的module.map文件。对于CommonCrypto,module.map如下所示:



module CommonCrypto [system] {
标头 / usr / include / CommonCrypto / CommonCrypto.h
export *
}



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



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


I遵循上面文章中的过程,并通过这种方式使其满足我的需要:



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



我删除了 [系统] lexon以确保安全(因为它消除了警告),并将框架与module.map文件放在同一文件夹中。



不要忘记在框架目标中包含 libc ++。tbd 和其他必需的依赖项(在链接的框架和库中) 常规标签中的部分)。


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天全站免登陆