Parse is a platform that offers many tools and one of the things it provides is a “service as a back-end”. Parse takes care of the implementation of the back-end so that developers can focus on building their apps while still leveraging the power of having data persistence in the cloud.
Long time reader, Lukas von Mateffy, shares this tutorial with you guys on how to use Parse in Swift.
When Apple announced Swift, they told us that their new programming language will still support Objective-C Code. Than means, we can still use the popular Frameworks like Parse etc.! If you don’t know what Parse is, check it out HERE.
For the Usage of Parse Features, Chris has already made a Tutorial in his Course.
Since the Methods are the same you can still work with the Methods you used before and you only have to change the Syntax.
In this Tutorial I will only cover how to import Parse Framework into your Swift Project and some Basics in how to use it. Alright, let’s get started!
Step 1: Setup Parse Backed
First of all you have to register a new Account at Parse.com.
Then it will prompt you to create a new Application. If you already have an Account, then you have to create one by yourself.
Enter the name of your Application and click “Create app”.
After that step, it will show you a List of your Application Keys. Keep these safe! With them you will access your Parse Data etc.! For the iOS App you will need the Application Key and the Client Key.
Now go to the Downloads Section in your Parse Backend. Then download the Parse iOS SDK.
Step 2: Setup Xcode Project
Create a new Xcode Project and fill in your Information. It’s important that you select Swift as the programming language.
Once the Application is created, click on your Project and go to “Build Phrases”.
In the List “Link Binary With Libraries” you will have to add these Frameworks to use Parse.
AudioToolbox.framework
CFNetwork.framework
CoreGraphics.framework
CoreLocation.framework
libz.dylib
MobileCoreServices.framework
QuartzCore.framework
Security.framework
StoreKit.framework
SystemConfiguration.framework
libsqlite3.dylib
Then you can simply drag the Parse.framework you downloaded before into your Project. It’s important to check “Copy Items if needed” when you drag over the File.
Step 3: Import Parse into Swift
Create a new File (File -> New -> File) of type Objective-C File.
Name it whatever you want, we’ll just call it “CustomClass”. It will prompt you if you would like to configure an Obj-C Bridging Header. Click “Yes.
Now it will add two new Files to your Project. First the .m File with your the Name you specified for your class. Delete this File. The second File is called YourProjectName-Bridging-Header.h. and this File is our Key to success.
Add this code to the YourProjectName-Bridging-Header.h:
#import
Step 4: Initialize Parse
Go into your AppDelegate.swift File and add this Code to your applicationDidFinishWithLaunching Method:
Parse.setApplicationId(“your_application_key”, clientKey: “your_client_key”)
Replace “your_application_key” and “your_client_key” with your own Keys and now you’re ready to use Parse.
To proof that it actually works, add this code to the same method and run the app.
var object = PFObject(className: "TestClass")
object.addObject("Banana", forKey: "favoriteFood")
object.addObject("Chocolate", forKey: "favoriteIceCream")
object.saveInBackground()
If you then take a look at the Data Browser in your Parse Backend, you will see some new Objects added!
And that’s how you use Parse in Swift.
Lukas is a 14 year old Web and App Developer. Being a long time reader, he now wants to share his knowledge about developing apps for the iPhone, iPad and iPod Touch.
Thanks 🙂
Hi Chris,
Very helpful– any chance you have something in the works on Parse logins with Swift?
Best,
Santosh
Hey Santosh, not at the moment but given the popularity of it, i’m going to try to get to this next week. Thanks!
Hello Chris! Thank you for the review. It’s very easy to follow but i got a problem when i tried to compile it.
I got this error message on linker things. I already tried to add Bolts framework to the project but it doesn’t work.
I got Parse ver 1.6.0 btw.
Sorry Juntima, i think i’ll have to update this tutorial!!
I tried add this lib: libsqlite3.dylib
and it’s work now 😀
Thank you! This worked for me too 🙂
Thanks for the big effort. This inclusion of libsqlite3 fixed the problem faced above. Keep up the good work.
I got this running. Looking at the parse dasboard I see the keys created as ‘Array’. How can you set a key to ‘string’ type?
I don’t get the dialog to create bridging header file. Only the CustomClass.m is created. I’m using Xcode 6.1.1 What am I doing wrong?
I already had a previous bridging header defined for GoogleCast. I deleted that and it’s working now.
May I know where to get this framework? After I downloaded parse SDK, it only contain bolts, parse, parsefacebookutills, parseui framework only. Thank you.
AudioToolbox.framework
CFNetwork.framework
CoreGraphics.framework
CoreLocation.framework
libz.dylib
MobileCoreServices.framework
QuartzCore.framework
Security.framework
StoreKit.framework
SystemConfiguration.framework
Hey can you recommend a good resource for learning developing with parse and swift more in depth? I am trying to use the tutorial videos on parse’s website but it is in objective c and I am having trouble converting syntax. Specifically I was trying to implement simple login with username and password and I just keep getting a blank screen. Any tips or resources?
Thanks!
Hey Max, not that I know of right now. But I’ll be creating tutorials on this in Dec/Jan. Sorry for the wait and thanks for reading!
No problem. I realized that the parse docs have both objective c and swift code snippets so it’s been fairly straightforward, thanks!
+1 to this!
Hi Chris, I try to do this example but with the new version I don’t have the option saveInBackground. I have another options:
– saveInBackgroundWithTarget(, selector: )
– saveInBackgroundWithBlock( Void#>)
I don’t know how to use this two options.
Thanks
Using testObject.save() Works!
Using testObject.save() will work though your app will be very slow since it waits for this action to be taken rather that taking care of it asynchronously.
The best way would be to use this testObject.saveInBackgroundWithTarget(nil, selector: nil)
Thanks
Hi, what does it mean for saveInBackground() vas saveInBackgroundWithTarget(nil, selector: nil). Thank you.
hi, apear me this error or what this is … how can i fix it ?
hi, appear me this error or what is that thing … how can i fix it? ://Users/JoaoFernandes/Desktop/Captura de ecrã 2014-10-24, à s 00.51.05.png
for downloading the file?
how can I redownload the file?
thanks!
Loved this, thanks!
After this how would you go about creating a Facebook log in?
when you use .addObject forKey: it adds the values as arrays rather than their real types. You should just use object[“favoriteFood”] = “banana” instead
Im new to iphone dev. I went through the tutorial it all compiles correctly I never see the data show on the parse side of the deal. Can’t figure out what im doing wrong since it compiles!! Any pointers would be greatly appreciated!!
same here :/
I get these errors when running:
Undefined symbols for architecture x86_64:
“_OBJC_CLASS_$_BFExecutor”, referenced from:
objc-class-ref in Parse(BFTask+Private.o)
l_OBJC_$_CATEGORY_BFExecutor_$_Background in Parse(BFTask+Private.o)
“_OBJC_CLASS_$_BFTask”, referenced from:
objc-class-ref in Parse(PFObject.o)
objc-class-ref in Parse(PFFile.o)
objc-class-ref in Parse(PFNetworkCommandRunner.o)
objc-class-ref in Parse(PFQuery.o)
objc-class-ref in Parse(PFRetryingCommandRunner.o)
objc-class-ref in Parse(PFTaskQueue.o)
objc-class-ref in Parse(PFUser.o)
…
“_OBJC_CLASS_$_BFTaskCompletionSource”, referenced from:
objc-class-ref in Parse(PFObject.o)
objc-class-ref in Parse(PFCommandCache.o)
objc-class-ref in Parse(PFFile.o)
objc-class-ref in Parse(PFNetworkCommandRunner.o)
objc-class-ref in Parse(BFTask+Private.o)
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)
Adding Bolts.framework solved this for me. It’s in the same package as the Parse SDK.
That’s great. Thank you 🙂
One small note… when I cut and paste the Parse.SetApplicationId line, it copied the italicised quotes which caused the code to fail to be parsed.
I had the change the quote like this “ to a quote like this ”
The difference is subtle and hard to spot.
🙂
thank you!
At Step 4 there’s some Code that isn’t in a code box (syntax highlighting etc.).
But thanks Chris for publishing this Tutorial! 😀
Fixed. Thanks Lukas!