Coder Social home page Coder Social logo

rncachingurlprotocol's Introduction

BACKGROUND

RNCachingURLProtocol is a simple shim for the HTTP protocol (that’s not nearly as scary as it sounds). Anytime a URL is downloaded, the response is cached to disk. Anytime a URL is requested, if we’re online then things proceed normally. If we’re offline, then we retrieve the cached version.

The point of RNCachingURLProtocol is mostly to demonstrate how this is done. The current implementation is extremely simple. In particular, it doesn’t worry about cleaning up the cache. The assumption is that you’re caching just a few simple things, like your “Latest News” page (which was the problem I was solving). It caches all HTTP traffic, so without some modifications, it’s not appropriate for an app that has a lot of HTTP connections (see MKNetworkKit for that). But if you need to cache some URLs and not others, that is easy to implement.

You should also look at AFCache for a more powerful caching engine that is currently integrating the ideas of RNCachingURLProtocol.

USAGE

  1. To build, you will need the Reachability code from Apple (included). That requires that you link with SystemConfiguration.framework.

  2. At some point early in the program (usually application:didFinishLaunchingWithOptions:), call the following:

    [NSURLProtocol registerClass:[RNCachingURLProtocol class]];

  3. There is no step 3.

For more details see Drop-in offline caching for UIWebView (and NSURLProtocol).

EXAMPLE

See the CachedWebView project for example usage.

LICENSE

This code is licensed under the MIT License:

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

rncachingurlprotocol's People

Contributors

azone avatar ebakan avatar rnapier avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

rncachingurlprotocol's Issues

AFHTTPRequest's all request will be GET method after register RNCachingURLProtocol

didFinishLaunchingWithOptions add:
[NSURLProtocol registerClass:[RNCachingURLProtocol class]];
then:
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; [manager POST:@"http://domain.com" parameters: …… success:…… ]
this request will be GET method type.

when annotation 「[NSURLProtocol registerClass:[RNCachingURLProtocol class]];」,AFHTTPRequest is successful.

Is it possible to force loading from cache even when network is on?

It seems the the library checks for reachability and just loads from the url when it is. But the app I'm building won't have lots of content that needs to be updated very frequently and it would be beneficial to have the content be there right when the UIWebView is loaded.

Is it possible to force loading from cache even when network is on? Maybe I could then fetch the remote content in the background while the user is reading (but this is not really necessary)

Any way to block cache or force a refresh?

Is there any way to flag a request or url so that the caching layer forces a connection to the internet and refreshes the cached file? I thought I could create a category off of NSURL or NSURLRequest that would add a "forceRefresh" property that I could read in you startLoading method, but it appears that something inside the system is stripping off my custom property.....I suspect it is doing a manual copy of those objects.

2 Questions about this library

2 Questions I want to ask is :

  1. How to specify more than one url for caching
  2. Which part of you code is used to get the cache data to display to the UIWebView because I want to use that for saving it to Sqlite database so that I can use that later as an offline view feature.
    Thank You

.idea

Dude, why you did that :( It's fuckin ugly

POST data to API (Django

For some reason, when using RNCachingURLProtocol together with AFNetworking, all my POST requests (JSON) to the API by AFNetworking stopped working. Took me a while to find out that RNCachingURLProtocol was interfering with it at some point.
Once I removed RNCachingURLProtocol, the API started working again.
All my POST requests were getting to the API as GET requests.

This is the code I was using:

AFHTTPRequestOperationManager *manager = [[AFHTTPRequestOperationManager alloc] init];
[manager setRequestSerializer:[AFJSONRequestSerializer serializer]];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Accept"];

[manager POST:serverURL parameters:parametersDict success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"JSON: %@", responseObject);

} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
NSLog(@"Error: %@", error);
}];

reject by Apple

i fund that on the code has "sockaddr_in" and "AF_INET",but should not user "sockaddr_in6" and "AF_INET6" replace them? so i thank it may be the reason with reject by Apple.

POST is casted to GET

Somehow this library, in connection with AFNetworking, created one of the stranger bugs i've ever encountered.

As soon as this library was registered, all of my request were casted to a GET request.
This created some odd errors if I tried to make a POST, DELETE etc. requests.

Using RNCachingURLProtocol for images

I see the example where pages are retrieved and everything is cached correctly. The next time I load the same url, I see the X-RNCache header.

However, Is there a way to do this using images? I don't see those headers even though Google Chrome shows that there's a last modified date, etc.

Non Unique Pathnames generated in [RNCachingURLProtocol cachePathForRequest]

When my web-application was in online mode, everything ran perfect. Everytime the CacheWebView ran in offline mode (all files were retrieved from cache only), my web-application encountered crazy behaviours like the assets of similiar visualisations were confused/mixed up,

While analyzing the runtime behaviour of the cache, I discovered the following issue:

The method "- (NSString *)cachePathForRequest:(NSURLRequest *)aRequest" sometimes produces non unique filenames. I found out about that by "NSLog(@"%@, %@\n", [[aRequest URL] absoluteString], fileName)" where fileName represents the generated path filename using [NString hash].

I managed to isolate the problem and located the problem as the use of [NSString hash].

[@"http://192.168.1.157/schmitz-web/assets/img/colorizer/models/medimatic_gyn/polster_pergament.png?cacheToken=1" hash]
[@"http://192.168.1.157/schmitz-web/assets/img/colorizer/models/medimatic_uro/polster_pergament.png?cacheToken=1 " hash]

Will result into the same hash value.
Further details on that can be found here: http://www.abakia.de/blog/blog/2012/12/05/nsstring-hash-is-bad/

I will supply a pull request for that.

Save Only Selected Website URL offline not all URL's.

Currently this library is saving last webpage visited and save it offline and when I load the app again that url is opened. What I want is the user to save the current website html page offline into Sqlite database and load that in a table view which will display all the url's saved.
When the user clicked on the table row the corresponding url should be opened offline in UIWebView.
How can I achieve this. Thank You

php issues as well as burstly ad banner propblems

Hey, been using your protocol and so far it's been great but Ive encountered two problems,
firstly I had to make an exception for php as if I was using the protocol the post comment protocol in my webview site wouldnt work properly.
Secondly I havent researched exactly why but now that Ive implemented burstly skyrocket add marketing banner it will never load with content as long as the protocol is used.
While I could leave these exceptions included it feels a bit hacky and I was wondering if you had encountered these issues and if you know a solution.
thx

Javascript Image Fetching Fails (I think)

I created a web page that uses retina.js from http://retinajs.com/ to download @2x images, however this does not appear to work properly. I can see the request being made for the retina images in (void)startLoading, however that's about as far as it gets because the next code to fire after [self setConnection:connection] is (void)stopLoading. Any ideas?

WKWebView support

Hi @rnapier
It seems that RNCachingURLProtocol doesn't support WKWebView.
WKWebView class was introduced in iOS 8 release.
If you will have some spare time, please take a look at:

  • is it possible to cache web pages in WKWebView?
  • how to do that?

Best

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.