Swift学习笔记(3)iOS 9 中的网络请求
2016-05-17 18:51
387 查看
Swift学习笔记(3)iOS 9 中的网络请求
目录
Swift学习笔记3iOS 9 中的网络请求目录
编码方法
请求方法
其他修改
完整代码
运行结果
编码方法
在iOS9中,以前常用的stringByAddingPercentEscapesUsingEncoding方法被废除了,取而代之的是stringByAddingPercentEncodingWithAllowedCharacters方法。用法示例:
var strURL=String(format:"http://blog.csdn.net/sps900608") //等价于strURL=strURL.stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding) strURL=strURL.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet(charactersInString:"`#%^{}\"[]|\\<> ").invertedSet)!
此外还可以如下写法:
strURL=strURL.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLFragmentAllowedCharacterSet)!
NSCharacterSet常用的类型有以下:
URLHostAllowedCharacterSet "#%/<>?@\^`{|} URLFragmentAllowedCharacterSet "#%<>[\]^`{|} URLPasswordAllowedCharacterSet "#%/:<>?@[\]^`{|} URLPathAllowedCharacterSet "#%;<>?[\]^`{|} URLQueryAllowedCharacterSet "#%<>[\]^`{|} URLUserAllowedCharacterSet "#%/:<>?@[\]^`
请求方法
在iOS 9中NSURLConnection类被废除,替代者是NSURLSession类使用示例:
let session=NSURLSession.sharedSession() let dataTask=session.dataTaskWithRequest(request) { (data, reponse, error) -> Void in if (error != nil){ NSLog("Error:\(error?.localizedDescription)") } else{ self.webView.loadData(data!, MIMEType: "text/html", textEncodingName: "utf-8", baseURL: url) } } dataTask.resume()
NSURLsessionTask共有3个实体子类,应用于3种不同的场景,分别是NSURLSessionDataTask(数据请求)、NSURLSessionUploadTask(上传)、NSURLSessionDownloadTask(下载),上述代码使用的是NSURLSessionDataTask(数据请求)。
其他修改
在iOS 9中,进行HTTP请求会报以下错误“Transport Security policy requires the use of a secure connection”,苹果官方推荐使用安全性更好的HTTPS协议,如果仍要进行HTTP请求,可以进行以下修改选择info.plist,在Info.plist中添加App Transport Security Settings类型Dictionary。然后在App Transport Security Settings下添加Allow Arbitrary Loads类型Boolean,值设为YES。如下图所示:
完整代码
import UIKit class ViewController: UIViewController { @IBOutlet var webView: UIWebView! override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. startRequest() } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() // Dispose of any resources that can be recreated. } func startRequest(){ var strURL=String(format:"http://blog.csdn.net/sps900608") strURL=strURL.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet(charactersInString:"`#%^{}\"[]|\\<> ").invertedSet)! let url=NSURL(string: strURL)! let request=NSURLRequest(URL: url) let session=NSURLSession.sharedSession() let dataTask=session.dataTaskWithRequest(request) { (data, reponse, error) -> Void in if (error != nil){ NSLog("Error:\(error?.localizedDescription)") } else{ self.webView.loadData(data!, MIMEType: "text/html", textEncodingName: "utf-8", baseURL:url) } } dataTask.resume() } }
运行结果
相关文章推荐
- Linux网络编程--进程间通信(一)
- tcpdump 结合wireshark
- LoadImage一个从网络访问图片,并存到缓存(内存,磁盘)直接可以用
- HttpURLConnection发送Get和Post请求
- java HttpClient 获取页面Cookie信息
- [转]TCP 的那些事儿(下)
- [转]TCP 的那些事儿(上)
- HTTP发送POST请求说明
- [译文]用神经网络实现能够自主避让障碍的生物
- 递归神经网络不可思议的有效性
- TCP/IP详解,卷1:协议 (Week 1)
- UNIX网络编程:共享内存区
- ios开发——代码实现获得手机系统的名字和版本号&当前网络状态
- Overlay网络与物理网络的关系
- https、SSL与数字证书介绍
- uploadify上传文件出现http302错误
- TCP 与 UDP的区别
- [原创]java WEB学习笔记07:关于HTTP协议
- 【HDU4859】 海岸线(网络流-最小割)
- 网络视频监控系统开发系列-----BMP文件头格式