SoFunction
Updated on 2025-04-14

Detailed explanation of the basic operations and usage of NSURL in IOS development

NSURL is actually the website address we see on the browser. Isn’t this just a string? Why do you still need to write an NSURL? It’s mainly because the strings of the website address are relatively complex, including many request parameters. In this way, each department needs to be parsed during the request process, so encapsulating an NSURL is very convenient to operate.


A URL is a concise representation of the location and access methods of resources that can be obtained from the Internet, and is the address of a standard resource on the Internet. Every file on the internet has a unique URL that contains information that points out 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.


NSURL is actually the website address we see on the browser. Isn’t this just a string? Why do you still write an NSURL?

The main reason is that the strings of the website address are relatively complex, including many request parameters, so each department needs to be parsed during the request process, so encapsulating an NSURL is very convenient to operate.

3. Usage

(1) You can use URL objects to construct URLs and access their parts. For example, [myURL scheme]
(2) For urls representing local files, you can also directly manipulate the properties of these files. For example, modify the last modification date of a file.
(3) You can use url for network communication. For example, you can use the NSURLSession NSURLConnection, and the NSURLDownload classes to access the contents of the remote resource.
(4) You can use url to read and write local files. For example, you can use a local file URL to call the stringWithContentsOfURL method to get the file content in NSString format.
(5) You can use url for communication. For example: You can use the openURL: method to make calls.
(6) You can use url to add tags.

For example:

NSURL *url = [NSURL URLWithString:@"/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: http
2015-12-10 21:53:57.171 [4697:358837] Host: 
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: Usage of NSURL

1: NSURL initialization method:

NSURL *url=[NSURL URLWithString:@"?id=1"]; 

2: Method to solve the failure of NSURL initialization.

Just use UTF8 transcoding of the NSString passed in.

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

3: Parameters that can be obtained after NSURL is successfully initialized (excerpted from: NSURL learning)

NSURL *url = [NSURL URLWithString: @"/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:  
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: Obtain the path of the package content file based on the file name and file suffix

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

URLForResource: File name
withExtension: File suffix
subdirectory: Looking for it in which subdirectory in the package.

If not found, nil will be returned
After finding it, return the following path:file://localhost/Users/amarishuyi/Library/Application%20Support/iPhone%20Simulator/5.1/Applications/FB0CDABC-D0E2-45FF-AA2C-959E8A65ADB4//KindEditor/examples/

The above content is a detailed explanation of the basic operations and usage of NSURL in IOS development shared by the editor. I hope you like it.