Getting "file not found" in Bridging Header when importing Objective-C frameworks into Swift project
Asked Answered
R

25

109

I have a Swift project for which I'm trying to import ObjC-based frameworks. The framework is located in a directory under the project's path and is referenced by the project in Xcode. It's also added to the "Link Binary with Libraries" in the project's 'Build Phases' page.

However, for some reason, I can't seem to include the framework in the Bridging-Header file. I get the following error:

BridgingHeader.h:5:9: error: 'Parse/Parse.h' file not found
#import <Parse/Parse.h>
        ^
<unknown>:0: error: failed to import bridging header 'BridgingHeader.h'

Things I've checked:

  1. The "Install Objective-C Compatibility Header" is set to "YES".
  2. The Header Search Paths includes the path to the frameworks.

I'm sure I'm missing something, so if anyone has any clues, that would be great.

Reeba answered 16/12, 2014 at 1:23 Comment(1)
i forgot to put library path in Header Search Paths, in this case question helped :DWee
R
91

Found a solution:

  • The "Objective-C Bridging Header" setting (aka SWIFT_OBJC_BRIDGING_HEADER) must be set at the Target level, and NOT the Project level. Be sure to delete the setting value at the Project level.

(to me, it seems like an Xcode bug, since I don't know why it fixes it).

Reeba answered 18/1, 2015 at 6:26 Comment(8)
This is really weird, I had the same error and it turns out I had the header defined at the Project level. Even though I already had it defined at the Target level, I had to remove it from the Project level in order to allow for third-party Frameworks to be added. It defiantly seems like it's a Xcode bug.Kerosene
@MarcoAlmeida, if you have tests target, try to remove "Objective-C Bridging Header" setting from that too. It should help.Ache
Hey there! Have the same issue here :( Although it is not resolved with the described above method...Vilberg
How can I do it set it?Waiver
'The world's most advanced technology company' circa 2016Arborvitae
This worked for me in Xcode 9.2. Still not fixed after that many years...Adulterous
How do you set that?Recurrence
you were right man! its tsill the same with xcode 14! :DNik
C
62

I have the same issue. I changed all my imports from #import "HMSegmentedControl.h" to #import <HMSegmentedControl/HMSegmentedControl.h> for example.

Calceolaria answered 12/2, 2016 at 11:13 Comment(3)
To work with cocoapods this is necessary. In Bridging header never import a pod as #import "abc.h" user #import <abc/abc.h> Thumbs up.Manxman
In my case I converted #import <ios-qr-encoder/UIImage+MDQRCode.h> to #import "UIImage+MDQRCode.h" and it workedOkra
You are the kingFlaming
T
29

I had to add my lib directory in User Header Search Paths:

test target settings - user header search path

In my case the lib directory contains the .a-library file and some header files. These are included in the bridging header file. However, the swift compiler wouldn't find them. Only when I added ${PROJECT_DIR}/lib to the User Header Search Path it built the test target.

(I am using Xcode 6.2 on Mavericks 10.9.5)

Tribade answered 27/4, 2015 at 18:2 Comment(3)
I could resolve this issue by adding Pods/* to the Header Search Paths of the main target. Interestingly, I did not have this problem before adding Swift frameworks as dependencies to my target. Xcode 7 beta 6.Harrisharrisburg
This was exactly what worked for me. I have a framework included in my project that contains library (.a). I had to create a search path to the headers for the library in the User Header Search Paths.Tenderfoot
Also had to import Pods/library folder, thank you so much !Gounod
M
17

We encountered the same error message, with a completely different cause.

Setup:

  • App target, all Obj-C code
  • Unit Test target with a single swift test case & a bridging header referring to app code

When we added the second swift test case, after a clean (or on a team mate's machine), we saw this error when building the unit test target.

It was fixed by adding a dummy Obj-C class to the unit test target.

Mylonite answered 6/3, 2015 at 23:30 Comment(4)
Thank you. This absolutely worked despite making absolutely no sense.Shimberg
I was able to delete the dummy Obj-C class afterwards. My guess is that a side effect of this solution is the creation of a test-target-specific bridging header, which in turn (I suspect) causes Xcode to ignore the host application's bridging header.Semiliquid
Addendum to above: after doing a clean build, I started getting a segmentation fault 11 during compile. After some trial and error, it appears that the dummy .m file has to be present, but it can be empty. Madness.Semiliquid
Adding a dummy objc class to my test target, fixed it for me!Handout
L
5

If using cocoapods, try reinstalling the pods by running the following command.

pod install
Lizarraga answered 2/9, 2015 at 11:20 Comment(1)
This helped me since I'd recently created a new Configuration that wasn't compiling (the same file not found issue), while the other configurations (Debug and Release) were. pod install seems to be required for any new configurations added...Galbanum
N
5

This error appeared when installing with Cocoapods the library CocoaImageHashing. The problem was that the search paths were wrong. So at the Target level, in Build Settings -> Search Paths -> Header Search Paths, the paths were corresponding to non existing folders, for example "${PODS_ROOT}/Headers/Public/CocoaImageHashing", when the folder structure Headers/Public/ did not exist. I added the path ${PODS_ROOT}/CocoaImageHashing and the error disappeared.

Neurath answered 23/2, 2017 at 11:9 Comment(0)
M
5

This happened to me after adding/renaming current configurations and it makes sense.

Every configuration makes use of Configurations Set generated by cocoapods so these things needs to match.

So if you add/rename configurations, these will need to use the right configuration sets, and for that running pod install will do it.

Murrah answered 8/2, 2019 at 6:43 Comment(0)
A
4

Well its little strange but I guess you have to add a resource to your "Copy Bundle Resources" phase of your test target to make it load all headers from your main app target. In my case, I added main.storyboard and it took care of the error.

enter image description here

Anaanabaena answered 28/4, 2015 at 16:20 Comment(0)
P
4

This somehow did the trick for me:

  • Clean project
  • Clean build folder
  • Restart Xcode
Pileup answered 4/8, 2016 at 15:11 Comment(3)
Do come with generic answers not hit & try trics.Shadowy
This particular hit & try trick requires very less effort so it shouldn't hurt to try while you think of other solutions. ;)Pileup
Sadly, this particular hit & try trick is too often the correct answer.Birck
G
4

(Updated as of May 27, 2017)

Xcode 8. Swift Project - importing Objective C.

Things to know:

  1. Bridging header file MUST be saved within the project's folder. (i.e. not saved at the same level that .xcodeproj is saved, but instead one level further down into the folders where all your swift and objective c files are saved). It can still find the file at the top level, but it will not correctly link and be able to import Objective C files into the bridging header file
  2. Bridging header file can be named anything, as long as it's a .h header file
  3. Be sure the path in Build Settings > Swift Compiler - General > Objective C Bridging Header is correctly pointing to you bridging header file that you made
  4. IMPORTANT: if you're still getting "not found", try to first empty your bridging header file and erase any imports you currently have written there. Be sure that the bridging header file can be found first, then start to add objective c imports to that file. For some reason, it will kick back the same "not found" error even if it is found but it doesn't like the import your trying for some reason
  5. You should not #import "MyBridgingHeaderFile.h" in any of your objective C files. This will also cause a "file not found" error
Gormless answered 29/5, 2017 at 16:19 Comment(2)
All these points are checked and still have the same issue :( Any further points should be taken into consideration?Preconception
@AmjadHusseini try to clean your project files then clear the derived data of your project then restart xCode. that worked with meFlightless
A
3

If helps anyone.

In my case my obj-c files were added has a reference folder (the blue folders in xcode) and the header couldn't find them. Just added the files, not the folder, from the finder to xcode and solved.

Ali answered 13/7, 2016 at 22:44 Comment(0)
W
2

I had the same problem. For me the reason was that I was using the same bridging-header for both my App and my Today Extension. My Today Extension does not include Parse, but because it was defined in the bridging-header it was trying to look for it. I created a new bridging-header for my Today Extension and the error dissapeared.

Waverley answered 30/12, 2014 at 16:14 Comment(1)
Doesn't sound like the same issue I have. I don't have a Today extension. In my case, I'm using a bridging header for the app, and the Parse framework is included in the app dependencies. The error also occurs for other frameworks, not just Parse.Reeba
M
2

My framework was working before and suddenly stopped working, and none of these answers were working for me. I removed the framework in Build Phases > Link Binary With Libraries, and re-added it. Started working again.

Marcellusmarcelo answered 19/2, 2016 at 15:34 Comment(0)
M
2

I had an issue and fixed it after spending 2 hours to find. My environment as below:

cocoapod 0.39.0

swift 2.x

XCode 7.3.1

Steps:

  1. project path: project_name/project_name/your_bridging_header.h
  2. In Swift section at Build Setting, Objective-C Bridging Header should be: project_name/your_bridging_header.h
  3. In your_bridging_header.h, change all declarations from .h to #import
  4. In class which is being used your_3rd_party. Declare import your_3rd_party
Millicent answered 16/5, 2016 at 10:31 Comment(2)
Your answer is correct just add @Calceolaria answer in third point to make it more clear. Worked for me thanks.Manxman
What exactly do you mean by "change all declarations from .h to #import" ?Alterant
G
2

If you are using Cocoapods, try:

  1. Quit Xcode
  2. pod deintegrate
  3. pod install

For me, this solved the weird issue.

Grimsley answered 27/7, 2021 at 7:6 Comment(0)
D
1

I had just duplicated an existing scheme and added another configuration to the project. I had to add a configuration with the same name to the framework's project in order for it to also build in the same DerivedData/($AppName)/Build/Products/($CONFIGURATION_NAME) folder. Otherwise the .framework file doesn't get created and therefore can't be imported.

Darn answered 16/9, 2016 at 19:35 Comment(0)
S
1

I ran into the same issue today when trying to use a pod written in Objective-C in my Swift project, none of the above solutions seemed to work.

In the podfile I had use_frameworks! written. Commenting this line and then running pod installagain solved this issue for me and the error went away.

Systemize answered 18/1, 2017 at 8:18 Comment(0)
B
1

I had a similar issue with pods. Basically trying to run my UI tests, Xcode complained about missing pods. Solution to this was much simpler than any described above:

  1. go to project file (main not a target)
  2. click on the "Info" tab (most left)
  3. set proper pod configuration for UI tests target ("Configurations" section right under "Deployment Target")

Working!

I found it in a thread: https://github.com/CocoaPods/CocoaPods/issues/2695

Sounds a bit like a bug for cocoa pods but I can see reasons why it might be tricky case.

Blumenfeld answered 22/3, 2018 at 14:28 Comment(0)
F
0

I had similar problem and only one solution worked out for me. I tried everything suggested, and I knew that I set my bridging header fine, because I had some other lib working.

When I copied library (drag and drop) into the project, without Cocoapods, only after that I could import headers without errors.

I used facebook/Shimmer library.

Furtado answered 9/2, 2018 at 13:15 Comment(0)
U
0

In my case I just had to quit the simulator...

Unpeopled answered 14/6, 2018 at 11:22 Comment(0)
L
0

Clean project,Clean build folder,Restart Xcode. i just remove path at project goto > Build Settings > Search the keyword. Swift Compiler - General -> Objective-C Bridging header worked for me.

Louettalough answered 5/2, 2019 at 14:12 Comment(0)
F
0

August 2019

In my case I wanted to use a Swift protocol in an Objective-C header file that comes from the same target and for this I needed to use a forward declaration of the Swift protocol to reference it in the Objective-C interface. The same should be valid for using a Swift class in an Objective-C header file. To use forward declaration see the following example from the docs at Include Swift Classes in Objective-C Headers Using Forward Declarations:

// MyObjcClass.h
@class MySwiftClass; // class forward declaration
@protocol MySwiftProtocol; // protocol forward declaration

@interface MyObjcClass : NSObject
- (MySwiftClass *)returnSwiftClassInstance;
- (id <MySwiftProtocol>)returnInstanceAdoptingSwiftProtocol;
// ...
@end
Fiveandten answered 6/8, 2019 at 7:32 Comment(0)
N
0

Adding this to "Other Swift Flags" worked for me:

-Xcc -fmodule-map-file=$(GENERATED_MODULEMAP_DIR)/<YourSwiftModuleImportedFromObjC>.modulemap

Reference: https://developer.apple.com/forums/thread/120152

Northward answered 15/3, 2022 at 19:31 Comment(0)
L
0

By setting User Header Search Paths to your bridge file with recursive worked for me.

Lorenzalorenzana answered 23/7, 2022 at 8:10 Comment(0)
R
0

For me, it happened after renaming the project. All I had to do is renaming the path from:

Build Settings > Framework Search Paths

to match the new project name.

enter image description here

Rectangle answered 15/1, 2023 at 11:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.