I have an existing Objective-C iOS project and I want to add swift files to it.
The project has 2 targets.
I created the bridging header MyProject-Bridging-Header.h file and the automatically created bridging header MyProject-Swift.h is generated by Xcode as expected. But only for target "A".
Target "B" doesn't see the MyProject-Swift.h file. The reason is, that the target doesn't have the 'swift compiler code generation' that is responsible for the automatic generation of this file.
I tried to set the file on in the project level - I expected I would be inherited to the targets. But this doesn't work.
How do I get he 'swift compiler code generation' into the existing targets build settings manually?
I found out by accident how to do it: create a new swift file and include all necessary targets (Xcode asks at the end of the file setup). Then Xcode asked me if I wanted the Bridging Header to be created and - much more important - added the Swift section to the target
For any new users the setting you are looking for is under "swift compiler - general"
Just scroll down from swift compiler - code generation.
Also make sure you select "all" and not "basic" (at the top) basic hides some features.
Building was an important step for me. Adding the Swift file to the target of interest, creating the bridging header, and building the project with these files included. It's not enough to just add it, then delete them and expect Xcode to update the project build settings for you.
Related
I need to add some Objective-C files of a library to my project, and I created the MyProject-Bridging-Header.h file myself. But now I'm not able to set its path because in the project's Build Settings I don't find the Objective-C Bridging Header entry.
Is somebody else experiencing the same? How could I solve this?
In Build Setting choose "all" instead of "basic"
Usually when you had an Objective-C/Swift project and have added new Swift/Objective-C class file Xcode asks whether you want to create Bridging Header or not.
If the option to create Bridging Header automatically f.e. has been denied there`s still an option to create it manually.
Since you have already created YourProject-Bridging-Header.h header file all you need to set the path to it in your project Build Settings.
To set the path to your Bridging Header file you need to navigate to your project Build Setting where look for Swift Compiler – General section like the Hoa mentioned above. It is located between "Swift Compiler - Custom Flags" and "Swift Compiler - Search Paths" ones at the same page with all "Swift Compiler" sections.
There you can see Objective-C Bridging Header which you can click and set the path to your YourProject-Bridging-Header.h Bridging Header file like YourProjectName/YourProject-Bridging-Header.h.
Now you can open up your YourProject-Bridging-Header.h and put Objective-C/Swift classes using the #import statement into it.
Thanks to http://www.learnswiftonline.com/getting-started/adding-swift-bridging-header/
And Objective-C Bridging Header not getting created with Xcode 8
I am doing an ios app in swift 3. And I want to add ARKitwhich is available in objective-c. I dragged all objective-c files into my project then it asked to create bridging header. So I let it to create. And I imported all objective-c.h files inside that like this.
But when I try to build its showing 22 number of issues like UIViewcannot identify etc..
Then I tried to add the bridging headerpath to build settings under swift compiler - code generation. But I dont see objective-c Bridging headersection under that.
Please help me. What can I do to add these objective-c files into my swift 3 project successfully.
Thanks
The Objective-C bridging header setting can be found under Swift Compiler - General under Build Settings.
But I like to add the bridging header by having Xcode generate it for me. I add a junk Objective-C class by File > New > File... and it will ask me if I want to add a bridging header to my project.
If you do that, you'll only need to move whatever you have in your bridging header now to that one and delete the header you have now. The header will be set properly in the project's settings.
Make sure your build setting is selected as "All" and "Combined" look at below link
Swift Compiler - General is missing in Xcode 9.1
I'm trying to use GoogleidentityToolkit library to handle login an things. I enable use_frameworks! on my pod file, but the module GITkit can't be found. I'm trying to figure out what's going. As far as I know if you use "use_frameworks" you don't need to create any bridging header file, since cocoapods compiles down the library into a single module, so later you can imported as usual on your*.swift files.
What do I need to get using Google Identity Toolkit library in Swift?
This question was asked one week after the release of CocoaPods 1.0.0 (at a time where CocoaPods 0.39.0 was still popular), and available version of Google Identity Toolkit was 1.1.3 from 2015, but got deprecated in favor of Firebase Authentication (pod 'FirebaseUI/Auth') following Google I/O 2016.
A) Create a Bridging Header file named
"ProjectName-Bridging-Header.h" in the root folder of your project.
B) Go to the project build settings and set the following values:
"Install objective-c compatibility header" : YES
"Objective-C Bridging Header" : path of your bridging header (e.g. "ProjectName/ProjectName-Bridging-Header.h"
After that you can use the header file to import all your ObjectiveC files which you want use within swift code.
NOTE: if required set the path as a recursive both in the resource headers and the Swift compiler search section.
None of the answers above worked for me or weren't precise enough.
In Xcode 11.4 (Swift 5.2) this solution worked for me:
1. Create a new header file in your project's root directory. I'm not sure if the name of the file actually matters, but Apple's auto-generated bridging header files are named "ProjectName-Bridging-Header.h".
2. Add all the imports you need to the newly created file.
3. In Project Navigator click on your project's name.
4. In the topmost bar choose "Build settings", and in the one a bit lower choose All and Combined.
5. Search for "Swift Compiler" in the upper right corner
6. Find "Swift Compiler- General" tab, expand it and double-click the right side of "Objective-C Bridging Header".
7. All you need to do now is just drag the bridging header file you've created into the pop-up window and hit enter. You're all set!
*Remember that you'll have to update the path to your Bridging Header every time you project's direct path changes
The easiest way I've found is to create a fake .swift file within XCode. This should bring up the prompt to automatically create a bridging header.
File > New > File...
For the filetype, choose Swift.
Allow Xcode to manually create the Swift Bridging Header.
Delete the .swift file you originally created.
Add a new file to Xcode (File > New > File), then select “Source” and click “Header File“.
Name your file “YourProjectName-Bridging-Header.h”.
Create the file.
Navigate to your project build settings and find the “Swift Compiler – Code Generation” section. You may find it faster to type in “Swift Compiler” into the search box to narrow down the results. Note: If you don’t have a “Swift Compiler – Code Generation” section, this means you probably don’t have any Swift classes added to your project yet. Add a Swift file, then try again.
Next to “Objective-C Bridging Header” you will need to add the name/path of your header file. If your file resides in your project’s root folder simply put the name of the header file there. Examples: “ProjectName/ProjectName-Bridging-Header.h” or simply “ProjectName-Bridging-Header.h”.Or, simply drag and drop bridging header file from finder to this empty field. This will automatically add the path of bridging header file.
Open up your newly created bridging header and import your Objective-C classes using #import statements. Any class listed in this file will be able to be accessed from your swift classes.
Swift 4 and Xcode 9.3
Create a Bridging Header file:
Xcode> File/New.../File> Header File
Name the file "ProjectName-bridging-header.h"
Save to root of your project folder
Xcode> Go to Build Settings (In the project explorer pane select the top most item, should be your project name and in the right pane select the "Build Settings" topic)
Just below "Build Setting" make sure "All" and "Combined" is selected
In search box type "swift compiler" and find "Objective-C Bridging Header" item
Collapse it and double click to the right of it to edit
Insert the file name of 1. above -> "ProjectName/ProjectName-bridging-header.h" (note the folder path if bridging file is saved in project folder)
Include the #import 's needed
First create briding header file with named "projectname-bridging-header.h" at your project root level.
Now in build settings set your bridging header file path and its objc compatibility header.
Once done, Clean and build your project its work fine.
When X-code tries to create a bridging header automatically, it crashes every single time, so I followed the instructions on how to manually create a bridging header.
(Create a .h file, name it <#PROJECT_NAME>-Bridging-Header.h, import all the .h files you need?)
Problem is, when I try to instantiate a class in the .swift file that's included in that header, nothing happens (it says that class doesn't exist) Also, in the Bridging Header it doesn't seem to autocomplete my filenames when I try to include them, leading me to believe somethings not linking properly.
Has anyone run into this? Does anyone know how to fix it?
You need to add it to your target's build settings:
In Xcode, if you go into the build settings for your target, and scroll all the way down you'll find a "Swift Compiler - Code Generation" section.
Set "Objective-C Bridging Header" to <#PROJECT_NAME>-Bridging-Header.h
I'm not sure of the correct value for "Install Objective-C Compatibility Header", but it's a yes/no, so you can toggle that if it doesn't work at first.
I tried to create a bridging header myself but for some reason Xcode didn't like it.
So i deleted my custom one, imported an Obj C file which made Xcode ask if I wanted it to create one for me.
I clicked yes, and it worked!
1) create a file called "FMDB-Bridging-Header.h"
inside this file type the following:
#import "FMDB.h"
3) go to Build Settings -> Swift Compiler - Code Generation
- add to 'Objective-C Bridging Header': FMDB-Bridging-Header.h
or if it was placed inside a folder in your project:
FolderName/FMDB-Bridging-Header.h
Add a header file to your project with the name "[your-project-name]-Bridging-Header.h
Go to Build Settings > Build Options and set "Embedded Content Contains Swift Code" to "Yes"
Go to Build Settings > Linking and add "#executable_path/Frameworks" to Runpath Search Paths
Build your project now!
it could help setting the name of the bridging header with its Project root, as "MyProject/MyProject-Bridging-Header.h" into the string value of the Swift Compiler Build key 'Objective-C Bridging Header'
I'm using AddThis to add sharing options in my iOS app.
I have imported the classes and added the -fno-objc-arc flag to all the imported classes since they don't use ARC.
However, when I try to run the app I get a slew of Parse Issues such as:
Expected identifier or '('
Unknown type name 'NSString'
Unknown type name 'Protocol'
...
These errors occur in NSObjCRuntime, NSZone, and NSObject. I have the requisite frameworks included as well. Any ideas?
Including this image if it helps:
I had the same issue on my project when I was trying to mix C code (.h and .c) with Objective-C code. Found the reason of the issue:
Check your .pch file to make sure every Objective-C framework #import (such as #import <UIKit/UIKit.h>) is enclosed in:
#ifdef __OBJC__
#endif
If they're outside of this conditional scope, the compiler will try to import Objective-C frameworks into C source code.
I just changed the filename of Base64Transcoder.c to Base64Transcoder.m, and now the project compiles. I have no idea why this fixes the problem, but it works.
I had the same issue, using C and C++ code with objective C, and i doesnt have a .pch
The easiest solution was to go into your build settings -> Custom Compiler Flags and set the "Other C Flags" to "-x objective-c" and set the "Other C++ Flags" to "-x objective-c++"
this will do the trick with xCode 7.2
I have had the same problem when my project contained .cpp files.
If .cpp file doesn't contain ObjectiveC frameworks(e.g. ) it has to 'Default-C++ Source' type
,
but if .cpp file has ObjectiveC frameworks - it must be as 'Objective-C++ Source'
TLDR: if your PCH file is OK, look through your CPP file headers to see if you've accidentally included any headers for Objective C objects.
The Details:
I got this because I had accidentally included an Objective-C class header in a C++ class header, indirectly. The structure was this:
Compass.h defined a pure Objective C class.
ActionTracker.h defined a C++ class that understood Objective C constructs (via ActionTracker.mm).
HelloWorld.h defined a purely C++ class.
In my original setup, HelloWorld.h included ActionTracker.h, but this was OK as ActionTracker.h didn't yet contain Compass.h. Later, I changed my code and included Compass.h in ActionTracker.h, which then pulled it into HelloWorld.h and I got these errors.
I had this same problem when I tried to move the info.plist file from one directory to another.
This somehow triggered XCode to edit the build phases for that target and significantly increased the amount of "Compile Sources" and "Copy Bundle Resources".
Luckily my project has multiple targets that I use for testing, (i.e. App Demo, App Dev, App Local, App 1.1, App 1.2 etc)
So I just duplicated one of the unaffected targets and renamed it (also renamed the bundle identifier and the build scheme) and this obviously fixed the problem for me since it's not the whole project that was affected but only that specific target.
If you want to try my solution, try to create a new target from scratch, or duplicate and rename any of your un-affected targets.