Cara Membuat SignUp User untuk Mobile Aplikasi iOS Menggunakan Mesosfer

Ditulis oleh Ikrar Ilyas, dipublikasi pada 06 Dec 2016 dalam kategori Tutorial
Cara Membuat SignUp User untuk Mobile Aplikasi iOS Menggunakan Mesosfer - CodePolitan.com

Cara membuat SignUp User untuk Mobile Aplikasi iOS menggunakan Mesosfer adalah tutorial lanjutan dari tutorial cara membuat starter project untuk Mobile Aplikasi iOS menggunakan Mesosfer. Jika kalian telah mengikuti tutorial sebelumnya, pasti akan lebih mudah mengikuti tutorial yang akan kita bahas kali ini.

Seperti biasa, akan saya sediakan tutorial untuk Objective-C dan Swift agar kalian lebih mudah. Tutorial ini juga telah di publikasikan di blog Mesosfer. Saya telah mendapat izin untuk menulis ulang dalam bahasa Indonesia disini.

Kelengkapan yang Diperlukan

  • Pastikan Anda telah memiliki akun di Mesosfer.
  • Install XCode terbaru. Penulis menggunakan Xcode 8.1 (8B62).
  • Anda bisa download ode dari sesi sebelumnya disini.

Mulai!

Buat Registration View pada Main.storyboard seperti di bawah:

signup ios

Tambahkan RegisterViewController Class pada project XCode Anda, kemudian tambahkan referencing outlet untuk setiap object library

Objective-C

@property (weak, nonatomic) IBOutlet UITextField *textEmail;
@property (weak, nonatomic) IBOutlet UITextField *textPassword;
@property (weak, nonatomic) IBOutlet UITextField *textFirstName;
@property (weak, nonatomic) IBOutlet UITextField *textLastName;
@property (weak, nonatomic) IBOutlet UITextField *textDateOfBirth;
@property (weak, nonatomic) IBOutlet UITextField *textHeight;
@property (weak, nonatomic) IBOutlet UITextField *textWeight;
@property (weak, nonatomic) IBOutlet UISwitch *switchIsMarried;

Swift

@IBOutlet weak var textEmail: UITextField!
@IBOutlet weak var textPassword: UITextField!
@IBOutlet weak var textFirstName: UITextField!
@IBOutlet weak var textLastName: UITextField!
@IBOutlet weak var textDateOfBirth: UITextField!
@IBOutlet weak var textHeight: UITextField!
@IBOutlet weak var textWeight: UITextField!
@IBOutlet weak var switchIsMarried: UISwitch!

Code lengkap RegisterViewController

Objective-C

#import 

@interface RegisterViewController : UITableViewController

@property (weak, nonatomic) IBOutlet UITextField *textEmail;
@property (weak, nonatomic) IBOutlet UITextField *textPassword;
@property (weak, nonatomic) IBOutlet UITextField *textFirstName;
@property (weak, nonatomic) IBOutlet UITextField *textLastName;
@property (weak, nonatomic) IBOutlet UITextField *textDateOfBirth;
@property (weak, nonatomic) IBOutlet UITextField *textHeight;
@property (weak, nonatomic) IBOutlet UITextField *textWeight;
@property (weak, nonatomic) IBOutlet UISwitch *switchIsMarried;

@end
  • .m file
#import "RegisterViewController.h"
#import "UIViewController+Helper.h"
#import 

@implementation RegisterViewController

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    // handle register user clicked row
    if (indexPath.section == 2 && indexPath.row == 0) {
        NSString *email = self.textEmail.text;
        NSString *password = self.textPassword.text;
        NSString *firstname = self.textFirstName.text;
        NSString *lastname = self.textLastName.text;

        NSDate *dateOfBirth = [[MFDateFormatter sharedFormatter] dateFromString:self.textDateOfBirth.text];
        NSNumber *height = [NSNumber numberWithDouble:[self.textHeight.text doubleValue]];
        NSNumber *weight = [NSNumber numberWithInt:[self.textWeight.text intValue]];
        NSNumber *isMarried = [NSNumber numberWithBool:self.switchIsMarried.isOn];

        MFUser *user = [MFUser user];
        // set required field
        user.email = email;
        user.password = password;
        user.firstname = firstname;
        user.lastname = lastname;
        // set custom field
        if (dateOfBirth) {
            user[@"dateOfBirth"] = dateOfBirth;
        }
        if (height) {
            user[@"height"] = height;
        }
        if (weight) {
            user[@"weight"] = weight;
        }
        if (isMarried) {
            user[@"isMarried"] = isMarried;
        }

        // register user async
        [user registerAsyncWithBlock:^(BOOL succeeded, NSError * _Nullable error) {
            if (succeeded) {
                [self showAlertWithTitle:@"Register Succeeded"
                                 message:@"Thank you for registering on Mesosfer." handler:nil];
            } else {
                [self showErrorWithTitle:@"Register failed" error:error handler:nil];
            }
        }];
    }
}
@end

Swift

import Foundation
import UIKit
import Mesosfer

class RegisterViewController: UITableViewController {
    @IBOutlet weak var textEmail: UITextField!
    @IBOutlet weak var textPassword: UITextField!
    @IBOutlet weak var textFirstName: UITextField!
    @IBOutlet weak var textLastName: UITextField!
    @IBOutlet weak var textDateOfBirth: UITextField!
    @IBOutlet weak var textHeight: UITextField!
    @IBOutlet weak var textWeight: UITextField!
    @IBOutlet weak var switchIsMarried: UISwitch!

    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        // handle register user clicked row
        if indexPath.section == 2 && indexPath.row == 0 {
            let email = self.textEmail.text
            let password = self.textPassword.text
            let firstname = self.textFirstName.text
            let lastname = self.textLastName.text

            let dateOfBirth = self.textDateOfBirth.text
            let height = self.textHeight.text
            let weight = self.textWeight.text
            let isMarried = self.switchIsMarried.isOn

            let user = MFUser()
            // set required field
            user.email = email
            user.password = password
            user.firstname = firstname
            user.lastname = lastname
            // set custom field
            if let date = MFDateFormatter.shared().date(from: dateOfBirth!) {
                user["dateOfBirth"] = date
            }
            user["height"] = Double(height!)
            user["weight"] = Int(weight!)
            user["isMarried"] = isMarried
            // register user async
            user.registerAsync(block: { (succeeded, error) in
                if succeeded {
                    self.showAlert(title: "Register Succeeded", message: "Thank you for registering on Mesosfer.")
                } else {
                    self.showError(title: "Register failed", error: (error as? NSError)!)
                }
            })
        }
    }
}

Untuk memunculkan custom alert, berikut adalah Objective-C Helper Class dan Swift Extension untuk UIViewController:

Objective-C

#import 
#import 

@interface UIViewController (Helper)

- (void) showAlertWithTitle:(NSString*_Nullable)title
                    message:(NSString*_Nonnull)message
                    handler:(void (^ __nullable)(UIAlertAction *_Nonnull action))handler;

- (void) showErrorWithTitle:(NSString*_Nonnull)title
                      error:(NSError*_Nonnull)error
                    handler:(void (^ __nullable)(UIAlertAction *_Nonnull action))handler;

@end
  • UIViewController+Helper.m file
#import "UIViewController+Helper.h"

@implementation UIViewController(Helper)

- (void) showAlertWithTitle:(NSString*_Nullable)title
                    message:(NSString*_Nonnull)message
                    handler:(void (^ __nullable)(UIAlertAction *_Nonnull action))handler {
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title
                                                                   message:message
                                                            preferredStyle:UIAlertControllerStyleAlert];
    [alert addAction:[UIAlertAction actionWithTitle:@"OK"
                                              style:UIAlertActionStyleCancel
                                            handler:handler]];
    [self presentViewController:alert animated:YES completion:^{

    }];
}

- (void) showErrorWithTitle:(NSString*_Nonnull)title
                      error:(NSError*_Nonnull)error
                    handler:(void (^ __nullable)(UIAlertAction *_Nonnull action))handler {
    NSString *message = [NSString stringWithFormat:@"Code=%ld\nDescription=%@", (long)error.code, error.localizedDescription];
    [self showAlertWithTitle:title message:message handler:handler];
}

@end

Swift

import Foundation
import UIKit
import Mesosfer

extension UIViewController {
    func showAlert(title: String? = nil, message: String, handler: ((UIAlertAction) -> Swift.Void)? = nil) {
        let alert = UIAlertController(title: title, message: message, preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "OK", style: .default, handler: handler))
        self.present(alert, animated: true, completion: {

        })
    }

    func showError(title: String, error: NSError, handler: ((UIAlertAction) -> Swift.Void)? = nil) {
        let message = "Code=\(error.code)\nDescription=\(error.localizedDescription)"
        self.showAlert(title: title, message: message, handler: handler)
    }
}

Kesimpulan

Kita telah membuat signup di mobile aplikasi iOS dengan cara yang sederhana dan cepat. Ini dapat membantu developer untuk menciptakan mobile aplikasi iOS dengan cepat sehingga memudahkan pekerjaan mereka.

Kalau kalian mencoba tutorial ini, share pengalaman kalian ya. Good luck!




Berlangganan Informasi CodePolitan Melalui Email

Jangan sampai kamu melewatkan informasi penting tentang pemrograman dan teknologi! Kamu bisa berlangganan Newsletter CodePolitan dengan cara mengisi formulir di samping.