Set Up Parse-Server with Swift 3

To start off I have only just started using Parse-Server having never used (or really heard of) Parse.com before in my life. I do, however, have experience with NodeJS and MongoDB. I am aiming this post at people who just want to grab some data from a locally hosted Parse-Server, however this should also work with Parse-Server hosted anywhere as long as you replace the URL’s correctly.

Okay onto the actual setup, head over to https://github.com/parse-community/parse-server and follow the instructions to install Parse onto your system. This should be fairly straightforward as long as you have NodeJS and MongoDB installed, if you don’t then head over to https://nodejs.org/en/download/ and https://www.mongodb.com/download-center and follow their instructions to install the software for your machine. Once that is all done you’re going to want to run the parse-server command “parse-server — appId APP_ID — masterKey MASTER_KEY — databaseURI mongodb://localhost/test” as far as I can tell APP_ID, MASTER_KEY and the /test bit of mongodb://localhost/test can be whatever you want. Run that command by hitting enter and you should have Parse-Server up and running, congratulations!

You may also want to install Parse-Dashboard, I’ll assume you do want to use it and honestly I don’t know why you wouldn’t, it makes life a lot easier. To do that head over to https://github.com/parse-community/parse-dashboard and follow the instructions over there, it’s pretty easy to set up just make sure your — appId, — masterKey and — serverURL (your — serverURL get’s output as part of your parse-server command) match up to your Parse-Server, you can set — appName to whatever you want. Once you run that head over to the url you’re given by the command (mine was http://0.0.0.0:4040/). You should see an interface that looks like this:

Parse-Dashboard startup page

Click on whatever your app name is and click the “Create a Class” button on the left hand side. We’re going to create a class called TestClass to hold some TestObjects. Fill out the popup as in the image below and click “Create Class”

Creating a new class

Next click “Add a new column” which shows up at the top of your screen and call it “object_name” as shown below.

Creating a new column

You can now click “Add a row”, double click the “object_name” field and call it whatever you want, I went with “obj1”. You should end up with something like what’s shown below.

The class with an object

On to Swift next.


For the Swift part we’re going to install everything with CocoaPods, in case you haven’t used them before go to https://cocoapods.org and scroll down to Install, you’ll need to copy the command and run it in terminal, you should then have CocoaPods installed.

Next you’ll want to create a new Xcode project, it’s going to be a Single View Application so select that under iOS and click next.

Create a single view application

Name the project whatever you want and create it, then quit Xcode and open your Xcode project in terminal. Running “ls” should show something like what you see below.

Running ls should give a similar output

Next run “pod init” to create a PodFile, running “ls” again should show you that a new file called PodFile has been created.

PodFile should show up

You can now go into whatever text editor you want and add “ pod ‘Parse’ ” to your PodFile like so:

PodFile with Parse added using nano

Save the file and run “pod install”. Congratulations, you’ve just added Parse to your project. From now on we need to use the .xcworkspace file so find and open that in Xcode.

Expand the folders named the same as your app and open the storyboard, so you have this:

The storyboard file

Add a label and connect it to your view controller, I’ve called mine “textLabel” then head over to AppDelegate.swift and at the top, under “import UIKit”, add:

import Parse

import Bolts

if you get an error you may need to build the project using Command+B. Under the application didFinishLaunchingWithOptions bit add the following code:

let config = ParseClientConfiguration {
 $0.applicationId = “APP_ID”
 $0.server = “
http://localhost:1337/parse"
 }
 Parse.initialize(with: config)

“$0.applicationId” should contain your appId and “$0.server” should contain the address to your Parse-Server server.

Initialised Parse

This will tell your app where to look for your data.

Next you’ll need to move over to your ViewController.swift file and again import Parse and Bolts like earlier.

In the “ViewDidLoad()” function create a new query by typing:

let query = PFQuery(className: “TestClass”)

We can execute the query by typing

query.findObjectsInBackground(block: {

(objectArr, err) -> Void in

})

“objectArr” contains every object in “TestClass”, for this tutorial we’re only going to grab the first one and output it’s object_name value to the label we created earlier. To do this we’ll create a variable to hold the object_name we’ve retrieved from our server. Add the following after “(objectArr, err) -> Void in”:

let obj_name = objectArr?[0].value(forKey: “object_name”) as! String

We’ll then set the text label’s text we created earlier to that String by typing:

self.textLabel.text = obj_name;

Your ViewDidLoad() function should now contain the following code:

let query = PFQuery(className: “TestClass”)

query.findObjectsInBackground(block: {

(objectArr, err) -> Void in

let obj_name = objectArr?[0].value(forKey: “object_name”) as! String

self.textLabel.text = obj_name;

})

You can now run the app to see if it works, if you get an error like the following:

[Error]: The resource could not be loaded because the App Transport Security policy requires the use of a secure connection. (Code: 100, Version: 1.14.4)

then you’ll need to go into info.plist and under “Information Property List” add “App Transport Security Settings” and then under that add “Allow Arbitrary Loads” and set it to YES.

Allowing Arbitrary Loads in info.plist

And that’s it, as you can see I get “obj1” as my label text which is exactly what I entered into Parse-Dashboard.

The App works

You can use the principles from this post to do loads with Parse. If you’d like me to write more about Parse as I learn it then let me know.

Thanks for reading!

One clap, two clap, three clap, forty?

By clapping more or less, you can signal to us which stories really stand out.