. . .

iOS 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

    • view 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 log in with your existing account here to start using ApiOmat.

    images/download/attachments/27723778/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/27723778/taskManager_newBackend.png

    3. Create a new module by going to the Module Market and clicking on "New Module" in the submenu. Give it a name and description and click "CREATE".

    images/download/attachments/27723778/newNmModule.png

    4. 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/27723778/task_addClassAttributes.png

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

    6. Go to the SDK Tab and download the Objective-C library by pressing "Download SDK" button. You will get a zip file with your generated classes and all necessary libraries.

    images/download/attachments/27723778/task_downloadSDK.png

    7. 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). Then click next, select a folder for your project and create it.

    images/download/attachments/27723778/SetupXcode.png

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

    images/download/attachments/27723778/Copylib1.png

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

    images/download/attachments/27723778/Copylib2.png

    10.Open “Build Settings”, switch to “All” on the left side and add at “Other Linker Flags” “-all_load” and if you are targeting iOS version less than 5.0 also add “-fobjc-arc”

    images/download/attachments/27723778/addLinkerFlags.png

    11. Now you are ready to go. In the class “AppDelegate.h” add the import:

    #import "User.h"
    #import "Datastore.h"

    and a new property:

    @property (strong, nonatomic) AOMUser *user;
    public var user: User?

    12. We configure Datastore and user in “AppDelegate.m” 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.

    @synthesize user;
     
    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    user = [[AOMUser alloc] init];
    [user setUserName:@"<username>"];
    [user setPassword:@"<password>"];
     
    //Configure Datastore with user credentials
    [AOMDatastore configureWithUser:user];
     
    [user loadMeAsyncWithFinishingBlock:^(NSError *error) {
    //There is no User with this name on server so add one
    if(error != nil) {
    [user saveAsyncWithBlock:^(NSError *error) {
    }];
    }
    }];
     
    return YES;
    }
    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]?) -> Bool {
     
    self.user = User(userName: "<username>", password: "<password>")
    //Configure Datastore with user credentials
    DataStore.configureWithCredentials(user: user)
    user?.loadMe { (error) in
      //There is no User with this name on server so add one
    if error != nil {
     
    user?.save(completion: { (error) in
    })
    }
    }
     
    return true
    }

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

    images/download/attachments/27723778/task_newUserCreated.png

    14. Now let’s play around with the Task-Class. Add the following code to some of your view controllers (e.g. ViewController.m). The best place is the viewDidLoad method. We will create 2 objects of class “Task” (Be sure that you imported the “Task.h” header file).

    - (void)viewDidLoad
    {
    [super viewDidLoad];
    Task *firstTask = [Task new];
    [firstTask setDescription:@"Drink coffee"];
    //Set task to "done"
    [firstTask setDone:1];
    //Save in backend
    [firstTask saveAsyncWithBlock:^(NSError *error) {
    //Add another task
    Task *secondTask = [Task new];
    [secondTask setDescription:@"Eat to lunch"];
    //Task is already undone
    [secondTask setDone:0];
    [secondTask saveAsyncWithBlock:^(NSError *error) {
    }];
    }];
    }
    override func viewDidLoad() {
     
    super.viewDidLoad()
    var firstTask = Task()
    firstTask.description = "Dring coffee"
     
    //Set task to "done"
    firstTask.done = 1
     
    firstTask.save(completion: { (error) in
     
    //Add another task
    var secondTask = Task()
    secondTask.description = "Eat to lunch"
    //Task is already undone
    secondTask.done = 1
    secondTask.save(completion: { (error) in
    })
    })
    }

    15. Now let’s request all tasks from the ApiOmat backend. We add this after the save statement of the second task.

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

    __block NSMutableArray *tasks = nil;
    [Task getAsyncWithQuery:@"" withBlock:^(NSMutableArray *models, NSError *error) {
     
    if(error == nil) {
    tasks = models;
    }
    }];
    var tasks:[Task]?
    Task.loadList { (models, error) in
    if error == nil {
    tasks = models
    }
    }

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

    __block NSMutableArray *undoneTasks = nil;
     
    [Task getAsyncWithQuery:@"" withBlock:^(NSMutableArray *models, NSError *error) {
     
    if(error == nil) {
    undoneTasks = models;
    for (Task *t in undoneTasks) {
    NSLog(@"Description: %@", [t description]);
    }
    }
    }];
    var undoneTasks:[Task]?
    Task.loadList(query: "done==0", usePersistentStorage: false) { (models, error) in
    if error == nil {
    undoneTasks = models
    guard let tasks = undoneTasks else { return }
    for let task in tasks {
    print("Description: \(task.description)")
    }
    }
    }