15

I am building a framework in which I need to import some objective-c frameworks.

For now I need to import "Beaconstac.framework", but we can not add a bridging header in a swift framework project.

How can I use this framework in my project?

I tried:

import Beaconstac

But the compiler reports error "No Such Module"

Is there any alternative way to do this?

Justicle
  • 14,761
  • 17
  • 70
  • 94
Krishna Verma
  • 814
  • 2
  • 8
  • 23
  • Check this out http://stackoverflow.com/questions/24002369/how-to-call-objective-c-code-from-swift – Babak Nov 19 '15 at 07:04
  • And this https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html – Babak Nov 19 '15 at 07:04
  • this answer is about how to use objective-c framework in swift but i am developing a swift framework not swift application – Krishna Verma Nov 19 '15 at 07:08
  • Doesn't matter you creating framework or application. you should be able to add external files to your project by bridging ! or may be i didn't get your concern? – Babak Nov 19 '15 at 07:17
  • 2
    we can not add a bridging header if we are creating a framework in swift. – Krishna Verma Nov 19 '15 at 07:22
  • check out this answer here this guy is saying we do not need a bridging header in a framework but its not working. http://stackoverflow.com/questions/28815487/building-a-swift-framework-with-references-to-objective-c-code – Krishna Verma Nov 19 '15 at 07:23
  • From the link i shared: "You can import external frameworks that have a pure Objective-C codebase, a pure Swift codebase, or a mixed-language codebase. The process for importing an external framework is the same whether the framework is written in a single language or contains files from both languages. When you import an external framework, make sure the Defines Module build setting for the framework you’re importing is set to “Yes"." – Babak Nov 19 '15 at 07:25
  • Just link the binary in your swift and it does not matter if that external framework is in swift or objc. Try if this works http://stackoverflow.com/a/28937177/366346 – GoodSp33d Nov 19 '15 at 07:25
  • you do *not* need bridging headers, make sure you're actually linking the library. (you can see this in build settings) – Peyman Nov 19 '15 at 07:28
  • . @GoodSp33d is also referring to same concept that has been described in apple developer documents (second link: Importing External Frameworks) – Babak Nov 19 '15 at 07:28
  • I am facing the same issue. Are you able resolve this issue? – Usman Awan Nov 21 '16 at 17:39

3 Answers3

18

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 *
}
  1. Create xcconfig file (File->New->iOS->Other->Configuration Settings file)

  2. In xcconfig file

SWIFT_INCLUDE_PATHS = $(SRCROOT)/
MODULEMAP_PRIVATE_FILE = $(SRCROOT)/module.modulemap

Now you can access ObjC.ClassA in SwiftProj.framework

idmean
  • 14,540
  • 9
  • 54
  • 83
Atul
  • 201
  • 2
  • 5
  • Do you have an example project for this? – snod Jan 11 '17 at 15:37
  • 2
    @girish, I tried this, but not working. How did you make it to work? Could you give me some hint on anywhere that I need to be careful about? Also when you create `.xcconfig` file, did you set the config path anywhere? Appreciate your help. – nuynait Nov 15 '17 at 16:21
  • 2
    @Atul, where to place the xcconfig file? How the project recognize the xcconfig file, too? Could you give more detail about it – Scofield Tran Jul 16 '18 at 06:54
  • @Atul followed same steps. But got error header "ObjC.framework/Headers/ClassA.h" not found. – Sujisha Os May 15 '19 at 06:02
  • what if we are using CocoaPods? where should the `module.modulemap` file be located? – boog May 28 '20 at 09:29
  • if you don't want to using xcconfig file. First go to **project -> build setting**, fine **Import Paths** under **Swift Compiler - Search Paths** input `$(SRCROOT)` , this is for complier to find out the modulemap file that you placed. Second go to **Private Module Map File** under **Packaging** input `$(SRCROOT)/module.modulemap` – hsing May 11 '22 at 03:17
4

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.

dcrow
  • 570
  • 5
  • 11
2

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

heypiotr
  • 2,139
  • 2
  • 15
  • 22