Splitting big tasks into small tasks is often one of the best ways to solve a problem. Thus, in the next few exercises, we will split each request/method into a 7 step process. In the source code, I have commented each of these steps to assist you as you implement the requests.
7 Step Process
- Set the parameters
- Specify any parameters for the web service method that should be included in the final url
- For example, if the final URL is https://api.themoviedb.org/3/authentication/token/new?api_key=ENTER_YOUR_API_KEY_HERE, then in this step you should set the API key
- Build the URL
- Combine the base URL, the method being used, and the parameters to create the final URL for the request
- Configure the request
- Create a
NSURLRequest
orNSMutableURLRequest
using the URL - (If necessary) When using a
NSMutableURLRequest
, you may need to configure the request‘s header fields and HTTP body
- Create a
- Make the request
- Using the shared
NSURLSession
singleton, issue the request and specify a completion handler
- Using the shared
- Parse the data
- Assuming there is no error when the request returns, parse the response data into a usable type like a
NSDictionary
,NSArray
, or[String:AnyObject]
, ...
- Assuming there is no error when the request returns, parse the response data into a usable type like a
- Use the data!
- Extract the data required for the specified task
- Start the request
- To initiate the request, it must be started (resumed)
时间: 2024-11-08 18:17:37