Xcode and iOS

home

Contents

Xcode version no, iOS version no

Layout Background

Layout lab 1, no constraint

Layout lab 2, adding constraints on one ui object

Layout lab 3, adding constraints on two ui objects

...

More on AutoLayout

UI and Code

Xcode

1. easy to test for multiple devices

2. MVC, Storyboard, ViewController.swift, connect, no id for each ui element

3. Indentity Inspector

4. Connection Inspector for Code Delegation

                class ViewController: UIViewController,
                   
                                      UITableViewDataSource,
                                      UITableViewDelegate
                   
            

5. Segue(Action Segue) on the storyboard rendering,   Attribute Inspector for Segue

                override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
                    let secondView = segue.destination as! SecondViewController
                    if (segue.identifier == "id1"){
                        secondView.data = " data from screen 1 "
                    } 
             }
            

6. Relation Segue, created from a helper - Navigation Controller

7. Tabbed Template

8. Master-Detail Template

...

REST iOS Client

code snippets

class ViewController: UIViewController {
    var myData = "hello"

    @IBAction func getClick(_ sender: AnyObject) {
        
        print("GET,   5517 11:10 ");
        // First, set up the URL request
        let todoEndpoint: String = "http://jsonplaceholder.typicode.com/todos/1"
        
        // The guard statement lets us check that the URL we’ve provided is valid.
        guard let urlObj = URL(string: todoEndpoint) else {
            print("Error: cannot create URL")
            return
        }
        let urlRequest = URLRequest(url: urlObj)
        
        print("location before call ");
 
        // Then we need an NSURLSession to use to send the request:
        let config = URLSessionConfiguration.default
        let session = URLSession(configuration: config)
        //let task = session.dataTaskWithRequest(urlRequest, completionHandler:{ _, _, _ in });
        
        let task = session.dataTask(with: urlRequest, completionHandler: { (data, response, error) in
            // do stuff with response, data & error here
            
            // check for any error
            guard error == nil else {
                print("error calling GET on /todos/1")
                print(error!)
                return
            }
            // make sure we got data
            guard let responseData = data else {
                print("Error: did not receive data")
                return
            }
            print("after call, check ok")
            
            do {
                print("entering do")
                guard let todo = try JSONSerialization.jsonObject(with: responseData, options: []) as? [String: AnyObject] else {
                    print("error trying to convert data to JSON")
                    return
                }
                print("The todo is: " + todo.description)
                
                // the todo object is a dictionary
                
                guard let todoTitle = todo["title"] as? String else {
                    print("Could not get todo title from JSON")
                    return
                }
                print("The title is: " + todoTitle)
                self.myData = todoTitle
            } catch {
                print("error trying to convert data to JSON")
                return
            }
            
        }) // end of dataTask
        
        print("location after dataTask ");
        task.resume();
        
    }
    
    
    @IBAction func postClick(_ sender: AnyObject) {
        
        print("POST   test p2");
        
        let todosEndpoint: String = "http://jsonplaceholder.typicode.com/todos"
        guard let todosURL = URL(string: todosEndpoint) else {
            print("Error: cannot create URL")
            return
        }
        let todosUrlRequest = NSMutableURLRequest(url: todosURL)
        todosUrlRequest.httpMethod = "POST"
        
        let newTodo = ["title": "Frist todo", "completed": false, "userId": 1] as [String : Any]
        let jsonTodo: Data
        do {
            jsonTodo = try JSONSerialization.data(withJSONObject: newTodo, options: [])
            todosUrlRequest.httpBody = jsonTodo
        } catch {
            print("Error: cannot create JSON from todo")
            return
        }
        
        let config = URLSessionConfiguration.default
        let session = URLSession(configuration: config)
        
        //let task = session.dataTaskWithRequest(todosUrlRequest, completionHandler:{ _, _, _ in })
        let task = session.dataTask(with: todosUrlRequest as URLRequest, completionHandler: {
            (data, response, error) in
            guard let responseData = data else {
                print("Error: did not receive data")
                return
            }
            guard error == nil else {
                print("error calling POST on /todos/1")
                //print(error)
                return
            }
            
            // parse the result as JSON, since that's what the API provides
            do {
                guard let receivedTodo =
                    try JSONSerialization.jsonObject(with: responseData, options: [])
                                        as? [String: AnyObject]
                    else {
                        print("Could not get JSON from responseData as dictionary")
                        return
                     }
                print("The todo is: " + receivedTodo.description)
                
                guard let todoID = receivedTodo["id"] as? Int
                    else {
                         print("Could not get todoID as int from JSON")
                         return
                    }
                print("The ID is: \(todoID)")
              } catch  {
                print("error parsing response from POST on /todos")
                return
              }
        }) 
        
        task.resume()
        print("after resume")
        
    }
    
    @IBAction func deleteClick(_ sender: AnyObject) {
        
        print("DELETE test 2")
        let firstTodoEndpoint: String = "http://jsonplaceholder.typicode.com/todos/1"
        let firstTodoUrlRequest = NSMutableURLRequest(url: URL(string: firstTodoEndpoint)!)
        firstTodoUrlRequest.httpMethod = "DELETE"
        
        let config = URLSessionConfiguration.default
        let session = URLSession(configuration: config)
        
        let task = session.dataTask(with: firstTodoUrlRequest as URLRequest, completionHandler: {
            (data, response, error) in
            guard let _ = data else {
                print("error calling DELETE on /todos/1")
                return
            }
        }) 
        task.resume()
        print("after return from DELETE")
        
    }
    
    @IBAction func test3(_ sender: Any) {
        print(self.myData)
    }