Coder Social home page Coder Social logo

lanhaoxiang / nsurlsession-nsurlprotocol-webviewcache Goto Github PK

View Code? Open in Web Editor NEW

This project forked from zhangli4659507/nsurlsession-nsurlprotocol-webviewcache

0.0 2.0 0.0 28 KB

基于NSURLProtocol+NSURLSession的UIWebView离线缓存功能实现

Objective-C 100.00%

nsurlsession-nsurlprotocol-webviewcache's Introduction

NSURLSession-NSURLProtocol-webviewCache

##产生缘由 其实项目已经上线了一年多了,一直没时间来将一些有用的东西摘出来。最近还算是比较闲,就将一些自认为值得拿出来分享的东西摘了出来。

##与其他公共类比较 之前在GitHub上star最多的莫过于RNCachingURLProtocol,我也认真的看了他的源码,写的非常不错,不的不说有点过时了。而且有一点没处理的好,如果放入直接的将其使用,他会将所有的http请求都缓存下来,不管你是不是来自UIWebView的请求,所以有很大的必要去改进。

##与NSURLCache比较 相信对使用过UIWebView的同学都会知道,其实UIWebView本身是有缓存的,那就是基于NSURLCache来实现的。之前到网上看到过基于他来对NSURLCache实现离线缓存,我开始也封装了个,但是发现不是特别的好用,显得特别的笨重。后面看了一篇关于介绍NSURLProtocol的文章,英文还行的同学可以去看看。

另外,之前的请求是基于NSURLConnection来封装的,但是现在看到苹果最近发的通告,所以就改进成了NSURLSession。

##使用方法 1.在Appdelegate中的application:didFinishLaunchingWithOptions:方法中加上: [NSURLProtocol registerClass:[TURLSessionProtocol class]];

注:在使用NSURLSession做请求,必须这样写才能截获该请求:

NSURLSessionConfiguration *config = [NSURLSessionConfiguration defaultSessionConfiguration];

config.protocolClasses = @[[TURLSessionProtocol class]];

对于拦截NSURLSession请求,可以参考这篇博客objc

2.在你创建请求时,给请求添加一个请求头: [request setValue:@(YES) forHTTPHeaderField:KProtocolHttpHeadKey];需要注意key必须是KProtocolHttpHeadKey,不然会拦截不到你的请求。

这样就大功告成。

###备注 如果需要修改缓存时间,请在TURLSessionProtocol.m文件中去将static NSUInteger const KCacheTime = 30;修改为你需要缓存时长的时间,单位是秒。

另外就是,最终的缓存内容是以归档的方式存入到沙盒的cache目录下,要是清空缓存也会将缓存的内容删除,如果不想一起删除,可以自定义目录。

##联系方式 如果你在使用过程中遇到任何的问题或者说有Bug和好的建议,请将你的需求发到我的Email:[email protected]

交流QQ:1565773005

新浪微博:比翼双飞之云中游.

欢迎交流。

nsurlsession-nsurlprotocol-webviewcache's People

Contributors

zhangli4659507 avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.