. . .

Swift Quickstart

    In this Tutorial you will learn to:

    • set up a backend with a custom class “Task”.

    • deploy the backend

    • create a simple iOS project with the generated SDK

    • set up the connection to the backend and create a User

    • have a look at the data in your backend

    • create Task objects in Objective-C and store it in the backend

    • query data from the backend

    1. Sign Up or login with your existing account here to start using backend as a service.

    images/download/attachments/27723810/gettingStarted_login.png

    2. Enter a unique name for your app's backend (e.g. “YourNameTaskManager”).

    Since we plan to give developers the opportunity to share projects, application names must be unique in the system. You do not need any of the predefined modules in this tutorial.

    images/download/attachments/27723810/taskManager_newBackend.png

    3. Go to the "Class Editor"- Tab to create a class in our backend. You will be asked for a class name after entering the tab. Just call the first class „Task“. Scroll down to the attributes and add a String “description”, a Number “done” and a Date “untilDate”.

    images/download/attachments/27723810/task_addClassAttributes.png

    4. Deploy your Backend by pressing the “Deploy”-Button.

    5. Go to SDK Tab and download the Swift SDK by pressing “Download SDK“ button. You will get a zip file with your generated classes and all necessary libraries.

    images/download/attachments/27723810/switf_downloadSDK.png

    6. Go to your Xcode and start a new Project under File->New->Project. Choose “Single View Application”, click next and enter a “Product Name”, e.g.: "TestTaskManager" (In older versions of Xcode: Make sure that “Use Automatic Reference Counting” is checked). Please also make sure, "Swift" is selected as your desired project's language. Then click next, select a folder for your project and create it.

    images/download/attachments/27723810/ApiOmat_Tutorial_Swift_Create_Project.png

    7.Now move the downloaded and unzipped library files to your project by dragging the directory to your “Project Navigator”.

    images/download/attachments/27723810/ApiOmat_Tutorial_Swift_Add_SDK.png

    8. Select "Copy items if needed" and tick the radio-button for "Create groups" at the "Added folders"-Option.

    images/download/attachments/27723810/ApiOmat_Tutorial_Swift_Add_SDK_Step_Two.png

    9. Configure the Datastore and user in “AppDelegate.swift” file.

    Add user credentials by replacing the text with your own and configure DataStore in method application:didFinishLaunchingWithOptions. Don’t forget to synthesize user property.

    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    // Override point for customization after application launch.
    let user = User()
    user.userName = "yourName"
    user.password = "yourPassword"
    DataStore.configureWithCredentials(user: user)
    user.loadMe { error in
    if let error = error {
    user.save(loadAfterwards: true, completion: { error in
    if error != nil {
    //handle error
    }
    })
    print(error)
    }
    }
    return true
    }

    10. Congratulations! Your backend and your connection are now working. Have a look at the Data-Tab of your Dashboard to see that the created user is in your Backend.

    images/download/attachments/27723810/task_newUserCreated.png

    11. Now let’s play around with the Task-Class. Add the following code to some of your view controllers (e.g. ViewController.swift). The best place is the viewDidLoad method. We will create 2 objects of class “Task”.

    override func viewDidLoad() {
    super.viewDidLoad()
    let firstTask = Task()
    firstTask.description = "drink coffee"
    //set task to done
    firstTask.done = 1
    //save task
    firstTask.save() { error in
    if error != nil {
    //handle error
    }
    }
     
    //create another task
    let secondTask = Task()
    secondTask.description = "have lunch"
    //task is not done, yet
    secondTask.done = 0
    //save task
    secondTask.save() { error in
    if error != nil {
    //handle error
    }
    }
    }
     

    12. Now let’s request all tasks from the ApiOmat backend. Add this after saving the statement of the second task.

    We use a common method to request list of tasks and get back an array.

    var tasks: [Task]? = nil
    Task.loadList() { taskList, error in
    if error != nil {
    //handle error
    }
    else {
    tasks = taskList as? [Task]
    }
    }

    13. Finally, we will use a query to get back all non-done tasks.

    var tasks: [Task]? = nil
     
    Task.loadList(query: "done==0") { taskList, error in
    if error != nil {
    //handle error
    }
    else {
    tasks = taskList as? [Task]
    }
    }