Basic operation and usage of Nsurl in IOS development _ios

Source: Internet
Author: User

Nsurl is actually the website address that we see in the browser, this is not a string, why also write a nsurl, mainly because the website address of the string is more complex, including a lot of request parameters, so in the request process needs to parse out each department, so encapsulate a nsurl, The operation is very convenient.

1.URL

URLs are a concise representation of the location and access to resources available on the Internet and are the addresses of standard resources on the Internet. Each file on the Internet has a unique URL that contains information about 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 of the file on the local disk, or even any piece of encoded data.

2.NSURL

Nsurl is actually the website address that we see in the browser, this is not a string, why still write a nsurl?

Mainly because the website address of the string is more complex, including a lot of request parameters, so in the request process needs to parse out each department, so the encapsulation of a nsurl, easy to operate.

3. Use

(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 date the file was last modified.
(3) You can use a URL for network communication. For example, you can use the Nsurlsession nsurlconnection, and the Nsurldownload class to access the contents of a remote resource.
(4) You can read and write local files using URLs. For example, you can use the URL of a local file to invoke the Stringwithcontentsofurl method to get the contents of the file in the NSString format.
(5) The URL can be used for communication. For example: You can use the OpenURL: method to make a phone call.
(6) You can add a label using a URL.

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

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 [4,697:358,837] 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)

The use of Ps:nsurl

1:nsurl Initialization method:

 
 

2: Resolves the Nsurl initialization failure method.

The nsstring will be passed in UTF8 transcoding.

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

3:nsurl parameters that can be obtained after successful initialization (excerpted 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]); 

The results are as follows:

 2012-03-31 18:22:20.904 smalldemolist[5473:11603] 12131232 2012-03-31 18:22:20.907 Sma lldemolist[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 Compone NTS 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 to the package content file based on the file name and file suffix

 
 

Urlforresource: File name
Withextension: File suffix
subdirectory: in which subdirectory of the package to look for.

If not found, it will return to nil
Once found, return to the following path: file://localhost/Users/amarishuyi/Library/Application%20Support/iPhone%20Simulator/5.1/Applications/ Fb0cdabc-d0e2-45ff-aa2c-959e8a65adb4/smalldemolist.app/kindeditor/examples/simple.html

The above content is small to share the iOS development in the basic operation and usage of nsurl, I hope you like.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.