Connect Objective-C framework to Swift iOS 8 app (Parse framework)

user284244 picture user284244 · Jun 17, 2014 · Viewed 45.4k times · Source

I am trying to use an Objective-C framework with the Swift programming language for iOS 8 development. This is a specific case of an import but the general problem is:

How do you import an Objective-C framework into swift and get the import recognized?

I am trying to integrate the Parse framework into a swift application using the iOS 8 and Xcode 6 betas.

Here is the technique for Parse framework integration in Objective-C:

https://www.parse.com/apps/quickstart#social/mobile/ios/native/existing

I have downloaded the Parse framework as a compressed archive, extracted it, and imported it into Xcode 6 without any problems. Within my application it appears as a properly formatted framework under the name Parse.framework.

My current thought process is to modify the AppDelegate.swift file in the root directory of my project. Here is the current file without modifications (automatically generated by Xcode upon swift project creation):

https://gist.github.com/fconcklin/e8ef7d8b056105a04161

I have tried to import parse by adding the line import Parse below the line import UIKit. However, Xcode issues a warning that there is no such module found and the build fails.

I also tried creating a file ${PROJ_NAME_HERE}-Bridging-Header.h that contains the Objective-C import of Parse using import <Parse/Parse.h>. This line doesn't throw an error but appears to ultimately make no difference.

Answer

user284244 picture user284244 · Jun 17, 2014

After further research I found the solution and realized that I was just confused.

The correct approach is as follows:

  • Import your Objective C framework by dragging and dropping the framework into an Xcode 6 Swift project.

  • Create a new Objective C file in your project (File->New->File [Objective C for iOS]).

  • Accept the prompt (agree) to create a bridging header file between Objective C and Swift.

  • Delete your newly created Objective C file but retain the bridging header file ${YOURPROJ}-Bridging-Header.h.

  • In the Bridging header file, import your framework using the standard Objective C import syntax (e.g. #import <Parse/Parse.h>).

  • This relinquishes the need to perform an import Parse statement in your AppDelegate.swift file. You can now write code that utilizes whatever framework as long as it is imported using the bridging header. It is available throughout your project's Swift files.

Now, if you would like to test Parse integration in your project, you can type Parse. and use code completion to browse the framework and see that the code completion is indicative of a successful import.

However, there is another caveat here that needs to be addressed when using a Bridging Header file. All dependencies of the framework need to be specified in the Bridging Header file as well. In the case of integrating Parse framework into a Swift application your Bridging Header file will look like this:

 #import <Foundation/Foundation.h>

 // Parse Dependencies
 #import <AudioToolbox/AudioToolbox.h>
 #import <CFNetwork/CFNetwork.h>
 #import <CoreGraphics/CoreGraphics.h>
 #import <CoreLocation/CoreLocation.h>
 #import <MobileCoreServices/MobileCoreServices.h>
 #import <QuartzCore/QuartzCore.h>
 #import <Security/Security.h>
 #import <StoreKit/StoreKit.h>
 #import <SystemConfiguration/SystemConfiguration.h>

 // Import parse framework
 #import <Parse/Parse.h>

Hope this helps.