1.URLURL is a concise representation of the location and method of access to resources available from the Internet, and is the address of standard resources on the Internet. Each file on the Internet has a unique URL that contains information that indicates the location of the file and how the browser should handle it. The URL may contain the location of the resource on the remote server, the path to the file on the local disk, or even any piece of encoded data. 2.NSURLNSURL is actually the website address that we see in the browser, this is not a string, why still write a nsurl? Mainly because the site address of the string are more complex, including a lot of request parameters, so in the request process needs to parse out each department, so encapsulation a nsurl, the operation is very convenient. 3. Uses (1) URL objects can be used to construct URLs and access their parts. For example, [Myurl scheme] (2) for URLs that represent local files, you can also manipulate the properties of these files directly. For example, modify the last modified date of the file. (3) You can use URLs for network communication. For example, you can use Nsurlsession nsurlconnection, and the Nsurldownload class to access the contents of a remote resource. (4) You can use the URL to read and write local files. For example, you can call the Stringwithcontentsofurl method through the URL of a local file to get the file contents in nsstring format. (5) You can use the URL to communicate. For example, you can use the OpenURL: method to make a call. (6) You can add tags using urls. Example:
Nsurl *url = [Nsurl urlwithstring:@"http://www.baidu.com/s?tn=baiduhome_pg&bs=nsrul&f=8&rsv_bp=1&rsv_spt=1&wd=nsurl& inputt=2709"]; NSLog (@"Scheme:%@", [URL scheme]); NSLog (@"Host:%@", [url host]); NSLog (@"Port:%@", [url port]); NSLog (@"Path:%@", [url path]); NSLog (@"Relative Path:%@", [url relativepath]); NSLog (@"Path Components as array:%@", [url pathcomponents]); NSLog (@"Parameter string:%@", [url parameterstring]); NSLog (@"Query:%@", [url query]); NSLog (@"Fragment:%@", [url fragment]); NSLog (@"User:%@", [url user]); NSLog (@"Password:%@", [url password]);
Results:
-- A-Ten +: -:57.171[4697:358837] Scheme:http -- A-Ten +: -:57.171[4697:358837] Host:www.baidu.com -- A-Ten +: -:57.172[4697:358837] Port: (NULL) -- A-Ten +: -:57.172[4697:358837] Path:/s -- A-Ten +: -:57.172[4697:358837] Relative Path:/s -- A-Ten +: -:57.172[4697:358837] Path Components asArray: ("/", s) -- A-Ten +: -:57.172[4697:358837] Parameterstring: (NULL) -- A-Ten +: -:57.173[4697:358837] Query:tn=baiduhome_pg&bs=nsrul&f=8&rsv_bp=1&rsv_spt=1&wd=nsurl&inputt=2709 -- A-Ten +: -:57.173[4697:358837] Fragment: (NULL) -- A-Ten +: -:57.173[4697:358837] User: (NULL) -- A-Ten +: -:57.173[4697:358837] Password: (NULL)
Basic operation of Nsurl in iOS development