Tutorial: Using Parse.com in a Swift Project

Swift is Apple’s new LLVM-based programming language.

Parse offers a toolset for server-side storage and processing that lets you build apps that need a back end, without having to build that back end yourself. (It covers much of the same ground as CloudKit, but has features that CloudKit does not at this time – but that’s a topic for another blog post.)

I’m looking at using Parse for a personal project. Also, I want to dive into all the new iOS 8 hotness, including Swift. So, the question came up: Can I make Parse play nice with an all-Swift greenfield project?

Unsurprisingly, the answer is a straightfoward “yes” – Parse has been very much on the ball with converting much of their iOS example code to Swift. You do have to jump through a couple of procedural hoops, but it’s nothing too taxing. What follows is a step-by-step tutorial for getting Parse working in your brand-new Swift project for iOS 8. It assumes that you know your way around Xcode, and that you know how to create new classes, link a framework in the project build phases, etc.

Step 1: Set Up Your App on Parse.com

Parse makes this pretty straightfoward. First, name your app:

Create your application in Parse by giving it a name

Create your application in Parse by giving it a name

Then, get your keys:

Get the authentication keys for your Parse app

Get the authentication keys for your Parse app

Drill down through the Quick Start Guide options and choose the Quick Start guide for a Data -> Mobile -> iOS -> Objective-C project. Keep that webpage open for the moment.

Step 2: Create Your iOS 8 Project with Swift

In Xcode 6 (as of this writing, you’ll need to be in Apple’s iOS Developer program to download the beta), create a new project. For this tutorial, just make it a Single View project, and be sure to select Swift as your language of choice:

Select Swift as your language when creating your application

Select Swift as your language when creating your application

Step 3: Set up the Parse SDK

From the Quick Start page you left open, download the SDK:

Download the Parse SDK

Look for this button to download the Parse SDK

From the downloaded .zip file, drag the Parse framework into your project:

Add the Parse framework to your project.

Add the Parse framework to your project.

Go to your target’s Build Phases and verify that the Parse framework has been added to the Link Binary with Libraries section:

Link the Parse framework into your binary

Link the Parse framework into your binary

While you’re at it, link the following libraries as well:

  • AudioToolbox.framework
  • CFNetwork.framework
  • CoreGraphics.framework
  • CoreLocation.framework
  • libz.dylib
  • MobileCoreServices.framework
  • QuartzCore.framework
  • Security.framework
  • StoreKit.framework
  • SystemConfiguration.framework

Now you have everything the Parse SDK needs to operate. Just to make sure, go ahead and build your project. It won’t do anything yet, but that’s expected, as long as it builds and runs.

Step 4: Create a Bridging Header

There’s one bit of magic you need in order to get your Swift files to consume code written in Objective-C, and that’s a bridging header. In this header, you will import all the Objective-C headers for classes that you want your Swift code to know about.

While it’s possible to manually create a bridging header and set up the project to use it, there’s a faster way: Create a dummy Objective-C object file (e.g., Dummy.m) in your project. Xcode will ask you whether you want to create a bridging header:

Xcode will ask you whether you want to create a bridging header. Say "yes".

Xcode will ask you whether you want to create a bridging header. Say “yes”.

Naturally, you should say, “yes”, and afterward your project should look something like this:

The project with the bridging header

The project with the bridging header

(There have been settings changes in the project file, as well, to get Swift to look to this new header for information.)

Delete Dummy.m – it has served its purpose. Add #import <Parse/Parse.h> to your bridging header file:

This is another good stopping point to build and run your app. It still won’t do anything, but if it builds and runs you probably haven’t introduced any problems.

Step 5: Adding Some Actual Code!

The boilerplate code provided with the Parse iOS Quick Start is all in Objective-C, and will need a little attention to convert it to Swift – but not too much. For example, when you see this:

…you’ll need to covert it to this:

If you want to convert all of Swift’s starter project by hand, I invite you to do so (it’s a nice learning experience, if you’re still coming up to speed on Swift), but for those who want to get straight to the fun part, you should make your application:didFinishLaunchingWithOptions: look something like this:

Note that this skips over setting up push notifications – I’m just setting it up to demonstrate access to Parse data services.

In your view controller, make your viewDidLoad look like this:

Now build and run. You won’t see anything happen, but there’s one more step…

Step 6: Verify

Head back to the browser tab with the Parse iOS Quick Start page, and click the Data Browser link from the menu at the head of the page:

Parse Data Browser link

Parse Data Browser link

Be aware that it might take a minute or two to update, but you should see the following in the left column of the data browser:

Data has been uploaded successfully

Data has been uploaded successfully

Further, if you drill into the test object table, you should see your custom field set:

Your custom value, on the server

Your custom value, on the server

If you see this, you’ve done it – Parse is now working in your new Swift app! Go get yourself a scoop of ice cream or something to celebrate.

Review

In this tutorial you have:

  1. Created a Parse app,
  2. Created a Swift project for iOS 8,
  3. Added the Parse framework (and its supporting frameworks) to your project,
  4. Created the bridging header that Swift needs to talk to Parse’s framework,
  5. Written some minimal code to create an anonymous user and store a custom data object, and
  6. Verified that your user and custom data object were created on the back end.

To move on from here, I’m going to refer you to Parse’s iOS documentation, which has all the Swift code samples you’ll need to start writing some non-trivial uses of Parse into your app. Write something cool!

Update

James Paolantonio tells me that he had difficulty with this procedure when applying it to an existing project using CocoaPods, in which case the build failed and started demanding the Facebook SDK and other stuff. There have been issues using CocoaPods with Xcode 6 projects, but there are workarounds.

In this case, adding 'Parse-iOS-SDK', '~> 1.2.19' to the Podfile seems to clear up the issue. Thanks, James, for that heads-up.

  12 comments for “Tutorial: Using Parse.com in a Swift Project

  1. Henry
    June 24, 2014 at 09:16

    AWESOME!! Thanks for the tutorial! Can you please also teach us how to implement Facebook login into apps in swift? I am new to iOS development so you’ll help me tremendously 🙂

  2. June 24, 2014 at 09:33

    Henry – I’ll be posting more tutorials as I explore Swift, but for something specific like that you’re better off checking out Facebook’s SDK documentation.

  3. Oliver
    July 2, 2014 at 13:09

    Thanks. Great post.

  4. Max
    October 7, 2014 at 04:12

    Awesome, thanks. Helped me a lot!

  5. Lawrence
    October 10, 2014 at 03:05

    I can do all of those, but there are errors if I try to subclass PFObject in Swift. Any hints?

  6. October 10, 2014 at 09:34

    Lawrence – Have you checked out the PFObject docs? https://parse.com/docs/ios/api/Classes/PFObject.html

    Also, don’t forget to call YourClass.registerSubclass() when the app starts up.

    Ping me on Twitter (@bradheintz) if you’re still stuck on this.

  7. Fabian
    November 13, 2014 at 04:32

    I always get a error at var defaultACL = PFACL.ACL() also testObject.saveInBackground() does not work any hint on that :(?

    • Aron
      December 2, 2014 at 00:41

      Just make it PFACL() and use saveEventually()

  8. Dude
    January 14, 2015 at 23:45

    This is awesome really sweet post

  9. Dani Smith
    March 17, 2015 at 10:05

    Hello. I am getting a bunch of errors:

    Undefined symbols for architecture x86_64:
    “_BFTaskMultipleExceptionsException”, referenced from:
    ___53+[PFObject(Private) deleteAllAsync:withSessionToken:]_block_invoke214 in Parse(PFObject.o)
    ___65+[PFObject(Private) _deepSaveAsync:withCurrentUser:sessionToken:]_block_invoke311 in Parse(PFObject.o)
    “_OBJC_CLASS_$_BFExecutor”, referenced from:
    objc-class-ref in Parse(PFObject.o)
    objc-class-ref in Parse(PFUser.o)
    objc-class-ref in Parse(Parse.o)
    objc-class-ref in Parse(PFCommandCache.o)
    objc-class-ref in Parse(PFConfig.o)
    objc-class-ref in Parse(PFFile.o)
    objc-class-ref in Parse(PFPinningEventuallyQueue.o)

    “_OBJC_CLASS_$_BFTask”, referenced from:
    objc-class-ref in Parse(PFObject.o)
    objc-class-ref in Parse(PFUser.o)
    objc-class-ref in Parse(Parse.o)
    objc-class-ref in Parse(PFCommandCache.o)
    objc-class-ref in Parse(PFConfig.o)
    objc-class-ref in Parse(PFEventuallyPin.o)
    objc-class-ref in Parse(PFFile.o)

    “_OBJC_CLASS_$_BFTaskCompletionSource”, referenced from:
    objc-class-ref in Parse(PFObject.o)
    objc-class-ref in Parse(PFOfflineStore.o)
    objc-class-ref in Parse(PFEventuallyQueue.o)
    objc-class-ref in Parse(PFSQLiteDatabase.o)
    objc-class-ref in Parse(BFTask+Private.o)
    objc-class-ref in Parse(PFNetworkCommandRunner.o)
    objc-class-ref in Parse(PFTaskHTTPRequestOperation.o)

    ld: symbol(s) not found for architecture x86_64
    clang: error: linker command failed with exit code 1 (use -v to see invocation)

    Please Help!

    • March 19, 2015 at 10:06

      I’m not sure exactly what’s going on there, but if I were to hazard a guess, it looks like the Bolts framework isn’t being linked in. In your place, I’d go back and make sure that I correctly followed all the instructions to integrate Parse into the project, and/or reinstall the Parse CocoaPod.

Leave a Reply

Your email address will not be published. Required fields are marked *