Import Objective-c framework into Swift framework

2019-02-06 09:12发布

I am building a framework in which I need to import some objective-c frameworks for now I need to import "Beaconstac.framework" but as we can not add a bridging header in a swift framework project so my question is how can I use this framework in my project this is not directly accessible in my project I tried

import Beaconstac

but its giving error "No Such Module"

is there any alternative to do this?

3条回答
我命由我不由天
2楼-- · 2019-02-06 09:49

Steps to include an existing Obj C framework in a swift framework project

Say we are creating an “SwiftProj.framework" project in swift which internally has to use an Objective C “ObjC.framework”

  1. Place the ObjC.framework in Frameworks folder, Link to Swift framework project via Linked Frameworks and Libraries, and create a module.modulemap file at the same level.
  2. In module.modulemap

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

  3. Create xcconfig file (File->New->iOS->Other->Configuration Settings file)

  4. In xcconfig file SWIFT_INCLUDE_PATHS = $(SRCROOT)/ MODULEMAP_PRIVATE_FILE = $(SRCROOT)/module.modulemap

Now you can access ObjC.ClassA in SwiftProj.framework

查看更多
小情绪 Triste *
3楼-- · 2019-02-06 09:50

You need to import the Beaconstac framework in your umbrella header. That is, if you'd ordinarily use, e.g., #import <Beaconstac/Beaconstac.h> in an Obj-C bridging header, for a framework you need to put that in the umbrella header.

See this chapter in Apple's documentation for more info:

https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-ID130

查看更多
4楼-- · 2019-02-06 09:55

Create a file called module.modulemap and include the following contents:

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

Be aware that you need to have the correct path to your Obj-C framework's umbrella header which may differ slightly with what's listed in the example above.

If you are still stuck, I would strongly suggest taking a look at this project.

查看更多
登录 后发表回答