Obj C bridging header does not seem to matter? - swift

I have a Swift / Parse iPhone project in Xcode. I have added Parse frameworks and as long as I import Bolts and import Parse in my swift file I am able to use all parse functions. I am now wondering, why do I need the Obj C bridging header at all? Currently there is nothing in it, yet everywhere it says that you need to properly setup your bridging header for Parse to work in Swift projects?

The difference is in your deployment target. For iOS8, you can use embedded frameworks, so you can import frameworks just with a simple
import FrameworkName. If you use CocoaPods, you can add use_frameworks! directive to your podfile and you can then use all pods as Frameworks without a bridging header.
If you want to provide a support for iOS7, you have to still use a bridging header, because embedded frameworks are not supported with this version of iOS.

Related

multiple project with one workspace xcode [duplicate]

Note: I know How to call Objective-C code from Swift, but I don't know below,
I want to use this EsptouchForIOS's Demo in my project. The demo is write in OC, it has a storyboard and controller. I want to know how to integrate the demo in my swift project, and use that storyboard and it's controller in my swift project.
I'll start writing from the very beginning. Suppose you have a project in Objective-C and now you want to continue your project's development in Swift. Follow the below guidelines: (This intends to your specific needs)
First choose to add a new file from File->New->File. In this process select your language as Swift. In the final step here, you will be prompted to Create Bridging Header. Select that:
Now build your project once (⌘+B). You may get an error like this:
Change your target's minimum deployment to the version that Swift supports. (Example in the below screenshot)
To use Objective-C resources in Swift files:
Now that you've got one ProjectName-Bridging-Header.h file in your project. If you want to use any Objective-C class in your Swift files, you just include the header file of that class in this bridging header file. Like in this project, you have ESP_NetUtil and ESPViewController class and their header files too. You want to expose them to Swift and use them later in Swift code. So import them in this bridging header file:
Build once again. Now you can go to your Swift file. And use the Objective-C classes as like you use any resource in swift. See:
N.B: You must expose all the class headers (that you're intending to use later in Swift) in that bridging header file
To use Swift resources in Objective-C files:
Now you may wonder, I've successfully used Objective-C resources in Swift. What about the opposite? Yes! You can do the opposite too. Find your Target->Build Settings->Swift Compiler - General->Objective-C Generated Interface Header Name. This is the header file you will be using inside your Objective-C classes for any Swift to Objective-C interoperability. To know more check here.
Now inside any of your Objective-C class, import that interface header and use Swift resources in Objective-C code:
You will get more understanding from the official apple documentation.
You can checkout the worked out version of your linked project here with Objective-C-Swift interoperability.
So according to your question, you have added an objective C bridge in your swift project using How to call Objective-C code from Swift.
Now, import all headers (.h) files of your objective-c source code (demo project) that you want to direct use in swift file.
For example, your demo project has EsptouchForIOS following header (file with extension .h) files in project source code.
ESPAppDelegate.h, ESPDataCode.h, ESPTouchDelegate.h
import a header file in your bridge, which you want to use in your swift code. Suppose in your swift code you want touch delegate ESPTouchDelegate then write,
#import "ESPTouchDelegate.h"
Here is snapshot of your demo integration in my Test Swift project with bridge
and import statements.
Now, there is function/method in an objective C file getValue
which is used/accessed in swift project/file.
Similarly, you can import as many files (source headers) as you want in bridge and use the same files (source code) in swift.
I have never tried to use objective-c from swift project. But I normally used swift classes from my objective-c project. I usually follow this instructions https://developer.apple.com/library/content/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html from apple developer website.

Integrating Microsoft Cognitive SpeechSDK framework into a Swift app

I'm trying to integrate Microsoft Bing Speech API with SpeechRecognitionService into my Swift application. Unfortunately, the Microsoft SDK only supports Objective-C atm, so I get around by adding #import "SpeechRecognitionService.h" to the Bridging Header after importing the SpeechSDK.framework, but I got the file not found error.
What am I doing wrong?
EDIT:
I did try import SpeechSDK framework directly into the needed class before but it was not working.
In my case, I'm still using the Bridging Header in order to import the framework. #import "SpeechRecognitionService.h" didn't work but a slight change as below works for me.
#import "SpeechSDK/SpeechRecognitionService.h"
There is no need to add header to bridging header, you can simply import the framework. From apple docs:
Importing External Frameworks
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”.
You can import a framework into any Swift file within a different
target using the following syntax:
import FrameworkName
See also “file not found” in Bridging Header when importing Objective-C frameworks into Swift project by CocoaPod
In my case, I'm still using the Bridging Header in order to import the framework. #import "SpeechRecognitionService.h" didn't work but a slight change as below works for me.
#import "SpeechSDK/SpeechRecognitionService.h"

Building reusable libraries with Swift that use bridging headers

I'm trying to get the hang of Swift, and am beginning by just doing a dumb port of a few applications I've written.
These applications have some core logic in common, for which I've used a Framework target in Xcode to share this with those projects. I'm having trouble coming up with an equivalent in Swift.
I know Swift compiles down to modules, which seems like what I want. I want a Swift module that I can share with my other projects. the major problem I seem to be having though, is that you cannot have a Framework with Swift if it also uses a bridging header starting in Beta 4, which I need to call some APIs (like Security.framework) that don't have Swift bindings. The compiler (Beta 5) fails with this error message:
<unknown>:0: error: using bridging headers with framework targets is unsupported
What can I do to create a reusable Swift module that also needs to use bridging headers? Alternatively, how can I use things in Security.framework without a bridging header? (Alternatively Aternatively, is there something other than a Framework I should be using to create a module that doesn't have any of these problems?)
To import Objective-C code to swift within the same framework target, just import each Objective-C header file in the umbrella header file. Apple's official document has mentioned that already: https://developer.apple.com/library/ios/documentation/swift/conceptual/buildingcocoaapps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-XID_81 see the Importing Code from Within the Same Framework Target part.
The cocoa built in frameworks have been migrated as modules in swift. To use the Objective-C's Security.framework, you just need to add a line:
import Security
at the header of the swift file.

Objective-C Bridging Header for frameworks

I've made a framework that requires the sqlite3 framework. How do I add a Objective-C Bridging Header for my framework that imports sqlite3 into my Swift file?
I already have a bridging header file for my project, but not for my framework.
I found a Objective-C Bridging Header setting in the target Build Settings. It was hidden by default. Check All instead of Basic.
In recent Xcode versions this solution would give the error Using bridging headers with framework targets is unsupported.
The workaround I've been using is to make the C-header public in the file inspector and import it in MyFramework.h like this example:
#import <MyFramework/MyObjectiveC.h>
How to change the C-header to public
Open your C-header and view the inspector by clicking in the upper right corner. To view the file inspector, click the file icon in the upper right corner.
just import your sqlite3 framework in your objective-c bridging file. You then automatically can use it in Swift.
Apple Docs:
Interoperability is the ability to interface between Swift and Objective-C in either direction, letting you access and use pieces of code written in one language in a file of the other language. As you begin to integrate Swift into your app development workflow, it’s a good idea to understand how you can leverage interoperability to redefine, improve, and enhance the way you write Cocoa apps.
One important aspect of interoperability is that it lets you work with Objective-C APIs when writing Swift code. After you import an Objective-C framework, you can instantiate classes from it and interact with them using native Swift syntax.
EDIT: You even can import an Objective-C framework or Swift framework or a mixed-language framework just into your swift file with import yourFramework
Apple Docs:
Importing External Frameworks
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.
You can import a framework into any Swift file within a different
target using the following syntax:
SWIFT
import FrameworkName
You can import a framework into any Objective-C .m file within a
different target using the following syntax:
#import FrameworkName;

How does importing within Swift work?

If I want to use Quartz and I type
import QuartzCore
for some reason, it works. If I type anything else, it doesn't work. When I check the documentation (Command click), it is a blank header file. How do I import headers?
As per the docs, import works for any Objective-C framework (or C library) that is accessible as a module.Objective-C frameworks vend APIs in header files. In Swift, those header files are compiled down to Objective-C modules, which are then imported into Swift as Swift APIs.