ios开发url php,IOS_IOS开发中NSURL的基本操作及用法详解,NSURL其实就是我们在浏览器上 - phpStudy...

IOS开发中NSURL的基本操作及用法详解

NSURL其实就是我们在浏览器上看到的网站地址,这不就是一个字符串么,为什么还要在写一个NSURL呢,主要是因为网站地址的字符串都比较复杂,包括很多请求参数,这样在请求过程中需要解析出来每个部门,所以封装一个NSURL,操作很方便。

1.URL

URL是对可以从互联网上得到的资源的位置和访问方法的一种简洁的表示,是互联网上标准资源的地址。互联网上的每个文件都有一个唯一的URL,它包含的信息指出文件的位置以及浏览器应该怎么处理它。

URL可能包含远程服务器上的资源的位置,本地磁盘上的文件的路径,甚至任意一段编码的数据。

2.NSURL

NSURL其实就是我们在浏览器上看到的网站地址,这不就是一个字符串么,为什么还要在写一个NSURL呢?

主要是因为网站地址的字符串都比较复杂,包括很多请求参数,这样在请求过程中需要解析出来每个部门,所以封装一个NSURL,操作很方便。

3.用途

(1)可以使用URL对象构造URL和访问他们的部分。例如,[myURL scheme]

(2)对于代表本地文件的url,您也可以直接操作这些文件的属性。例如,修改文件的最后修改日期。

(3)可以使用url进行网络通信。例如,您可以使用NSURLSession NSURLConnection,和NSURLDownload类来访问远程资源的内容。

(4)可以使用url读写本地文件。例如,你可以通过一个本地文件的URL,调用stringWithContentsOfURL方法,得到NSString格式的文件内容。

(5)可以使用url进行通讯。例如:可以用openURL:方法来拨打电话。

(6)可以使用url添加标签。

举例:

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

结果:

2015-12-10 21:53:57.171 [4697:358837] Scheme: http

2015-12-10 21:53:57.171 [4697:358837] Host: www.baidu.com

2015-12-10 21:53:57.172 [4697:358837] Port: (null)

2015-12-10 21:53:57.172 [4697:358837] Path: /s

2015-12-10 21:53:57.172 [4697:358837] Relative path: /s

2015-12-10 21:53:57.172 [4697:358837] Path components as array: (

"/",

)

2015-12-10 21:53:57.172 [4697:358837] Parameter string: (null)

2015-12-10 21:53:57.173 [4697:358837] Query: tn=baiduhome_pg&bs=NSRUL&f=8&rsv_bp=1&rsv_spt=1&wd=NSurl&inputT=2709

2015-12-10 21:53:57.173 [4697:358837] Fragment: (null)

2015-12-10 21:53:57.173 [4697:358837] User: (null)

2015-12-10 21:53:57.173 [4697:358837] Password: (null)

ps:NSURL的用法

1:NSURL初始化方法:

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

2:解决NSURL初始化失败的方法.

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

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

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: (

"/",

)

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

以上内容是小编给大家分享的IOS开发中NSURL的基本操作及用法详解,希望大家喜欢。相关阅读:

JS控制表单提交的方法

jquery实现弹出层完美居中效果

同步文本框内容JS代码实现

Node.js入门教程:在windows和Linux上安装配置Node.js图文教程

C++判断pe文件实例

JSON无限折叠菜单编写实例

javascript Array.prototype.slice的使用示例

深入学习C语言中memset()函数的用法

Win8关闭游戏时提示虚拟内存不足是什么原因如何解决

jquery easyui中treegrid用法的简单实例

php实现文件下载(支持中文文名)

SQL Server中删除重复数据的几个方法

asp.net连接数据库读取数据示例分享

winXP系统电脑打印文字字体出现乱码的解决办法

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
iOS开发上传图片可以采用以下步骤: 1.选择要上传的图片,可以使用系统提供的UIImagePickerController控制器,或者使用第三方库,例如TZImagePickerController。 2.将选的图片转换为NSData格式。 3.使用NSURLSession或AFNetworking等网络库,将图片数据上传到服务器。 以下是一个简单的上传图片的示例代码: ``` // 选择图片 UIImagePickerController *imagePicker = [[UIImagePickerController alloc] init]; imagePicker.sourceType = UIImagePickerControllerSourceTypePhotoLibrary; imagePicker.delegate = self; [self presentViewController:imagePicker animated:YES completion:nil]; // 将选的图片转换为NSData格式 - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<UIImagePickerControllerInfoKey,id> *)info { UIImage *selectedImage = info[UIImagePickerControllerOriginalImage]; NSData *imageData = UIImageJPEGRepresentation(selectedImage, 0.5); // 上传图片到服务器 NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration]; NSURL *url = [NSURL URLWithString:@"http://example.com/upload.php"]; NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url]; request.HTTPMethod = @"POST"; NSURLSessionUploadTask *uploadTask = [session uploadTaskWithRequest:request fromData:imageData completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { // 处理服务器返回的响应 }]; [uploadTask resume]; [picker dismissViewControllerAnimated:YES completion:nil]; } ``` 其,upload.php是服务器端接收图片的脚本文件。在服务器端,可以使用PHP等语言来处理上传的图片数据。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值