NSURL

简介: 1:NSURL初始化方法: NSURL *url=[NSURL URLWithString:@"http://www.baidu.com?id=1"]; 2:解决NSURL初始化失败的相关解决方案.

1:NSURL初始化方法:

NSURL *url=[NSURL URLWithString:@"http://www.baidu.com?id=1"];


2:解决NSURL初始化失败的相关解决方案.

将传进来的NSString 进行 UTF8 转码即可.

1:针对 URLWithString 初始化失败的解决方案

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:针对 fileURLWithPath 初始化失败的解决方案

self.filePathString = [self.filePathString stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSURL *url = [NSURL fileURLWithPath:self.filePathString];

转码成功后 会自动 在字符串左侧添加 "file:///"

3:NSURL 成功初始化后可以获取的参数 (摘自:NSURL 学习 )


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]);

结果如下:

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:根据文件名称和文件后缀获取程序包内容文件的路径

NSURL *urlKindEditor = [[NSBundlemainBundle]URLForResource:@"simple"withExtension:@"html"subdirectory:@"KindEditor/examples"]; 


URLForResource:文件名称

withExtension:文件后缀

subdirectory:在程序包中的哪个子目录中寻找. 

如果没有找到将会返回nil

找到后返回如下路径: file://localhost/Users/amarishuyi/Library/Application%20Support/iPhone%20Simulator/5.1/Applications/FB0CDABC-D0E2-45FF-AA2C-959E8A65ADB4/SmallDemoList.app/KindEditor/examples/simple.html

5:对比两个URL 是否相等

[url isEqual:[_audioPlayer url]]





目录
相关文章
|
数据采集 并行计算 JavaScript
实战指南:在 Node.js 中利用多线程提升性能
在 Node.js 的世界中,多线程技术一直是一个受到广泛关注的领域。最初,Node.js 设计为单线程模式。随着技术发展,Node.js 引入了多线程支持,进而利用多核处理器的强大性能,提升了应用性能。接下来的内容将深入探讨 Node.js 如何实现多线程,以及在何种场合应该采用这种技术。
|
负载均衡 Ubuntu 应用服务中间件
|
Linux 数据安全/隐私保护 Windows
更换(Pypi)pip源到国内镜像
pip国内的一些镜像 阿里云 http://mirrors.aliyun.com/pypi/simple/ 中国科技大学 https://pypi.mirrors.
247096 2
|
7月前
|
数据采集 JSON API
小红书笔记详情 API 接口(小红书 API 系列)
小红书作为热门生活方式平台,拥有海量用户生成内容。通过其笔记详情接口,开发者可获取指定笔记的完整内容、作者信息及互动数据(点赞、评论、收藏数等),助力内容分析与市场调研。接口采用HTTP GET请求,需提供笔记ID,响应数据为JSON格式。注意小红书有严格反爬虫机制,建议使用代理IP并控制请求频率。
1226 3
|
开发工具 iOS开发
iOS制作.framework静态库
iOS制作.framework静态库
199 1
|
缓存 监控 安全
云服务器公网流量异常排查指南
云服务器公网流量异常排查指南
888 1
|
机器学习/深度学习 JSON Linux
NSLog日志输出不全问题分析与解决方案
NSLog日志输出不全问题分析与解决方案
440 0
|
测试技术 索引
BackTrader 中文文档(十七)(1)
BackTrader 中文文档(十七)
149 0
mac命令行安装应用
Mac通过命令行安装ipa包到ios手机,可以在终端通过第三方库libimobiledevice安装。libimobiledevice 是一个跨平台的软件库,支持 iPhone, iPod Touch, iPad and Apple TV等设备的通讯协议,不需要越狱。
|
存储 缓存 负载均衡