Import Objective-c framework into Swift framework (Google Analytics + Cocoapod)

后端 未结 3 726
一整个雨季
一整个雨季 2021-02-19 18:12

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 a

3条回答
  •  爱一瞬间的悲伤
    2021-02-19 18:46

    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.

提交回复
热议问题