IOS Nsurl Basic Operations-ready

Source: Internet
Author: User

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:

  1. 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 "];
  2. NSLog (@"scheme:%@", [url scheme]);
  3. NSLog (@"host:%@", [url host]);
  4. NSLog (@"Port:%@", [url port]);
  5. NSLog (@"path:%@", [url path]);
  6. NSLog (@"Relative path:%@", [url relativepath]);
  7. NSLog (@"Path components as array:%@", [url pathcomponents]);
  8. NSLog (@"Parameter string:%@", [url parameterstring]);
  9. NSLog (@"query:%@", [url query]);
  10. NSLog (@"Fragment:%@", [url Fragment]);
  11. NSLog (@"User:%@", [url user]);
  12. NSLog (@"Password:%@", [url Password]);

Results:

    1. 2012-08-29 15:52:23.781 nsurl[3560:f803] Scheme:http
    2. 2012-08-29 15:52:32.793 nsurl[3560:f803] Host:www.baidu.com
    3. 2012-08-29 15:52:39.102 nsurl[3560:f803] Port: (NULL)
    4. 2012-08-29 15:52:42.590 nsurl[3560:f803] Path:/s
    5. 2012-08-29 15:52:52.516 nsurl[3560:f803] Relative path:/s
    6. 2012-08-29 15:53:05.576 nsurl[3560:f803] Path components as array: (
    7. "/",  
    8. S
    9. )
    10. 2012-08-29 15:53:32.861 nsurl[3560:f803] Parameter string: (NULL)
    11. 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
    12. 2012-08-29 15:53:52.942 nsurl[3560:f803] Fragment: (NULL)
    13. 2012-08-29 15:53:54.539 nsurl[3560:f803] User: (NULL)
    14. 2012-08-29 15:53:57.808 nsurl[3560:f803] Password: (NULL)

1:nsurl Initialization method:


    1. 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

    1. NSString *strlocalhtml = @ "File:///Users/amarishuyi/Desktop/My IPhone life/webdeveloper/webplug-in/exteditor/ Datapage/kmqt/ext-htmleditor.html ";
    2. strlocalhtml = [NSString stringwithformat:@ "%@? value=%@ ", Strlocalhtml,self.txturl.text];
    3. strlocalhtml= [strlocalhtml stringbyaddingpercentescapesusingencoding:nsutf8stringencoding];
    4. Nsurl * Url=[nsurl urlwithstring:strlocalhtml];

2: Solution for Fileurlwithpath initialization failure

    1. self.filepathstring = [self.filepathstring stringbyreplacingpercentescapesusingencoding:nsutf8stringencoding];
    2. 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)

  1. 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 "];
  2. NSLog (@ "scheme:%@", [url scheme]);
  3. NSLog (@ "Host:%@", [url host]);
  4. NSLog (@ "Port:%@", [url port]);
  5. NSLog (@ "Path:%@", [url path]);
  6. NSLog (@ "Relative path:%@", [url relativepath]);
  7. NSLog (@ "Path components as array:%@", [url pathcomponents]);
  8. NSLog (@ "Parameter string:%@", [url parameterstring]);
  9. NSLog (@ "Query:%@", [url query]);
  10. NSLog (@ "Fragment:%@", [url Fragment]);
  11. NSLog (@ "User:%@", [url user]);
  12. NSLog (@ "Password:%@", [url Password]);

The results are as follows:

  1. 2012-03-31 18:22:20.904 smalldemolist[5473:11603] 12131232
  2. 2012-03-31 18:22:20.907 smalldemolist[5473:11603] Scheme:http
  3. 2012-03-31 18:22:20.907 smalldemolist[5473:11603] Host:www.baidu.com
  4. 2012-03-31 18:22:20.907 smalldemolist[5473:11603] Port: (NULL)
  5. 2012-03-31 18:22:20.907 smalldemolist[5473:11603] Path:/s
  6. 2012-03-31 18:22:20.907 smalldemolist[5473:11603] Relative path:/s
  7. 2012-03-31 18:22:20.907 smalldemolist[5473:11603] Path components as array: (
  8. "/",
  9. S
  10. )
  11. 2012-03-31 18:22:20.916 smalldemolist[5473:11603] Parameter string: (NULL)
  12. 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
  13. 2012-03-31 18:22:20.917 smalldemolist[5473:11603] Fragment: (NULL)
  14. 2012-03-31 18:22:20.917 smalldemolist[5473:11603] User: (NULL)
  15. 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

Related Article

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.