Xcode not automatically creating bridging header?
Asked Answered
P

9

87

I imported an Obj-C file into my swift project and Xcode automatically prompted me to create a bridging header file as expected. However, I deleted both the files (moved to trash) to make some changes, but when i try to import the Obj-C file once again, the prompt doesn't come up anymore. What is the reason? I even tested this same scenario in another project, It seems once I delete Xcode's auto created bridging header, It won't bring the prompt up again another time. Why is this?

Porism answered 30/7, 2015 at 6:18 Comment(0)
R
187

The reason of your issue is, the Xcode build settings still holds the path to auto generated Bridging Header file. You cannot get any build errors because of the header file (ProjectName-Bridging-Header.h) still exist in your project directory.

How to resolve:

Click on your project target, Go to Build Settings tab (choose all instead of basic), search for Bridging Header. You can see the Xcode generated path entry. Select it and click on delete button.

Also, make sure to delete the Xcode auto generated bridging header file (ProjectName-Bridging-Header.h) from your Xcode project directory.

Now, try to import the Obj-C file once again. You can see the prompt to create a bridging header file as expected.

enter image description here

Richellericher answered 30/7, 2015 at 6:32 Comment(2)
I am trying to add obj-c file into the project but never got that bridging header prompt. I don't have that Bridging option on the build settings either :/Sruti
If Objective-c Bridging Header not found the follow this link #38072429Friedrich
E
52

File -> New File -> Header File

Save it as a YourApp-Bridging-Header

Than go to Build Settings, find "Objective-C Bridging Header" (as previously guys told) and pass way to your Bridging Header as: YourApp/YourApp-Bridging-Header.h Where 'YourApp/' is a name of app. It could contain white space, for example 'Your App/'.

In my case in xCode 7.3.1 it works

Elbertina answered 18/5, 2016 at 9:11 Comment(3)
$(SRCROOT)/$(PROJECT_NAME)/$(SWIFT_MODULE_NAME)-Bridging-Header.h Copy Paste "Objective-C Bridging Header"Hodman
Aman Deep, you are welcome!Elbertina
drag and drop is much saferRomp
B
11

There are different way of creating Bridging Header in xcode as mention/suggested above. Here I would give you the best and easiest way of creating Bridge.

  1. Image1 shows that there is no bridging present at current point of time.

Image1

  1. Image2 show that I have copied a folder containing objective-c header files,after copying a popup will come on screen.

Image2

  1. Image3 is will show the popup message. But after pressing the finish button, you will notice that still there is no bridge present. for reference see image2.

Image3

  1. Now in image5&6 , I just drag and drop the objective-c header file, after doing that you will get popup message.

Image4

  1. After pressing on finish button you get message like Would you like to configure an Objective-C bridging header?. After pressing on Yes Button you get your bridge Header,which you can see in image5.

Image5

if you want to keep your header inside one group then use New Group and drag&drop your file in that group.

Image6

Never create any folder, or else you will get error due to that.

Barytone answered 30/7, 2015 at 9:29 Comment(0)
D
6

have a look under your project settings -> build settings, scroll down to the User-Defined section, see if it has a key called SWIFT_OBJC_BRIDGING_HEADER and if it has a value. if its there it probably thinks you have one already, in that case just recreate the file with the same name.

Deedradeeds answered 30/7, 2015 at 6:21 Comment(4)
no, theres no value with that name. only key there is MTL_ENABLE_DEBUG_INFOPorism
forgot to mention, are you clicked on your app target at the top left and not your project? should be a drop down that has a section called projects then targets, just click on your target if you have not alreadyDeedradeeds
maybe just click the + at the top and add a new entry with that key, then with a value APPNAME-Bridging-Header.h then just create that file in your project as well, hopefully can use it then.Deedradeeds
Ramshads answer is probably more accurate, just realised my project im using as an example is the wrong way round, its an obj-c project importing swift, so the key is probably not rightDeedradeeds
M
6
  1. Bridge file only prompt once in a project.

  2. If you delete the file by accident, you have to build a new bridge file. It's the same.

  3. Go to File->New File->Header File, and the name should be YourProjectName-Bridging-Header, or else It can't be recognised.

Mignonne answered 30/7, 2015 at 6:31 Comment(0)
M
1

After dealing with this issue around one hour. I found the real issue was folder!

Never Create/Copy folder.

To solve the issue-

First Create a group of any name you like. Then copy Objective-C files into the group (not folder).

Then definitely you will be prompt for Bridging Header File Creation.

Miramontes answered 25/3, 2020 at 16:53 Comment(0)
P
0

If you want to configure it manually simply go to Build Settings and change the selection to ALL from Basic (Default) or customized.

Because empty fields are hidden to make things easier for the average end user, if you miss the prompt to add the bridging-header you may have difficulty finding it manually. Also the search option will not include hidden fields.

Change to All and the Swift Compiler - General section should appear with a child setting of "Objective-C Bridging Header"

Photovoltaic answered 28/1, 2019 at 21:42 Comment(0)
B
0

Bridge file only prompt once in a project.

If you delete the file by accident, you have to build a new bridge file. It's the same.

Go to File->New File->Header File, and the name should be YourProjectName-Bridging-Header, or else It can't be recognised.

If you are working with react native finally you need to add these lines:

//
//  Use this file to import your target's public headers that you would like to expose to Swift.
//
#import <React/RCTBridgeModule.h>
#import <React/RCTViewManager.h>
#import <React/RCTLog.h>

#import <Foundation/Foundation.h>
#import <React/RCTEventEmitter.h>
#import <React/RCTConvert.h>

@interface RCT_EXTERN_MODULE(core, NSObject)
@end
Bashemeth answered 17/8, 2022 at 8:23 Comment(0)
F
-4
  1. open xcode project
  2. File -> new -> File -> if you want .swift file then click on Swift File .h file click on Header File .m file objective c file etc
Faxan answered 10/11, 2017 at 10:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.