Skip to content

joelparkerhenderson/demo-swift-rest

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Demo Swift REST

REST

This is a demonstration of:

This README describes how to create the demo.

Start

To use this demo, you can clone this repo, or you can use this README to create your own project.

If you clone this repo, then be aware that there are multiple git branches, so pick the one you want.

  • swift-4-xcode-9: Swift version 4, Xcode version 9, iOS version 11.

  • swift-3-xcode-8: Swift version 3, Xcode version 8, iOS version 10.

Create the project

Launch Xcode and create a new project.

  • Use the iOS template "Single View Application" and Project Name "Demo Swift REST".

  • Help

Create a simple way to print some text to the screen.

  • We create a text view object and IBOutlet named "demoTextView".

  • Help.

Add Alamofire, ObjectMapper, and Realm. We suggest using Carthage or Cocoapods.

  • Carthage Cartfile:

    github "Alamofire/Alamofire"
    github "Hearst-DD/ObjectMapper" ~> 2.2
    github "realm/realm-cocoa"
    
  • Help

If you want a simpler introduction to each piece of this demo, then see these related repos:

Add Alamofire

Edit ViewController.swift.

Add Alamofire networking code:

import UIKit
import Alamofire

class ViewController: UIViewController {

  @IBOutlet weak var demoTextView: UITextView!

  override func viewDidLoad() {
    super.viewDidLoad()
    Alamofire.request(.GET, "https://httpbin.org/get")
      .validate()
      .responseString { response in
         self.demoTextView.text = response.result.value
       }
     }
  }
  

Verify Alamofire works by runing the app.

The Simulator screen shows the response result value string, which looks something like this.

{
  "args": {},
  "headers": {
    "Accept": "*/*",
    "Accept-Encoding": "gzip;q=1.0, compress;q=0.5",
    "Accept-Language": "en-US;q=1.0",
    "Host": "httpbin.org",
    "User-Agent": "MyApp/com.example.MyApp …"
  },
  "origin": "207.237.149.238",
  "url": "https://httpbin.org/get"
}

Add an ObjectMapper model class

Create a directory named "Models".

Create a model called "Item" that implements the ObjectMappable interface:

import ObjectMapper

class Item: Mappable {

  // Create some properties that correspond to the
  // key fields in the JSON data that we will fetch.
  var origin: String?
  var url: String?

  // Implement Mappable
  required init?(map: Map) {
  }

  // Implement Mappable
  func mapping(map: Map) {
    origin <- map["origin"]
    url <- map["url"]
  }

}

Instantiate an ObjectMapper model instance

Edit ViewController.swift.

Add simple code to create a model object, then print some output to the screen.

import UIKit
import Alamofire
import ObjectMapper

class ViewController: UIViewController {

  @IBOutlet weak var demoTextView: UITextView!

  override func viewDidLoad() {
    super.viewDidLoad()
    Alamofire.request("https://httpbin.org/get")
      .validate()
      .responseString { response in
        self.demoTextView.text = response.result.value
        let item = Mapper<Item>().map(JSONString: response.result.value!)
        self.demoTextView.text = item!.url!
      }
    }
  }
  …

Run the app.

The Simulator screen shows the item URL, which is "https://httpbin.org/get".

Upgrade the model to use Realm

Edit Models/Item.swift.

Add code to import RealmSwift, and add public init methods, and make the properties dynamic and default to nil.

import ObjectMapper
import RealmSwift

public class Item: Object, Mappable {

  // Create some properties that correspond to the
  // key fields in the JSON data that we will fetch.
  dynamic var origin: String? = nil
  dynamic var url: String? = nil

  // Realm init
  convenience public init(data: [String: AnyObject]) {
    self.init()
  }

  // Implement Mappable
  required convenience public init?(map: Map) {
    self.init()
  }

  // Implement Mappable
  public func mapping(map: Map) {
    origin <- map["origin"]
    url <- map["url"]
  }

}

Upgrade the view to use Realm

Edit ViewController.swift

Add code to open Realm, and write and item, and read an item.

import UIKit
import Alamofire
import ObjectMapper
import RealmSwift

class ViewController: UIViewController {

  @IBOutlet weak var demoTextView: UITextView!

  override func viewDidLoad() {
    super.viewDidLoad()
    
    let realm = try! Realm()

    Alamofire.request("https://httpbin.org/get")
      .validate()
      .responseString { response in
        self.demoTextView.text = response.result.value
        let item = Mapper<Item>().map(JSONString: response.result.value!)
        self.demoTextView.text = item!.url!

        // Write the item to the database
        try! realm.write {
          realm.add(item!)
        }

        // Call and safely unwrap the url from the database, then assign to the textView
        if let url = realm.objects(Item).first?.url {
          self.demoTextView.text = url
        }
      }
      

Run

Run the app.

The Simulator screen shows the item URL, which is "https://httpbin.org/get".

Congratulations, you're successful!

Tracking

  • Package: demo_swift_rest
  • Version: 3.0.0
  • Created: 2016-05-30
  • Updated: 2017-09-22
  • License: BSD, GPL, MIT
  • Contact: Joel Parker Henderson (http://joelparkerhenderson.com)