External Obj-C libraries used for your Swift project

Become a Subscriber

I was recently working on a Swift project when I came in the need for encryption. As always I love simple and easy to use libraries so I hopped over to Clay’s encryption library which you can check out here.

This was my first time using an external library for a Swift project and it got infuriating quite quickly. After a few hours I finally figured out how to get XCode to identify the right files so I could access the Encrypt and Decrypt functions.

After you’ve downloaded your library, I like to rename the folder the files live in as a simple naming convention. For this project I used “Crypto”. Now here is a quick fun fact on xcode. When you create a new group you are not technically creating a folder, just a grouping mechanism to make the files in XCode more organized. When you look in finder you will find all your files under the same project folder and none of the grouping folders exist.

For the Crypto what I did instead was drag and drop the entire folder from Finder to a group I had called “Library.” This imported the entire as a real folder for the XCode project. This is important to remember.

Now after having imported the files we have to bridge the Obj-C language with the Swift language. In order to do that we need a file called “Bridging-Header.h” Inside of that file I have the header file I need from the library to mix and mingle the Obj-C and Swift functions.

#import "Crypto.h"

So then I jumped to a swift file to access the Encrypt and Decrypt function when XCode kept throwing

Use of unresolved identified

That’s strange, I thought I had bridged the header file and should now have access to those functions. Turns out we still need to do two more things.

Click on your project in XCode and click the “Build Settings” tab.

Inside of there make sure the “All” & “Combined” options are selected and inside of the search bar do a search for: header search

You should see “Header Search Paths.” Double Click the right side of the row and here we need to type in:

$(PROJECTDIR)/directoryofproject/folderimported

In my case it was $(PROJECT_DIR)/Flourish-Teaching/Crypto

Great! Almost there. Now we need xcode to identify the bridging-header so in the search bar this time input: swift compiler

You should see an “Objective-C Bridging Header” and again double click the right side of that row and type in wherever your bridging-header exists:

$(PROJECTDIR)/directoryof_project/Bridging-Header.h

In my case it was $(PROJECT_DIR)/Flourish-Teaching/Bridging-Header.h

And that’s it! Those errors should go away and you now have access to your library’s functions.

Note: You may end up with some build issues around undefined symbols and crazy jargon, for such errors refer to this page