Show Menu

Looking to hire an app developer?

Submit your 30 day Job Listing for FREE

facebook logo

So you want to use an Objective-C Library or SDK in your Swift application eh? Well do not fret. This quick tutorial will show You how you can create an Objective-C Bridging Header in your Swift application so you can use them both together and seamlessly.

So, I had a request to show people how to connect Objective-C Libraries/Frameworks etc.. in a Swift Application and also what the differences are between Libraries/Frameworks.

In this example I will show you how to connect the Facebook iOS SDK which is Objective-C to your Swift application using an Objective-C Bridging Header.

What are the differences?

There are many differences between a Library, SDK, API, Toolkit etc. I found a really good answer on StackOverflow that sums them up very nicely:

A Library is a section or chunk of Objective-C or Swift code that you make a call from your own code, to help you do things more quickly/easily. For example, a Bitmap Processing library will provide facilities for loading and manipulating bitmap images, saving you having to write all that code for yourself.

AnAPI (application programming interface) is a term meaning the functions/methods in a library that you can call to ask it to do things for you – the interface to the library.

An SDK (software development kit) is a library (often with extra tool applications, data files and sample code) that aid you in developing code that uses a particular system (e.g. extension code for using features of an operating system (Windows SDK), drawing 3D graphics via a particular system (DirectX SDK), writing add-ins to extend other applications (Office SDK), or writing code to make a device like an Arduino or a mobile phone do what you want)

A toolkit is like an SDK – it’s a group of tools (and often code libraries) that you can use to make it easier to access a device or system.

A framework is usually a huge library that provides many services (rather than perhaps only one focussed ability as most libraries do). For example, Facebook provides an application framework – it provides most (if not all) of the services you need to write a vast range of applications – so one “library” provides support for pretty much everything you need to do. Often a framework supplies a base on which you build your own code, rather than you building an application that consumes library code.

To create our Objective-C bridging header in Swift I’m going to be using the Facebook SDK. This means that I will be adding a framework from the SDK into the project instead of dragging an Objective-C Library to the Project Navigation Area.

Get the Facebook iOS SDK

Head over to the Facebook Developers Resources area and download the latest version of their iOS SDK. This will download a package file .pkg

This will then start an installation. Follow this through until you see the complete and final stage. Like so:

Facebook iOS pkg Installation

Add Facebook Framework to your project

The next step is to make sure that we add the required framework to our project. For this you need to go to the General settings tab and scroll down until you see Linked Frameworks and Libraries:

swift connect libraries and frameworks

Click the + button to add a new library and in the search look for: FacebookSDK.Framework. If you can not find the framework using the search you may have to click the Add Other button and search in your folders. Check yourDocuments folder first.

Normally in a project you would use:


#import 

However Even though you have just added the Facebook iOS SDK Framework to your project you still cannot use it as its Objective-C. This is where the Swift / Objective-C Bridging Header comes in.

So how do you create an Objective-C Bridging header? It’s pretty simple.

There are two ways to create an Objective-C Bridging Header. The first one is to create a new Objective-C File and specify that it is used a header file. This will then create two files: .h and .m or Just create the Header File on its own.

1. Create Objective-C File as Header

Go to File > New > File and Select Objective-C:

Swift Create Objective C Bridging Header

Click the Next button. You will be then asked to name this new file. You can name it whatever you like. I have named mine: Objective-CBridgingHeader. You will then be asked to chose a location for the filed to be saved. Usually Xcode will detect the correct folder but double check and then click save. You will then be presented with another Dialogue that says: Would you like to configure an Objective-C bridging header?:

create objective c bridging header question

Click Yes. This will now create two files for you.

2. Create Objective-C Header File Directly

Go to File > New > File and Select Header File:

Xcode Choose Header File

Just like the above example, Chose a name and save it. This will then create Just one file: Objective-CBridgingHeader.h

Now, depending on what ever example you have chosen open the .h file and add the following line:


#import 

Ok, so you have now created your Objective-C Bridging Header File in Swift, YAY.

I hope you found this useful.

having issues?

We have a Questions and Answer section where you can ask your iOS Development questions to thousands of iOS Developers.

Ask Question

FREE Download!

Get your FREE Swift 2 Cheat Sheet and quick reference guide PDF download when you sign up to SwiftMonthly


Sharing is caring

If you enjoyed this tutorial, please help us and others by sharing using one of the social media buttons below.


iOS-Blog Admin Team

Written by:

We're here to help.

Comments

comments