NSURL其实就是我们在浏览器上看到的网站地址,这不就是一个字符串么,为什么还要在写一个NSURL呢,主要是因为网站地址的字符串都比较复杂,包括很多请求参数,这样在请求过程中需要解析出来每个部门,所以封装一个NSURL,操作很方便。参考链接
简单用法:
NSString *str = @"http://www.jianshu.com";
// NSString转成NSURL
NSURL *URL = [NSURL URLWithString:str];
NSString *scheme = [URL scheme];// 取出协议头
NSLog(@"%@",scheme);
NSString *host = [URL host];// 取出主机地址
NSLog(@"%@",host);
// NSURL转成NSString
NSString *str1 = [URL absoluteString];
NSLog(@"%@",str1);
全部用法:
NSURL *url = [NSURL URLWithString:@"https://www.baidu.com/s?wd=coderzb&rsv_spt=1&rsv_iqid=0x8ec61a4600000289&issp=1&f=8&rsv_bp=0&rsv_idx=2&ie=utf-8&rqlang=&tn=baiduhome_pg&rsv_enter=1&inputT=9743"];
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]);