iOS Development iOS Networking and APIs 1 — Questions and Answers
Question 1: Which iOS framework is used to make HTTP network requests?
- URLSession (Correct answer)
- CFNetwork
- Alamofire
- NSURLConnection
Correct answer: URLSession
URLSession is Apple's modern, built-in framework for performing HTTP and HTTPS requests, downloads, and uploads.
Question 2: What is the purpose of URLRequest in iOS networking?
- Encapsulates a URL, HTTP method, headers, and body for a network request (Correct answer)
- Parses the JSON response from a server
- Manages authentication credentials for a session
- Caches responses on disk
Correct answer: Encapsulates a URL, HTTP method, headers, and body for a network request
URLRequest is a struct that holds all information needed for a request: URL, HTTP method, headers, and optional body data.
Question 3: What does the `async/await` version of URLSession data task return?
- (Data, URLResponse) (Correct answer)
- (Data, URLResponse, Error)
- Result<Data, Error>
- AnyCancellable
Correct answer: (Data, URLResponse)
The async `URLSession.data(for:)` method returns a tuple of `(Data, URLResponse)` and throws an error on failure.
Question 4: Which HTTP method is used to create a new resource on a REST API?
- POST (Correct answer)
- GET
- PUT
- PATCH
Correct answer: POST
The HTTP POST method is conventionally used to submit data to a server to create a new resource.
Question 5: What is App Transport Security (ATS) in iOS?
- A policy that requires apps to use HTTPS for network connections by default (Correct answer)
- An Apple service for rate-limiting API calls
- A framework for encrypting local data
- A certificate pinning mechanism
Correct answer: A policy that requires apps to use HTTPS for network connections by default
ATS enforces that all network connections from an iOS app use HTTPS with strong TLS settings unless explicitly exempted.
Question 6: What does JSON decoding with `JSONDecoder` require of Swift types?
- They must conform to Decodable (Correct answer)
- They must inherit from NSObject
- They must conform to NSCoding
- They must implement a custom init
Correct answer: They must conform to Decodable
`JSONDecoder` can decode any Swift type that conforms to the `Decodable` protocol, automatically mapping JSON keys to properties.
Which iOS framework is used to make HTTP network requests?