Basic operation and usage of NSURL in IOS development

  • 2020-05-13 03:29:31
  • OfStack

NSURL is actually we see website address on the browser, is this a string, why even in writing a NSURL, mainly because the website address of the string are complex, including a lot of request parameters, so need to parse out each department in the process of the request, so 1 NSURL encapsulation, the operation is very convenient.

1.URL

URL is a concise representation of the location and access methods of resources available on the Internet, and is the address of a standard resource on the Internet. Every file on the Internet has a one-only URL, which contains information about where the file is and what the browser should do with it.

URL may contain the location of a resource on a remote server, the path to a file on a local disk, or even any 1 encoded piece of data.

2.NSURL

NSURL is the address of a web site that you see in your browser. Why write an NSURL?

The main reason is that the website address string is relatively complex, including a lot of request parameters, so in the request process needs to be resolved out of each department, so it is very convenient to package 1 NSURL.

3. Use

(1) you can use URL objects to construct URL and access their parts. For example, [myURL scheme]
(2) for url, which represents local files, you can also directly manipulate the properties of these files. For example, modify the last modified date of a file.
(3) url can be used for network communication. For example, you can use the NSURLSession NSURLConnection, and NSURLDownload classes to access the content of remote resources.
(4) you can read and write local files using url. For example, you can call the stringWithContentsOfURL method from the URL of a local file to get the NSString file content.
(5) url can be used for communication. For example, you can use the openURL: method to make phone calls.
(6) you can use url to add labels.

For 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:


2015-12-10 21:53:57.171 [4697:358837] Scheme: http
2015-12-10 21:53:57.171 [4697:358837] Host: www.baidu.com
2015-12-10 21:53:57.172 [4697:358837] Port: (null)
2015-12-10 21:53:57.172 [4697:358837] Path: /s
2015-12-10 21:53:57.172 [4697:358837] Relative path: /s
2015-12-10 21:53:57.172 [4697:358837] Path components as array: (
 "/",
)
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=2709
2015-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)

ps: the use of NSURL

1:NSURL initialization method:


NSURL *url=[NSURL URLWithString:@"http://www.baidu.com?id=1"]; 

2: resolve the method of NSURL initialization failure.

Transcode the incoming NSString to UTF8.


NSString *strLocalHtml = @"file:///Users/amarishuyi/Desktop/My IPhone Life/WebDeveloper/WebPlug-in/ExtEditor/DataPage/KMQT/Ext-HTMLEditor.html"; 
strLocalHtml = [NSString stringWithFormat:@"%@?Value=%@",strLocalHtml,self.txtUrl.text]; 
strLocalHtml= [strLocalHtml stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; 
NSURL * url=[NSURL URLWithString:strLocalHtml]; 

3: parameters available after successful initialization of NSURL (from :NSURL learning)


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]); 

The results are as follows:


2012-03-31 18:22:20.904 SmallDemoList[5473:11603] 12131232 
2012-03-31 18:22:20.907 SmallDemoList[5473:11603] Scheme: http 
2012-03-31 18:22:20.907 SmallDemoList[5473:11603] Host: www.baidu.com 
2012-03-31 18:22:20.907 SmallDemoList[5473:11603] Port: (null) 
2012-03-31 18:22:20.907 SmallDemoList[5473:11603] Path: /s 
2012-03-31 18:22:20.907 SmallDemoList[5473:11603] Relative path: /s 
2012-03-31 18:22:20.907 SmallDemoList[5473:11603] Path components as array: ( 
 "/", 
) 
2012-03-31 18:22:20.916 SmallDemoList[5473:11603] Parameter string: (null) 
2012-03-31 18:22:20.917 SmallDemoList[5473:11603] Query: tn=baiduhome_pg&bs=NSRUL&f=8&rsv_bp=1&rsv_spt=1&wd=NSurl&inputT=2709 
2012-03-31 18:22:20.917 SmallDemoList[5473:11603] Fragment: (null) 
2012-03-31 18:22:20.917 SmallDemoList[5473:11603] User: (null) 
2012-03-31 18:22:20.917 SmallDemoList[5473:11603] Password: (null)

4: get the path of the package content file according to the file name and file suffix


NSURL *urlKindEditor = [[NSBundlemainBundle] URLForResource:@"simple"withExtension:@"html"subdirectory:@"KindEditor/examples"]; 

URLForResource: file name
withExtension: file suffix
subdirectory: which subdirectory in the package to look for.

If not, nil will be returned
After finding, return to the following path: file: / / localhost/Users/amarishuyi Library/Application % 20 Support/iPhone % 20 Simulator / 5.1 / Applications/FB0CDABC - D0E2-45 FF - AA2C - 959 E8A65ADB4 / SmallDemoList app/ES127E N/examples simple. html

The above content is the basic operation and usage of NSURL in IOS development Shared by this site, I hope you like it.


Related articles: