Basic NSURL operations and basic iosnsurl operations in iOS development
1. URLURL is a simple representation of the location and access method of resources that can be obtained from the Internet. It is the address of standard resources on the Internet. Each file on the Internet has a unique URL. It contains information that specifies the location of the file and how the browser should handle it. The URL may contain the location of resources on the remote server, the path of files on the local disk, or even any encoded data. 2. NSURLNSURL is actually the website address we see in the browser. Isn't it a string? Why do we need to write an NSURL? The main reason is that the website address strings are complex and contain many Request Parameters. In this way, each department needs to be parsed during the request process. Therefore, it is convenient to encapsulate an NSURL. 3. Purpose (1) You can use a URL object to construct a URL and access its part. For example, [myURL scheme] (2) for URLs that represent local files, you can also directly operate on the attributes of these files. For example, modify the last modification date of a file. (3) You can use URLs for network communication. For example, you can use the NSURLSession NSURLConnection and NSURLDownload classes to access the content of remote resources. (4) you can use a url to read and write local files. For example, you can use the URL of a local file to call the stringWithContentsOfURL method to obtain the file content in NSString format. (5) You can use URLs for communication. For example, you can use openURL: 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]);
Result:
2015-12-10 21:53:57.171 [4697:358837] Scheme: http2015-12-10 21:53:57.171 [4697:358837] Host: www.baidu.com2015-12-10 21:53:57.172 [4697:358837] Port: (null)2015-12-10 21:53:57.172 [4697:358837] Path: /s2015-12-10 21:53:57.172 [4697:358837] Relative path: /s2015-12-10 21:53:57.172 [4697:358837] Path components as array: ( "/", s)2015-12-10 21:53:57.172 [4697:358837] Parameter string: (null)2015-12-10 21:53:57.173 [4697:358837] Query: tn=baiduhome_pg&bs=NSRUL&f=8&rsv_bp=1&rsv_spt=1&wd=NSurl&inputT=27092015-12-10 21:53:57.173 [4697:358837] Fragment: (null)2015-12-10 21:53:57.173 [4697:358837] User: (null)2015-12-10 21:53:57.173 [4697:358837] Password: (null)