居然会自动解码,要不要这么智能,弄了快一个上午,傻逼了。。。。
1、如果不能自动解析的话,就得用http://stackoverflow.com/questions/29694050/gzip-usage-in-swift
using thisNSData+GZIP.swiftlibrary to zip/unzip data from server and here is process how to use it:
- Add
NSData+GZIP.swift
file to your project. - Add
libz.dylib
library to your project. - Add a line
#include <zlib.h>
to yourProjectName-Bridging-Header.h
file. - Invoke from your Swift/ObjC files.
2、如果你和我情况一样,加一行代码就搞定了:
request.addValue("Accept-Encoding",forHTTPHeaderField:"gzip")
其他系统会自动解析,以前怎么用,现在也怎么用。
例如:
let request :NSMutableURLRequest = NSMutableURLRequest(URL: URL,cachePolicy: NSURLRequestCachePolicy.UseProtocolCachePolicy,timeoutInterval: 40) request.HTTPMethod = "GET" request.addValue("Accept-Encoding",forHTTPHeaderField: "gzip") NSURLConnection.sendAsynchronousRequest(request,queue: NSOperationQueue.mainQueue(),completionHandler: {(response :NSURLResponse?,data: NSData?,error: NSError?) -> Void in callback(error,data) })原文链接:https://www.f2er.com/swift/325246.html