URL Loading System Programming Guide - Apple Developer
URL Loading System Programming Guide - Apple Developer
URL Loading System Programming Guide - Apple Developer
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
Life Cycle of a <strong>URL</strong> Session<br />
Life Cycle of a <strong>URL</strong> Session with <strong>System</strong>-Provided Delegates<br />
2. Create a session, specifying a configuration object and a nil delegate.<br />
3. Create task objects within a session that each represent a resource request.<br />
Each task starts out in a suspended state. After your app calls resume on the task, it begins downloading<br />
the specified resource.<br />
The task objects are subclasses of NS<strong>URL</strong>SessionTask—NS<strong>URL</strong>SessionDataTask,<br />
NS<strong>URL</strong>SessionUploadTask, or NS<strong>URL</strong>SessionDownloadTask, depending on the behavior you are<br />
trying to achieve. These objects are analogous to NS<strong>URL</strong>Connection objects, but give you more control<br />
and a unified delegate model.<br />
Although your app can (and typically should) add more than one task to a session, for simplicity, the<br />
remaining steps describe the life cycle in terms of a single task.<br />
Important: If you are using the NS<strong>URL</strong>Session class without providing delegates, your app must<br />
create tasks using a call that takes a completionHandler parameter, because otherwise it cannot<br />
obtain data from the class.<br />
4. For a download task, during the transfer from the server, if the user tells your app to pause the download,<br />
cancel the task by calling cancelByProducingResumeData: method. Later, pass the returned resume<br />
data to either the downloadTaskWithResumeData: or<br />
downloadTaskWithResumeData:completionHandler: method to create a new download task that<br />
continues the download.<br />
5. When a task completes, the NS<strong>URL</strong>Session object calls the task’s completion handler.<br />
Note: NS<strong>URL</strong>Session does not report server errors through the error parameter. The only<br />
errors your app receives through the error parameter are client-side errors, such as being unable<br />
to resolve the hostname or connect to the host. The error codes are described in <strong>URL</strong> <strong>Loading</strong><br />
<strong>System</strong> Error Codes.<br />
Server-side errors are reported through the HTTP status code in the NSHTTP<strong>URL</strong>Response<br />
object. For more information, read the documentation for the NSHTTP<strong>URL</strong>Response and<br />
NS<strong>URL</strong>Response classes.<br />
6. When your app no longer needs a session, invalidate it by calling either invalidateAndCancel (to cancel<br />
outstanding tasks) or finishTasksAndInvalidate (to allow outstanding tasks to finish before invalidating<br />
the object).<br />
2013-10-22 | Copyright © 2003, 2013 <strong>Apple</strong> Inc. All Rights Reserved.<br />
65