Official Apple Document translation: URL Loading System Programming Guide

Source: Internet
Author: User
Tags ftp protocol

URL Loading System Programming Guide

(This article is a summary description, some limitations)
Original link

About

This guide describes the related classes that use standard Internet protocols to handle URLs, communicate with servers.
The URL Loading system includes a series of classes and protocols to support app access to content on URLs. The core class is Nsurl, which helps the app to control the URL and what the URL points to.
To support the Nsurl,apple Foundation framework provides a rich class to help you load URL content, update data from the server, manage cookie storage, control caching, process certificates and authentication methods on the app, and extend user protocols.


The URL Loading system supports access to resources through the following protocols:

    • FTP Protocol (ftp://)
    • Hypertext Transfer Protocol (/HTTP)
    • Encrypting Hypertext Transfer Protocol (https://)
    • Local Resources (file://)
    • Data URLs (data://)

Obviously, it also supports the Socks Gateway (?) of the proxy server and user system settings.

提示:此外,OS X和iOS在其他应用中也提供了打开URLs的APIs,例如Safari.这些APIs不包含在本文档中.*For more information about Launch Services in OS X, read Launch Services Programming Guide. *For more information about the openURL: method in the NSWorkSpace class in OS X, read NSWorkspace Class Reference. *For more information about the openURL: method in the UIApplication class in iOS, read UIApplication Class Reference.
Overview

The URL loading system includes a number of helper classes to assist with URLs. There are five main categories: protocol support, certificate authentication, cookie storage, configuration management and cache management.

Load URL

The most commonly used class in the URL loading system allows the app to retrieve content from the source URL. Depending on the needs of different apps, you can choose different search methods. How to choose the right API depends on your OSX version or the target version of your iOS app, and the type of data you get File or a piece of memory:
* IOS7 above, or OSX v10.9 or above, priority should be given to using nsurlsession to process URL requests.
* If you must support an older version of OSX, you can select nsurldownload to download the contents of the URL and store it on your hard disk.
* If you must support an older version of iOS or OSX, you can use nsurlconnection to download content and save it in memory, or you can write data to the hard disk if needed.

How you choose the right method depends on whether you want to save the data in memory or on your hard drive.

Get data content into memory

At a high level, there are two basic ways to load URLs:

    • For simple requests, use nsurlsession directly to retrieve content from a nsurl object, regardless of whether the result is a nsdata object or a disk file.
    • For complex requests, such as uploading data, using nsurlsession or nsurlconnection to load a nsurlrequest, or its subclasses Nsmutableurlrequest.

Whichever way you use it, the app can get response data in the following two ways:

    • A block to complete processing. This block is called when the class that loads the URL receives data from the server.
    • A custom delegate. When the URL load class obtains data from the server, its proxy method is called periodically. If necessary, the app can be responsible for accumulating data continuously.

In addition, for the data itself, the URL load class gives the above blocks and delegate a response object (compressed metadata) related to the request, such as MIME and content length.

Related chapters: using nsurlsession using Nsurlconnection

Download data as a file

There are also two basic ways to download content from a URL as a file:

    • For simple requests, use nsurlsession directly to retrieve content from a nsurl object, regardless of whether the result is a nsdata object or a disk file.
    • For complex requests, such as uploading data, using nsurlsession or nsurldownload to load a nsurlrequest, or its subclasses Nsmutableurlrequest.

Nsurlsession has two significant advantages over nsurldownload : It can be used on iOS, and even when the application hangs, exits, and flashes back, it can still download data in the background.

提示:通过NSURLDownload或者NSURLSession进行的下载是没有缓存的,如果需要缓存下载结果,必须使用NSURLConnection或者NSURLSession,并且由开发者自己将数据写入磁盘.

Related chapters: using Nsurldownload using Nsurlsession

Auxiliary classes

The URL load class uses two specific load classes to provide additional data: one for the request itself (Nsurlrequest) and one for the server (nsurlresponse).

URL Request

A Nsurlrequest object encapsulates a URL and the corresponding protocol information, and also specifies a local cache policy. When using nsurlconnection and Nsurldownload, you can also set its time-out ( The timeout for nsurlsession is configured in per-session basis).

提示:当客户端app使用一个NSMutableURLRequest对象向服务器发起连接或者下载时,request会进行深拷贝,在下载初始化完成后修改request不会影响之前的设置.

Some protocols support setting their own properties. For example, HTTP protocol, in nsurlrequest the return HTTP request body, the method of the request header and the conversion method. The same nsmutableurlrequest added these methods.

Response data

The response data of a server to a request can be divided into two parts: the content data itself and its description. The usual protocol metadata is encapsulated in Nsurlresponse, and contains MIME type, expected data size, text encoding type, And the subclass of the requested Url.nsurlresponse specific protocol can provide additional information, such as Nshttpurlresponse stores the response headers and status codes returned by the server.

提示:NSURLResponse对象中只存储了响应的元数据.其他的URL加载类通过block或代理提供响应数据.一个NSCachedURLResponse实例包含一个NSURLResponse对象,URL内容数据以及其他额外信息,参见后文Cache Management .
Redirect and other request changes

Some protocols, such as HTTP, notify the client in some way when the destination URL is migrated. When this happens, the URL load class notifies its proxy. If your app implements the corresponding proxy method, you can choose to request a redirected address or return an error message.

Related chapters: Handling redirects and other Request changes

Certifications and certificates

Some servers restrict access unless you provide the appropriate authentication information. From a server perspective, restricted content is grouped into a collection that requires a separate set of certifications. Certificates can also be used to confirm that the server being accessed is not trustworthy.

提示:证书永久存储在用户钥匙串中,所有app公用.
Cache Management

The URL loading system provides disk caching and memory caching to reduce app dependency on the network and speed up loading.

Cookie Management

Due to the stateless nature of the HTTP protocol, customers often use cookies to provide URL requests for persistent data storage. The URL loading system provides an interface to create and manage cookies, use cookies as part of an HTTP request, and interpret the Web server's response when receiving cookies.

Protocol support

The URL loading system natively supports the HTTP,HTTPS,FILE,FTP and data protocols. However, the URL loading system also allows applications to register your own classes that have supported additional application layer network protocols. You can also add specific protocol properties to the URL request and response objects.

Reprint Please specify source: http://blog.csdn.net/qq329735967
Any questions welcome email to [email protected]

Official Apple Document translation: URL Loading System Programming Guide

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.