Skip to content

Latest commit

 

History

History

Networking

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 
 
 

iOS Networking with Swift

Lesson 2: Using Web Services and APIs

  • Network the global system of interconnected computer networks from which we can access data.
  • Data images, text, video, or any media that can be used by our apps.
  • Protocol a way of communicating; standard operating procedure; the rules for communication
  • HTTP (Hypertext Transfer Protocol) a protocol or “handshake” that defines how messages (and data) are sent and received by computers; the underlying protocol for the World Wide Web
  • Client a computer that requests data (or services) from a server
  • Server a computer that provides data (or services) to a client upon request
  • HTTP request a request for data (or a resource) that a client makes to a server; there are various types of HTTP requests called “HTTP methods”
  • URL (Uniform Resource Locator) — specifies the location for retrieving data over the web; can be thought of as the name of a file on the World Wide Web, because it usually refers to a file on some machine on the network
  • HTTP method specifies the type of HTTP request that is being made; in this course, we will also call these (HTTP) request types
  • HTTP status code a number returned in response to an HTTP request which indicates the result of the request; you may also hear these referred to as “response codes” or “status messages." Here is a nice listing of different HTTP status codes.
  • HTTP GET request a type of HTTP request where a client requests a specified resource from a server
  • URLSessionTask : URL sessions provide three types of tasks: data tasks, upload tasks, and download tasks. Network request are know as "task"
override func viewDidLoad() {
    super.viewDidLoad()

    let imageURL = URL(string: "https://upload.wikimedia.org/wikipedia/commons/4/4d/Cat_November_2010-1a.jpg")!

    let task = URLSession.shared.dataTask(with: imageURL) { (data, response, error) in

        if error == nil {
            let downloadImage = UIImage(data: data!)

            performUIUpdatesOnMain {
              self.imageView.image = downloadImage
            }
        }
    }
    task.resume()
}
  • Configuring App Transport Security
  • Singleton a special kind of object that can only be instantiated once
  • URLSession manages network requests for our app
  • URLSessionConfiguration specify settings (ex. timeout interval) for a NSURLSession
  • URLSessionTask a network task that can be executed
  • URLSessionDataTask subclass of NSURLSessionTask; downloads data directly into memory
  • URLSessionDownloadTask subclass of NSURLSessionTask; downloads data to a temporary file
  • URLSessionUploadTask subclass of NSURLSessionTask; specialized for uploading data
  • URLResponse a container for information about the response to a network request such its status code
  • URL escaping/encoding a mechanism for ensuring that all characters in a URL are valid ASCII characters
  • Serialization 말그대로 객체를 직렬화하여 전송 가능한 형태로 만드는 것을 의미한다. 객체들의 데이터를 연속적인 데이터로 변형하여 Stream을 통해 데이터를 읽도록 해준다. 이것은 주로 객체들을 통째로 파일로 저장하거나 전송하고 싶을 때 주로 사용된다.
  • Deserialization 말그대로 객체를 직렬화하여 전송 가능한 형태로 만드는 것을 의미한다. 객체들의 데이터를 연속적인 데이터로 변형하여 Stream을 통해 데이터를 읽도록 해준다. 이것은 주로 객체들을 통째로 파일로 저장하거나 전송하고 싶을 때 주로 사용된다.
  • JSONSerialization a class used to convert bytes of JSON data into objects or vice versa
  • Between App and Server
  1. The client contacts the server and tries to use the flickr.galleries.getPhotos method to get some (URLs of) photos.
  2. The server returns a JSON object that contains locations of photos in the specified gallery.
  3. The client randomly chooses something out of that JSON object, attempts to download the image from that randomly chosen URL, and displays it."
  • URLComponents
import Foundation

var components = URLComponents()
components.scheme = "https"
components.host = "api.flickr.com"
components.path = "/services/rest"
components.queryItems = [URLQueryItem]()

let queryItem1 = URLQueryItem(name: "method", value: "flickr.photos.search")
let queryItem2 = URLQueryItem(name: "api_key", value: "1234")
let queryItem3 = URLQueryItem(name: "text", value: "purple")

components.queryItems!.append(queryItem1)
components.queryItems!.append(queryItem2)
components.queryItems!.append(queryItem3)

print(components.url!)