微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

swift 解析GZIP

居然会自动解码,要不要这么智能,弄了快一个上午,傻逼了。。。。


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:

  1. AddNSData+GZIP.swiftfile to your project.
  2. Addlibz.dyliblibrary to your project.
  3. Add a line#include <zlib.h>to yourProjectName-Bridging-Header.hfile.
  4. Invoke from your Swift/ObjC files.
提问题的人设置的方法是对的,不过他的 ProjectName-Bridging-Header.h设置出错


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)
})

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐