iOS官方文档 Foundation篇---URLs

2019-01-28  本文已影响9人  qianfei

人生就像品茗,懂得吃苦,才能回甘啊。

————鷇音子

NSURL

表示资源位置的对象,例如远程服务器上的项目或本地文件的路径;

NSBURL结构

一个NSURL对象是由两个部分组成,一个是基础URL和相对于基础URL解析的字符串。如果NSURL对象的字符串部分在没有基础URL的情况下完全解析,则该对象所有其他URL都被认为是相对的。

NSURL *baseURL = [NSURL fileURLWithPath:@"file:///path/to/user/"];
NSURL *URL = [NSURL URLWithString:@"folder/file.html" relativeToURL:baseURL];
NSLog(@"absoluteURL = %@", [URL absoluteURL]); // file:///path/to/user/folder/file.html

完整url的构成:
https://johnny:p4ssw0rd@www.example.com:443/script.ext;param=value?query=value#ref

scheme user password host port path pathExtension pathComponents parameterString query fragment
https johnny p4ssw0rd www.example.com 443 /script.ext ext ["/", "script.ext"] param=value query=value ref

– absoluteString – absoluteURL – baseURL – fileSystemRepresentation – fragment – host –lastPathComponent – parameterString – password – path – pathComponents – pathExtension – port– query – relativePath – relativeString – resourceSpecifier – scheme – standardizedURL – user

创建NSURL对象
//创建并返回使用提供的URL字符串初始化的NSURL对象
+ (nullable instancetype)URLWithString:(NSString *)URLString;

//创建并返回使用BaseURL和URLString初始化的NSURL对象
+ (nullable instancetype)URLWithString:(NSString *)URLString relativeToURL:(nullable NSURL *)baseURL;

//初始化并将新创建的NSURL对象作为具有指定路径的文件URL返回
+ (NSURL *)fileURLWithPath:(NSString *)path;

/**
 初始化并将新创建的NSURL对象作为具有指定路径的文件URL返回
 
 @param path:有效的系统路径
 @param isDir :YES:path表示目录,NO:path表示文件。

 @return NSURL  路径
 */
+ (NSURL *)fileURLWithPath:(NSString *)path isDirectory:(BOOL)isDir;

+ (NSURL *)fileURLWithPath:(NSString *)path relativeToURL:(nullable NSURL *)baseURL;

+ (NSURL *)fileURLWithPath:(NSString *)path isDirectory:(BOOL) isDir relativeToURL:(nullable NSURL *)baseURL;


- (nullable instancetype)initWithString:(NSString *)URLString;
- (nullable instancetype)initWithString:(NSString *)URLString relativeToURL:(nullable NSURL *)baseURL;
- (instancetype)initFileURLWithPath:(NSString *)path;
- (instancetype)initFileURLWithPath:(NSString *)path isDirectory:(BOOL)isDir;
- (instancetype)initFileURLWithPath:(NSString *)path relativeToURL:(nullable NSURL *)baseURL; 
- (instancetype)initFileURLWithPath:(NSString *)path isDirectory:(BOOL)isDir relativeToURL:(nullable NSURL *)baseURL;
NSString *filePath = @"/Users/calabashBoy/Desktop/name.plist";
NSString *fileURL = @"file:///Users/calabashBoy/Desktop/name.plist";
NSURL *url = [NSURL URLWithString: fileURL];//    url = file:///Users/calabashBoy/Desktop/name.plist
NSURL *url = [NSURL URLWithString:filePath];// url = /Users/calabashBoy/Desktop/name.plist
NSURL *url = [NSURL fileURLWithPath:fileURL];// url = file:///Users/calabashBoy/Desktop/name.plist
NSURL *url = [NSURL fileURLWithPath:filePath];// url = file:///Users/calabashBoy/Desktop/name.plist
NSURL *url = [NSURL fileURLWithPath:filePath isDirectory:NO];

BOOL isEqual = [url isEqual:url];//判断两个url是否相同
BOOL isFileURL = [url9 isFileURL];//是否是文件URL
BOOL isFileReferenceURL = [url9 isFileReferenceURL]//是否是文件引用URL
BOOL isFileReferenceURL = [url9 checkResourceIsReachableAndReturnError:nil];//是否可以访问文件URL指向的资源
访问URL的各部分
NSURL *url = [NSURL URLWithString:@"https://johnny:p4ssw0rd@www.example.com:443/script.ext;param=value?query=value#ref"];
NSString *absoluteStr = url.absoluteString;
NSURL *absoluteUrl = url.absoluteURL;
NSURL *baseUrl = url.baseURL;
//包含URL的文件系统路径字符串
const char *fileSystemRepresentation = url9.fileSystemRepresentation;// "/script.ext"
//标识符
NSString *fragment = url.fragment;//ref
//主机
NSString *host = url.host;// www.example.com
//最后一个路径组件
NSString *lastPathComponent = url.lastPathComponent;//script.ext
//参数字符串
NSString *parameterString = url.parameterString;//param=value
//密码
NSString *password = url.password;//p4ssw0rd
//路径
NSString *path = url.path;//    /script.ext
//路径组件数组
NSArray *pathComponents = url.pathComponents;// (/,script.ext)
//路径扩展
NSString *pathExtension = url.pathExtension;//ext
//端口
NSNumber *port = url.port;//443
//查询字符串
NSString *query = url.query;//query=value
//相对路径
NSString *relativePath = url.relativePath;///script.ext
//URL的相对部分的字符串
NSString *relativeString = url.relativeString;
//资源说明符
NSString *resourceSpecifier = url.resourceSpecifier;//   //johnny:p4ssw0rd@www.example.com:443/script.ext;param=value?query=value#ref
NSString *scheme = url.scheme;//https
NSURL *standardizedURL = url.standardizedURL;
//用户名
NSString *user = url.user;//johnny
修改URL

//添加路径组件
- (nullable NSURL *)URLByAppendingPathComponent:(NSString *)pathComponent;

//添加路径组件,组件指定目录时使用尾部斜杠
- (nullable NSURL *)URLByAppendingPathComponent:(NSString *)pathComponent isDirectory:(BOOL)isDirectory;

//添加路径扩展名
- (nullable NSURL *)URLByAppendingPathExtension:(NSString *)pathExtension;

//删除最后一个路径组件
@property (nullable, readonly, copy) NSURL *URLByDeletingLastPathComponent;

//删除路径扩展
@property (nullable, readonly, copy) NSURL *URLByDeletingPathExtension;

//删除所有缓存的资源值和临时资源值。
- (void)removeAllCachedResourceValues;

NSURL *url = [NSURL URLWithString:@"https://johnny:p4ssw0rd@www.example.com:443/script.ext;param=value?query=value#ref"];
NSURL *filePathURL = [url filePathURL];
NSURL *fileReferenceURL = [url fileReferenceURL];

// 添加路径组件
// https://ajohnny:p4ssw0rd@www.example.com:443/script.ext/com;param=value?query=value#ref
NSURL *appendPathComponentURL = [url URLByAppendingPathComponent:@"com"];

// 添加路径组件,组件指定目录时使用尾部斜杠
// https://ajohnny:p4ssw0rd@www.example.com:443/script.ext/com/;param=value?query=value#ref
NSURL * appendPathComponentISDirURL = [url URLByAppendingPathComponent:@"com" isDirectory:YES];

// 添加路径扩展名
// https://ajohnny:p4ssw0rd@www.example.com:443/script.ext.text;param=value?query=value#ref
NSURL *appendPathExtURL = [url URLByAppendingPathExtension:@"text"];

// 删除最后一个路径组件
// https://ajohnny:p4ssw0rd@www.example.com:443/;param=value?query=value#ref
NSURL *deletLastPathComponentURL= [url URLByDeletingLastPathComponent];

// 删除路径扩展
// https://ajohnny:p4ssw0rd@www.example.com:443/script;param=value?query=value#ref
NSURL *deletePathExtensionURL = [url URLByDeletingPathExtension];

// 使用绝对路径指向与原始URL相同的资源的URL
// https://ajohnny:p4ssw0rd@www.example.com:443/script.ext;param=value?query=value#ref
NSURL *standardizingPathURL = [url URLByStandardizingPath];

//删除所有缓存
[url removeAllCachedResourceValues];

NSURLComponents

NSURLComponents 是一个用于解析和构建 URL 的类,
通过 NSURLComponents 可以单独修改 URL 的某一部分,获取某一部分编码后的值;

// 通过传入的 URLString 创建
+ (nullable instancetype)componentsWithString:(NSString *)URLString;

//通过传入的 url 创建,resolve 是否解析 url 的 baseURL
+ (nullable instancetype)componentsWithURL:(NSURL *)url resolvingAgainstBaseURL:(BOOL)resolve;

- (nullable instancetype)initWithString:(NSString *)URLString;
- (nullable instancetype)initWithURL:(NSURL *)url resolvingAgainstBaseURL:(BOOL)resolve;

NSURLQueryItem

URL查询URL的单个名称/值对;

创建NSURLQueryItem
- (instancetype)initWithName:(NSString *)name value:(nullable NSString *)value;
+ (instancetype)queryItemWithName:(NSString *)name value:(nullable NSString *)value;
NSURLQueryItem *item = [NSURLQueryItem queryItemWithName:@"key" value:@"value"];
item.name;//key
item.value;//value

非常优秀的简书内容:
https://www.jianshu.com/p/38f5f53dfbad

欢迎留言指正,会持续更新。。。
上一篇下一篇

猜你喜欢

热点阅读