Import Objective-c framework into Swift framework project
Asked Answered
H

3

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?

Headlight answered 19/11, 2015 at 7:1 Comment(11)
Check this out #24002869Teagan
And this developer.apple.com/library/ios/documentation/Swift/Conceptual/…Teagan
this answer is about how to use objective-c framework in swift but i am developing a swift framework not swift applicationHeadlight
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?Teagan
we can not add a bridging header if we are creating a framework in swift.Headlight
check out this answer here this guy is saying we do not need a bridging header in a framework but its not working. #28815987Headlight
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"."Teagan
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 https://mcmap.net/q/683471/-building-a-swift-framework-with-references-to-objective-c-codeLatter
you do not need bridging headers, make sure you're actually linking the library. (you can see this in build settings)Vie
. @Latter is also referring to same concept that has been described in apple developer documents (second link: Importing External Frameworks)Teagan
I am facing the same issue. Are you able resolve this issue?Balkan
M
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

Mackler answered 19/11, 2015 at 20:16 Comment(3)
its giving me error "include non modular header inside framework module"Headlight
@KrishnaVerma How did you fix that?Ari
@KrishnaVerma I have the same issue. Did you find the solution?Peacoat
E
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

Eliason answered 26/7, 2016 at 18:9 Comment(6)
Do you have an example project for this?Dissatisfaction
@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.Crick
@Atul, where to place the xcconfig file? How the project recognize the xcconfig file, too? Could you give more detail about itBlob
@Eliason followed same steps. But got error header "ObjC.framework/Headers/ClassA.h" not found.Shannonshanny
what if we are using CocoaPods? where should the module.modulemap file be located?Misfile
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.modulemapSquirmy
B
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.

Blastoderm answered 1/4, 2016 at 0:12 Comment(0)
M
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

Mackler answered 19/11, 2015 at 20:16 Comment(3)
its giving me error "include non modular header inside framework module"Headlight
@KrishnaVerma How did you fix that?Ari
@KrishnaVerma I have the same issue. Did you find the solution?Peacoat

© 2022 - 2024 — McMap. All rights reserved.