iOS

Install Parse SDK on your iOS Swift Project

Introduction

In this section you will learn how to install Parse Swift iOS SDK into your Xcode project.

In this tutorial we will use a basic app created in Swift with Xcode 12 and iOS 14.

At any time, you can access the complete Project built with this tutorial at our GitHub repository.

Prerequisites

To complete this tutorial, you need:

  • An app created at Back4App.
  • Xcode.
  • Basic iOS app.
    • Note: If you don’t have a basic app created you can open Xcode and hit File-> New-> Project -> iOS. Then select App. After you create your basic app you are ready to follow this guide.

    Creating intial Xcode project

    Setting the project to single view app

Choose your Installation Method

Step 1 - Add the Parse Swift SDK Package - Swift Package Manager

Follow this step if you haven’t yet installed Parse iOS SDK.

Newer versions of XCode have the Swift Package Manager built in. This is the easiest and best way to install the Parse Swift SDK on your project and keep it updated.

Currently we only recommend using this method for installing the Parse Swift SDK.

Under the (File) menu, select (Swift Packages) and then (Add Package Dependency)

open terminal from project folder

On the (Choose Package Repository) window, paste the URL for the Parse Swift SDK github website ( https://github.com/parse-community/Parse-Swift ) and click Next

open terminal from project folder

On the (Repository) window, you can select a Version, Branch or specific Commit. Choose the method you prefer and click Next

open terminal from project folder

Wait for XCode to resolve all Parse-Swift dependencies and then click Next

open terminal from project folder

Check if ht package product ParseSwift is checked and your target is correctly selected on Add to Target, then click Next

open terminal from project folder

The Swift package should appear on the Dependencies tree right under your project, showing its version on the right side:

open terminal from project folder

If you need to update the ParseSwift package, right click it under the Dependencies tree and choose Update Package. The process will automatically update everything for you.

open terminal from project folder

Congratulations! You have now installed the Parse Swift iOS SDK

Step 2 - Connect your Parse App

  1. Open your project’s AppDelegate.swift file to set up the app’s credentials.
  2. Parse Swift iOS SDK uses these settings to connect to the Back4App servers.
  3. At the top of the file you should see a function called ‘didFinishLaunchingWithOptions’.
  4. Paste the following code snippet inside this function, and make sure it is above the line that says ‘return true’.

AppDelegate.swift

1
ParseSwift.initialize(applicationId: "PASTE_YOUR_APPLICATION_ID_HERE", clientKey: "PASTE_YOUR_CLIENT_ID_HERE", serverURL: URL(string: "https://parseapi.back4app.com")!)

At the top of your AppDelegate.swift file make sure to include Parse as a module by including the follwing code snippet right below ‘import UIKit’.

AppDelegate.swift

1
    import ParseSwift

Your AppDelegate.swift file should now look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import UIKit
import ParseSwift

@main
class AppDelegate: UIResponder, UIApplicationDelegate {



    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        ParseSwift.initialize(applicationId: "PASTE_YOUR_APPLICATION_ID_HERE", clientKey: "PASTE_YOUR_CLIENT_ID_HERE", serverURL: URL(string: "https://parseapi.back4app.com")!)
        return true
    }

    // MARK: UISceneSession Lifecycle

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }


}

Be careful, If Xcode tells you there is No Such Module ‘Parse’ there’s an easy solution. In Xcode open ‘Target > Build Settings > Search Paths > Framework Search Paths’ and then add two values: ‘$(PROJECT_DIR)’ and ‘$(inherited)’. Xcode will now be able to find your Parse module.

  1. Go to your App Dashboard at Back4App website.
  2. Navigate to app’s settings: Click on Features > Core Settings block > Server.
  3. Return to your AppDelegate.swift file and paste your applicationId and clientKey.

See more at our New Parse App guide.

Step 3 - Test your connection

Open your ViewController.swift file.

At the top of the file make sure to include Parse as a module by including the follwing code snippet right below ‘import UIKit’.

ViewController.swift

1
    import ParseSwift

Inside the function called ‘viewDidLoad’ add a snippet of code below the code that configures parse.

ViewController.swift

1
    testParseConnection()

Then add a function below viewDidLoad() method.

ViewController.swift

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
    struct GameScore: ParseObject {
        //: Those are required for Object
        var objectId: String?
        var createdAt: Date?
        var updatedAt: Date?
        var ACL: ParseACL?

        //: Your own properties.
        var score: Int = 0

        //: Custom initializer.
        init(score: Int) {
            self.score = score
        }

        init(objectId: String?) {
            self.objectId = objectId
        }
    }

    func testParseConnection(){
        let score = GameScore(score: 10)
        let score2 = GameScore(score: 3)
        score.save { result in
            switch result {
            case .success(let savedScore):
                assert(savedScore.objectId != nil)
                assert(savedScore.createdAt != nil)
                assert(savedScore.updatedAt != nil)
                assert(savedScore.ACL == nil)
                assert(savedScore.score == 10)

                /*: To modify, need to make it a var as the value type
                    was initialized as immutable.
                */
                var changedScore = savedScore
                changedScore.score = 200
                changedScore.save { result in
                    switch result {
                    case .success(var savedChangedScore):
                        assert(savedChangedScore.score == 200)
                        assert(savedScore.objectId == savedChangedScore.objectId)

                        /*: Note that savedChangedScore is mutable since it's
                            a var after success.
                        */
                        savedChangedScore.score = 500

                    case .failure(let error):
                        assertionFailure("Error saving: \(error)")
                    }
                }
            case .failure(let error):
                assertionFailure("Error saving: \(error)")
            }
        }
    }
}
  1. Build your app in a device or simulator (Command+R).
  2. Wait until the main screen appears.
  3. Login at Back4App Website.
  4. Find your app and click on Dashboard.
  5. Click on Core.
  6. Go to Browser.

If everything works properly, you should find a class named GameScore and the saved objects in it.

Next Steps

At this point, you have learned how to get started with iOS apps. You are now ready to explore Parse Server core features and Back4App add-ons.

Learn more by walking around our iOS Tutorials or check Parse open source documentation for iOS SDK.

Step 1 - Install the Parse Swift iOS SDK

Follow this step if you haven’t yet installed Parse Swift iOS SDK.

Xcode can use CocoaPods as dependency manager for Swift and Objective-C Cocoa projects.

You can refer to CocoaPods Getting Started Guide for additional details.

To install CocoaPods, open your terminal, copy the following code snippet and paste it in to your terminal and hit return:

$ sudo gem install cocoapods

CocoaPods should install automatically after you enter your password. If there’s a problem you may need to upgrade your local version of Ruby.

Next open up the Xcode Project folder and open a terminal window in that folder.

open terminal from project folder

Now you are going to create a Podfile. Copy the following code snippet and paste it in to your terminal and hit return:

$ pod init

If your folder now shows your Podfile you did it correctly.

project with podfile

Be careful, If you don’t see the podfile make sure your terminal is actually inside the project folder..

Next open your Podfile with Xcode or any text editor and under each target add “pod ‘Parse’”.

pod 'ParseSwift'

Your Podfile will look similar to this:

platform :ios, '14.0'

target 'Cocoapods_ParseSwift' do
  # Comment the next line if you don't want to use dynamic frameworks
  use_frameworks!

  # Pods for Cocoapods_ParseSwift
  pod 'ParseSwift'

end

Now you are going to add Parse Swift to your project. Make sure your terminal is opened to your project folder. Copy the following code snippet and paste it in to your terminal and hit return:

$ pod install

CocoaPods will rebuild the project as a workspace and your project will now look like this.

Workspace with Parse added

If you have already opened your Xcode project close it. From now on you’ll open the workspace file instead of the project file. Double click on the workspace file to open it.

Open Workspace file

Congratulations! You have now installed the Parse iOS SDK

Step 2 - Connect your Parse App

  1. Open your project’s AppDelegate.swift file to set up the app’s credentials.
  2. Parse iOS SDK uses these settings to connect to the Back4App servers.
  3. At the top of the file you should see a function called ‘didFinishLaunchingWithOptions’.
  4. Paste the following code snippet inside this function, and make sure it is above the line that says ‘return true’.

AppDelegate.swift

1
ParseSwift.initialize(applicationId: "PASTE_YOUR_APPLICATION_ID_HERE", clientKey: "PASTE_YOUR_CLIENT_ID_HERE", serverURL: URL(string: "https://parseapi.back4app.com")!)

At the top of your AppDelegate.swift file make sure to include Parse as a module by including the follwing code snippet right below ‘import UIKit’.

AppDelegate.swift

1
    import ParseSwift

Your AppDelegate.swift file should now look like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import UIKit
import ParseSwift

@main
class AppDelegate: UIResponder, UIApplicationDelegate {



    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
        // Override point for customization after application launch.
        ParseSwift.initialize(applicationId: "PASTE_YOUR_APPLICATION_ID_HERE", clientKey: "PASTE_YOUR_CLIENT_ID_HERE", serverURL: URL(string: "https://parseapi.back4app.com")!)
        return true
    }

    // MARK: UISceneSession Lifecycle

    func application(_ application: UIApplication, configurationForConnecting connectingSceneSession: UISceneSession, options: UIScene.ConnectionOptions) -> UISceneConfiguration {
        // Called when a new scene session is being created.
        // Use this method to select a configuration to create the new scene with.
        return UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    }

    func application(_ application: UIApplication, didDiscardSceneSessions sceneSessions: Set<UISceneSession>) {
        // Called when the user discards a scene session.
        // If any sessions were discarded while the application was not running, this will be called shortly after application:didFinishLaunchingWithOptions.
        // Use this method to release any resources that were specific to the discarded scenes, as they will not return.
    }


}

Be careful, If Xcode tells you there is No Such Module ‘Parse’ there’s an easy solution. In Xcode open ‘Target > Build Settings > Search Paths > Framework Search Paths’ and then add two values: ‘$(PROJECT_DIR)’ and ‘$(inherited)’. Xcode will now be able to find your Parse module.

  1. Go to your App Dashboard at Back4App website.
  2. Navigate to app’s settings: Click on Features > Core Settings block > Server.
  3. Return to your AppDelegate.swift file and paste your applicationId and clientKey.

See more at our New Parse App guide.

Step 3 - Test your connection

Open your ViewController.swift file.

At the top of the file make sure to include Parse as a module by including the follwing code snippet right below ‘import UIKit’.

ViewController.swift

1
    import ParseSwift

Inside the function called ‘viewDidLoad’ add a snippet of code below the code that configures parse.

ViewController.swift

1
    testParseConnection()

Then add a function below viewDidLoad() method.

ViewController.swift

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
    struct GameScore: ParseObject {
        //: Those are required for Object
        var objectId: String?
        var createdAt: Date?
        var updatedAt: Date?
        var ACL: ParseACL?

        //: Your own properties.
        var score: Int = 0

        //: Custom initializer.
        init(score: Int) {
            self.score = score
        }

        init(objectId: String?) {
            self.objectId = objectId
        }
    }

    func testParseConnection(){
        let score = GameScore(score: 10)
        let score2 = GameScore(score: 3)
        score.save { result in
            switch result {
            case .success(let savedScore):
                assert(savedScore.objectId != nil)
                assert(savedScore.createdAt != nil)
                assert(savedScore.updatedAt != nil)
                assert(savedScore.ACL == nil)
                assert(savedScore.score == 10)

                /*: To modify, need to make it a var as the value type
                    was initialized as immutable.
                */
                var changedScore = savedScore
                changedScore.score = 200
                changedScore.save { result in
                    switch result {
                    case .success(var savedChangedScore):
                        assert(savedChangedScore.score == 200)
                        assert(savedScore.objectId == savedChangedScore.objectId)

                        /*: Note that savedChangedScore is mutable since it's
                            a var after success.
                        */
                        savedChangedScore.score = 500

                    case .failure(let error):
                        assertionFailure("Error saving: \(error)")
                    }
                }
            case .failure(let error):
                assertionFailure("Error saving: \(error)")
            }
        }
    }
}
  1. Build your app in a device or simulator (Command+R).

    Build app

  2. Wait until the main screen appears.
  3. Login at Back4App Website.
  4. Find your app and click on Dashboard.
  5. Click on Core.
  6. Go to Browser.

If everything works properly, you should find a class named GameScore with the saved objects.

Next Steps

At this point, you have learned how to get started with iOS apps. You are now ready to explore Parse Server core features and Back4App add-ons.

Learn more by walking around our iOS Tutorials or check Parse open source documentation for iOS SDK.