NSURL Basic Operation of IOS Development

  • 2021-12-09 10:17:45
  • OfStack

NSURL Basic Operation of IOS Development

NSURL is actually the website address we see on the browser. Isn't this a string? Why write an NSURL? The main reason is that the string of website address is complex, including many request parameters, so it is necessary to resolve each department during the request process, so it is very convenient to encapsulate an NSURL:


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:


2012-08-29 15:52:23.781 NSurl[3560:f803] Scheme: http 
2012-08-29 15:52:32.793 NSurl[3560:f803] Host: www.baidu.com 
2012-08-29 15:52:39.102 NSurl[3560:f803] Port: (null) 
2012-08-29 15:52:42.590 NSurl[3560:f803] Path: /s 
2012-08-29 15:52:52.516 NSurl[3560:f803] Relative path: /s 
2012-08-29 15:53:05.576 NSurl[3560:f803] Path components as array: ( 
  "/", 
  s 
) 
2012-08-29 15:53:32.861 NSurl[3560:f803] Parameter string: (null) 
2012-08-29 15:53:37.528 NSurl[3560:f803] Query: tn=baiduhome_pg&bs=NSRUL&f=8&rsv_bp=1&rsv_spt=1&wd=NSurl&inputT=2709 
2012-08-29 15:53:52.942 NSurl[3560:f803] Fragment: (null) 
2012-08-29 15:53:54.539 NSurl[3560:f803] User: (null) 
2012-08-29 15:53:57.808 NSurl[3560:f803] Password: (null) 

Thank you for reading, hope to help everyone, thank you for your support to this site!


Related articles: