Objective-C 语法之 NSURL
2015-06-19 23:37
337 查看
有时我们需要获取请求地址的相关信息,这时我们就可以用 NSURL 的一些方法操作来获取它。
需要注意的一点是:请求地址里可能存在特殊字符或中文,为了正确获取信息,建议使用 stringByAddingPercentEscapesUsingEncoding: 对字符串进行编码,然后对应的使用 stringByReplacingPercentEscapesUsingEncoding: 对字符串进行解码。
main.m
结果:
需要注意的一点是:请求地址里可能存在特殊字符或中文,为了正确获取信息,建议使用 stringByAddingPercentEscapesUsingEncoding: 对字符串进行编码,然后对应的使用 stringByReplacingPercentEscapesUsingEncoding: 对字符串进行解码。
main.m
#import <Foundation/Foundation.h> void testURL(NSString *strUrl) { strUrl = [strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]; //对字符串进行编码 NSURL *url = [NSURL URLWithString:strUrl]; NSMutableString *mStrInfo = [[NSMutableString alloc] initWithString:@"URL相关信息如下:\n"]; [mStrInfo appendFormat:@"absoluteString: %@\n", [url absoluteString]]; [mStrInfo appendFormat:@"relativeString: %@\n", [url relativeString]]; [mStrInfo appendFormat:@"baseURL: %@\n", [url baseURL]]; [mStrInfo appendFormat:@"absoluteURL: %@\n", [url absoluteURL]]; [mStrInfo appendFormat:@"scheme: %@\n", [url scheme]]; [mStrInfo appendFormat:@"resourceSpecifier: %@\n", [url resourceSpecifier]]; [mStrInfo appendFormat:@"host: %@\n", [url host]]; [mStrInfo appendFormat:@"port: %@\n", [url port]]; [mStrInfo appendFormat:@"user: %@\n", [url user]]; [mStrInfo appendFormat:@"password: %@\n", [url password]]; [mStrInfo appendFormat:@"path: %@\n", [url path]]; [mStrInfo appendFormat:@"fragment: %@\n", [url fragment]]; //编码后的字符串,获取到的值为null,而不是正常情况的a=1;最终a=1被作为query的值内容了 [mStrInfo appendFormat:@"parameterString: %@\n", [url parameterString]]; [mStrInfo appendFormat:@"query: %@\n", [url query]]; [mStrInfo appendFormat:@"relativePath: %@\n", [url relativePath]]; //如果baseURL是nil,relativePath就跟path一样 [mStrInfo appendFormat:@"pathComponents: %@\n", [url pathComponents]]; NSLog(@"%@", [mStrInfo stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]); //对字符串进行解码 } int main(int argc, const char * argv[]) { @autoreleasepool { NSString *strUrl = @"http://www.cnblogs.com:80/huangjianwu/archive/2015/06.html?author=KenmuHuang黄&page=1#a=1"; testURL(strUrl); } return 0; }
结果:
2015-06-19 23:17:29.496 OCNSURL[768:34254] URL相关信息如下: absoluteString: http://www.cnblogs.com:80/huangjianwu/archive/2015/06.html?author=KenmuHuang黄&page=1#a=1 relativeString: http://www.cnblogs.com:80/huangjianwu/archive/2015/06.html?author=KenmuHuang黄&page=1#a=1 baseURL: (null) absoluteURL: http://www.cnblogs.com:80/huangjianwu/archive/2015/06.html?author=KenmuHuang黄&page=1#a=1 scheme: http resourceSpecifier: //www.cnblogs.com:80/huangjianwu/archive/2015/06.html?author=KenmuHuang黄&page=1#a=1 host: www.cnblogs.com port: 80 user: (null) password: (null) path: /huangjianwu/archive/2015/06.html fragment: (null) parameterString: (null) query: author=KenmuHuang黄&page=1#a=1 relativePath: /huangjianwu/archive/2015/06.html pathComponents: ( "/", huangjianwu, archive, 2015, "06.html" )
相关文章推荐
- objective C using a string to call a method
- [Object-c] 在ios程序中自动滚动TableView到某行的方法
- JMX之ObjectName
- \'RegistrationForm\' object has no attribute \'clean_data 语法变更
- Objective-C----继承
- Could not reserve enough space for object heap解决办法
- Objective-C程序设计第三章:类,对象和方法
- Objective-C----@class 和 #import
- Objective-C----初识Objective-C
- Objective-C中一个方法如何传递多个参数的理解
- Objective-c中的相对路径和绝对路径
- <NSCoding>存储数据 archivedDataWithRootObject unarchiveObjectWithData
- 操纵JsonObject的方法
- Objective-C----防御式编程
- 【iOS开发之Objective-C】新建第一个工程-HelloWorld
- Objective-C----setter 和 getter
- CocoaPods一个Objective-C第三方库的管理利器
- Objective-C----NSLog( )
- Objective-C 2.0的运行时编程
- objective-c中的method swizz实现"猴打补丁"