One day you’ll need to set up, let’s say, a mobile analytics system or a crash reports sender in your app. They are all useful, but some of them require you to import their framework into your Xcode project. Problem is, they’re now all written not using Swift Programming Language. In this little article I’ll tell you how to properly import a framework into your project.
I’ll show how to do it by importing a PLCrashReporter framework into an iOS application, but the process is absolutely the same for a Mac app and/or any other framework, for example Crashlytics, or any framework for a mobile analytics system.
Drag and drop the framework into your Xcode project. Check Copy items if needed.
Now we need to let Xcode know where to search for framework’s headers.
Select your project in the Project Navigator, then select your target in Targets menu.
Select Build Settings tab on the top.
Locate Header Search Paths (you can use search field on the top of course) and double click on its value.
Add a new line here by clicking plus (+) button.
Now you need to write here the path to the new framework’s headers.
For example, if my CrashReporter.framework is located in the same directory in which .xcodeproj file of my project is located, so I write this:
$(PROJECT_DIR) keyword is a nice thing that obviously represents project’s directory. It’s not recommended to write a full path to it (for example,
Users/username/Documents/ProjectName), because there will be problems if you try to move the project or send it to someone.
And yes, you can double click on the framework to see its contents. Don’t worry if Headers folder is an alias. You shouldn’t care.
Add a Bridging Header.
We need to tell Swift that here’s some stuff which isn’t written in Swift so it’ll see it.
Select File — New — File… or just press ⌘N, select Source on the left (doesn’t matter if it’s under OS X or iOS) and select Header File. Name the file
"Projectname" is your project’s name obviously. For example, if I’d want to create a bridging header in my Timerange app, I’d write
Go to this new header and import all headers from the framework that you’ll use. If it’s a crash reporter or mobile analytics system framework, then there’s probably one main header that you should import. For example, in
PLCrashReporter framework there’s one main header, which is
CrashReporter.h, so I only import this one:
Last thing we need to do here is to tell Xcode about our new bridging header.
Go to Build Settings we visited before, locate Objective-C Bridging Header line and double click on it’s value.
My bridging header is located in project directory, so I write
That’s it! Now Swift will see new classes and functions, and autocompletion will work just fine. If not, build the project by pressing ⌘B, then try again.
Notice how Xcode automatically translates the syntax of framework’s language into Swift language. Sweet!