ios网络:应用一个请求的7个步骤

时间:2023-03-10 03:35:34
ios网络:应用一个请求的7个步骤

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

  1. Set the parameters
  2. Build the URL
    • Combine the base URL, the method being used, and the parameters to create the final URL for the request
  3. Configure the request
    • Create a NSURLRequest or NSMutableURLRequest using the URL
    • (If necessary) When using a NSMutableURLRequest, you may need to configure the request's header fields and HTTP body
  4. Make the request
    • Using the shared NSURLSession singleton, issue the request and specify a completion handler
  5. Parse the data
    • Assuming there is no error when the request returns, parse the response data into a usable type like a NSDictionaryNSArray, or [String:AnyObject], ...
  6. Use the data!
    • Extract the data required for the specified task
  7. Start the request
    • To initiate the request, it must be started (resumed)