Nsurl is actually what we see in the browser website address, this is not a string, why also write a nsurl it, mainly because the web address of the string is more complex, including a lot of request parameters, so in the request process need to parse out each department, so encapsulate a nsurl, The operation is very convenient:
- 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)
1:nsurl Initialization method:
- Nsurl *url=[nsurl urlwithstring:@ "http://www.baidu.com?id=1"];
2: Resolve Nsurl initialization failure related solution.
Will pass in the NSString carries on the UTF8 transcoding can.
1: Solution for URLWithString initialization failure
- 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];
2: Solution for Fileurlwithpath initialization failure
- self.filepathstring = [self.filepathstring stringbyreplacingpercentescapesusingencoding:nsutf8stringencoding];
- Nsurl *url = [Nsurl fileURLWithPath:self.filePathString];
A successful transcoding automatically adds "file:///" to the left side of the string
3:nsurl parameters that can be obtained after successful initialization (excerpt: 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: (
- "/",
- S
- )
- 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: Gets the path to 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: Find in which subdirectory in the package.
Nil will be returned if not found
When found, return to the following path: File://localhost/Users/amarishuyi/Library/Application support/iphone simulator/5.1/applications/ Fb0cdabc-d0e2-45ff-aa2c-959e8a65adb4/smalldemolist.app/kindeditor/examples/simple.html
IOS Nsurl Basic Operations-ready