https://github.com/intrepidpursuits/webservices-example-ios
Example iOS project for the "Consuming Web Services" lecture at Intrepid Pursuits. Written in Swift.
https://github.com/intrepidpursuits/webservices-example-ios
Last synced: about 1 year ago
JSON representation
Example iOS project for the "Consuming Web Services" lecture at Intrepid Pursuits. Written in Swift.
- Host: GitHub
- URL: https://github.com/intrepidpursuits/webservices-example-ios
- Owner: IntrepidPursuits
- License: mit
- Created: 2016-09-12T19:47:58.000Z (almost 10 years ago)
- Default Branch: interactive
- Last Pushed: 2018-06-18T18:34:58.000Z (about 8 years ago)
- Last Synced: 2025-03-25T02:43:45.074Z (about 1 year ago)
- Language: Swift
- Size: 43 KB
- Stars: 1
- Watchers: 11
- Forks: 10
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Consuming Web Services in iOS
## In-lecture exercise
Update the `name` property in the `ViewModel` class to match your first name (all lowercase, no spaces).
Implement the `execute` method of the struct `HTTPRequestHandler` to handle **GET** requests. To test your implementation run the app in the simulator or on device then click the 🔁 icon.
### Solution
Your finished method should look something like this:
```
func execute(callback: @escaping (Result) -> Void) {
guard let url = URL(string: path) else {
callback(.failure(RequestError.invalidURL))
return
}
var request = URLRequest(url: url)
request.httpMethod = method.rawValue
if let headers = headers {
for (key, value) in headers {
request.addValue(value, forHTTPHeaderField: key)
}
}
if let body = body {
do {
request.httpBody = try JSONEncoder().encode(body as? ColorData)
} catch (let e) {
callback(.failure(e))
}
}
let task = URLSession.shared.dataTask(with: request) { data, response, error in
if let error = error {
callback(.failure(error))
return
}
guard let response = response as? HTTPURLResponse else {
callback(.failure(RequestError.noResponse))
return
}
guard response.statusCode < 400 else {
callback(.failure(RequestError.httpResponse(response.statusCode)))
return
}
guard let data = data else {
callback(.failure(RequestError.noData))
return
}
if let str = String(data: data, encoding: .utf8) {
print("Received response: \(str)")
}
do {
let colorData = try JSONDecoder().decode(ColorData.self, from: data)
callback(.success(colorData))
}
catch {
callback(.failure(RequestError.decoderFailure))
}
}
task.resume()
}
```
## Post-lecture exercise
Build upon your `execute` function to handle **PUT** requests as well. You can see what the post takes in as arguments in the `ViewModel` class under the `setColor` method. You will need to add header values to and serialize the body data for URLRequest.
To test your implementation, run your app in simulator or on device and select a color from the color picker. The color you picked should appear under your name in the companion web app.
Create a pull request with this updated code for review and feedback.