Giter Site home page Giter Site logo

evermeer / evurlcache Goto Github PK

View Code? Open in Web Editor NEW
297.0 297.0 50.0 10.23 MB

a NSURLCache subclass for handling all web requests that use NSURLRequest

License: Other

Ruby 11.28% Swift 87.59% Objective-C 1.13%
cache nsurlcache-subclass nsurlrequest swift uiwebview

evurlcache's People

Contributors

alexookah avatar claygarrett avatar evermeer avatar flycjh avatar gregkerzhner avatar kitsepamvitallikotivskyi avatar pahnev avatar pryaniq avatar schmidt9 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

evurlcache's Issues

Files placed in PreCache are not found

I've tried both in a custom application as well as running the example application right away.
Added another folder to the app without any luck as well.

Not sure if this is a EVURLCache problem rather than something else but writing and reading from the cache folder works.

The top folder is named according to the domain name convention.
When I try in the simulator and later opens the PreCache folder, the files are there.
But the PreCache folder cannot be accessed.

Add Cache-Control expiration if server header is not set

Hi,

i am trying to modify the header of the response.
and i was looking inside the library maybe there was a way to add an option to Modify or add a Cache-Control header?

if !EVURLCache.ADD_CACHE_CONTROL {
    if let httpResponse = cachedResponse.response as? HTTPURLResponse {
        var headers = httpResponse.allHeaderFields as? [String : String]
        headers!["Cache-Control"] = "max-age=\(EVURLCache.CACHE_EXPIRATION_SECONDS)"
    }
}

Cache not found, wrong path

the file is cached here:

/var/mobile/Containers/Data/Application/50B1343F-AE81-41BE-A7DB-CA21A059481A/Documents/Cache/192.168.1.224/polymer/bower_components/webcomponentsjs/webcomponents-lite.min.js

but then it is loaded from here.. and of course not found

/var/containers/Bundle/Application/78A9AE26-66AA-4264-91AF-CA96DCDBD9EE/TopEase%20Mobile%20App.app/PreCache/192.168.1.224/polymer/bower_components/webcomponentsjs/webcomponents-lite.min.js
why its looking in the wrong path?

Caching raw data cause crash

Hi there,

I discovered a crash today when trying to cache a page that contain an iframe (Youtube video). I get that message in the log :
WARNING: Unable to get the path from the request: <NSURLRequest: 0x154dd4750> { URL: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAADGCAYAAAAT+OqFAAAAdklEQVQoz42QQQ7AIAgEF/T/D+kbq/RWAlnQyyazA4aoAB4FsB ... rkJggg== }

I figured out that the code was not able to define a path in storagePathForRequest:rootPath as request.URL, in this case, is nil.

So I made a workaround to prevent my app from crashing by making sure localUrl is not nil here : https://github.com/nartex/EVURLCache/tree/nicolas255-patch-1
I think that it should be handled better if possible but I have no idea how to to do that.

WKWebView support?

Hey, great plugin! Do you know if WKWebView is supported? For example, I'm loading an HTML string like this:

webView.loadHTMLString("<html><head><link href="styles.css" /><script src="script.js" /></head><body>...</body></html>", baseURL: NSURL(string: 'http://example.com'))

Does this mean those files can be in /PreCache/example.com/ and it will pull them locally instead of the web?

Crash when logging is turned on

When I turn on the logging, the application crashes because of the following piece of code:

    // Log a message with info if enabled
    public static func debugLog<T>(object: T, filename: String = #file, line: Int = #line, funcname: String = #function) {
        if LOGGING {
            let dateFormatter = NSDateFormatter()
            dateFormatter.dateFormat = "MM/dd/yyyy HH:mm:ss:SSS"
            let process = NSProcessInfo.processInfo()
            let threadId = "." //NSThread.currentThread().threadDictionary
            NSLog("\(dateFormatter.stringFromDate(NSDate())) \(process.processName))[\(process.processIdentifier):\(threadId)] \((filename as NSString).lastPathComponent)(\(line)) \(funcname):\r\t\(object)\n")
        }
    }

Specifically the NSLog("\(dateFormatter.stringFromDate(NSDate())) \(process.processName))[\(process.processIdentifier):\(threadId)] \((filename as NSString).lastPathComponent)(\(line)) \(funcname):\r\t\(object)\n") line crashes the application.

Error it gives: EXC_BAD_ACCESS.
When: using Webview
xCode version: 7.3

How to prevent EVURLCache to cache a request with Cache-Control header?

This question comes from someone on Stackoverflow, but we've been having the exact same issue. http://stackoverflow.com/questions/39020639/how-to-prevent-evurlcache-to-cache-a-request-with-cache-control-header

When I use EVURLCache, the POST request response for my AlamoFire request is also cached even if cache-control and Pragma headers are defined (should be cached only 4 hours in this case).

It would be great if EVURLCache did actually cache for x hours, like the cache-control header defines, instead of just keeping the old cache.

NSURLErrorDomain appear on some requests when in plane mode

When on offline "plane mode", the following error appears and provides a Result.Failure enum type for response.result for the Alamofire request.

The strange thing is that previous request url offline cache works well on the same domain.

I tried to disable the previous request but the failing one still fails.

Any tips to debug ? I added breakpoints in Alamofire where errors are thrown, and also in EVURLCache.swift but I couldn't find where what throws this error. It looks to be lower level than EVURLCache or Alamofire

Note that all caches work well when in online mode.

---------------------
Request: POST http://mydomain/api
Headers: [
  Content-Type : application/json; charset=UTF-8
  Accept : application/json
  x-auth-token : 826fdd79-f2ca-456e-9620-80903a05c508:1504282475655:a7815835d4dd35cd01265204d1d42772
  Content-Length : 145
]
09/01/2016 18:41:02:301 myapp)[1205:.] EVURLCache.swift(104) cachedResponseForRequest:
    Returning cached data from /var/mobile/Containers/Data/Application/DAA4BA7E-9D8D-40F9-A6AB-04FE71E240F1/Documents/Cache/mydomain/api/index.html

---------------------
Error: The Internet connection appears to be offline.

the full error is : FAILURE: Error Domain=NSURLErrorDomain Code=-1009 "The Internet connection appears to be offline."

My request code is the following :

        let paginatedRestServiceAddress = "mydomain/api"
        let token = "123456789"
        Alamofire.request(.POST, paginatedRestServiceAddress, headers: [
            "Content-Type": "application/json; charset=UTF-8",
            "Accept": "application/json",
            "x-auth-token":token
        ], parameters: [:], encoding: .Custom({
            (convertible, params) in
            let mutableRequest = convertible.URLRequest.copy() as! NSMutableURLRequest
            mutableRequest.HTTPBody = Mapper().toJSONString(_pagination)!.dataUsingEncoding(NSUTF8StringEncoding, allowLossyConversion: false)
            return (mutableRequest, nil)
        }))
            .validate(statusCode: [200])
            .responseString { (response: Response<String, NSError>) in
// the error appears at this moment
                debugPrint(response.result)
        }

and EVURLCache is loaded with :

        #if DEBUG
            EVURLCache.LOGGING = true // We want to see all caching actions
        #endif
        EVURLCache.filter { request in
            let url = request.URL!.URLString
            return !self.shouldUrlBeExcluded(url)
        }
        EVURLCache.activate()

obj-c version does not load the page after cached

Hi,
Sorry to bug you with this old version, I hope there is a quick fix for it. I could load the first time your webpage, then after killing the app, either when its offline or when its online it doesn't load any webpages. Whatever URL I try to load the debug says cache found for URL, it prints the Reachability flag, the age and it stays blank.
Any ideas?

Thanks a lot
Greg

PreCache not working

I've done quite a bit of testing utilizing the PreCache in attempts to load from a different website. I've narrowed it down to the fact that even a modification of the index file in the demo breaks the PreCache from being able to be found. I simply edit the index and add a word, and it no longer loads. Replacing the file with the original allows it to function again. The error thrown is "The file is probably not put in the local path using NSKeyedArchiver". It's almost as if it's looking for a hashed file. If I change the URL of the demo PreCache and also change the WebView URL to match, it loads fine as well.

When I attempt to load a different URL entirely, with new files, even the same index file, it gives the error above. This can all be replicated within the Demo.

Use CoreServices

Hi, there i see this warning in Xcode:
MobileCoreServices has been renamed. Use CoreServices instead.
Could we change it to CoreServices?

Is it available for android?

Hello,
I use this for iPhone and very very thank you for this beacause i didn't found another so it's very helpfull for offline webpage store.

but i want it for android also,

If you or any one have this same offline webpage cache feature available in android than provide me.
Or URL post here.

How to deactivate EVURLCache?

I need activate EVURLCache in first webview, but i want deactivate EVURLCache in second webview. How do I do this?

Cache not updating

I'm unable to get the module working at all, at this point. I was previously able to load the 'live' site, the module would cache, and I could copy the cache over to precache. However, evurlcache never pulls the live site and always loads from precache.

If I remove the domain folder out of the precache folder, it loads the 'live' site but does not save it to cache. I have tried uninstalling the app, etc.

It seems like the callback to attempt a load and cache a live site is never initiated. However, it seems to randomly cache various parts of random pages, but never the entire page. This includes the sample links provided.

I'm fairly desperate for a solution and have attempted stepping through the code, but I can't find anything glaringly obvious. I also haven't delved too much into documentation regarding NSURLCache.

Does anyone have a working example or a suggestion? I've been struggling with this for a while and unfortunately I don't have another solution readily available.

Some web pages cant be opened in webView after caching - redirect issue

Try to load demo demo app with webView with any of links below instead of "http://evict.nl":

http://www.newser.com/story/229485/aetna-doing-something-about-workers-student-loans.html
http://www.beckershospitalreview.com/payer-issues/when-the-doctor-orders-time-in-the-park-humana-metcare-partner-for-nature-driven-prescriptions.html
http://www.beckershospitalreview.com/healthcare-information-technology/10-largest-hipaa-settlement-fines.html

After this turn off internet connection and open app again.
Expected result: web page is loaded from cache and displayed
Actual result: web page is failed to be opened

Same behaviour appears for some other urls but not for all.

Without network nothing is loaded

Hi,

Thanks a lot for this library!
I just tried the demo app without any modifications on a 9.2 iPhone 5s and without the wifi connection nothing is loaded. Then I turn on the wifi, your website is loaded I navigate to a submenu, I quit, I come back with wifi OFF and nothing loads upon opening the app. I get a white page.
Thanks!

can't cache

1.when i open a url :http://m.qiushibaike.com it store,but i cut off wifi can't load the cache data?

2.the local path is wrong

2014-10-22 00:40:54.697 LocalCache[5134:119705] EVURLCache.m:(62) CACHE found for /Users/huiyang/Library/Developer/CoreSimulator/Devices/8A37AEBC-49C1-4756-8456-B99345F46A33/data/Containers/Data/Application/218F6749-573A-4898-954A-7AE8B9A92BAF/Documents/Cache//index.html

//index.html

Html not cached, only images

Please see attached project (it's your test project, I just replaced url).
The problem is only images are being cached, but not html itself. I thought it's because of 'no-cache' or something like this, but the page actually contains <meta http-equiv="Cache-Control" content="public">, so I don't know...
EVURLCache.zip

POST request seems to be unable to be recreated from cache

On the same request, when being in plane mode, if I call it using .GET method, cache is returned but when I call it using .POST, no cache is returned.
Cache data is well loaded in EVURLCache.swift cachedResponseForRequest function but not forwarder to Alamofire (I didn't try without Alamofire, do perhaps this is non only on EVURLCache side).

So as I only need cache when in plane mode, I did a workaround by using .GET method when no network is detected. As I don't make a server call, it works (otherwise my server checks the calling method and refuse non POST methods).
But this is a very ugly workaround I did IMO, and it would be nice to have cache returned for POST.

This also works as the cache storage makes no difference for a GET or a POST, this is the same folder.
I also think this should be fixed by having different cache folders depending on request method.

Swift 3 version would be so nice !

As Swift 3 is out, a compatible version of EVURLCache would be nice.
For the moment I embed the lib in my project and convert it to Swift 3.

Some of the problems

Hi!@evermeer

If I use
AVPlayerItem * item = [AVPlayerItem playerItemWithURL: [NSURL URLWithString: @ "https://..."]]. Methods play video,

can cache the video like this?

Uses cache on specific content, while others don't use cache at all?

I'm trying EVURLCache. Great stuff. However, is that possible that I write filter that uses cache, while others do not use cache at all? I know I can do that in reverse (filtering content not to use cache) now with the code below. How about I only want to cache some web view contents but not others?

dpes4c0uqaanwhi

How to store multiple URL in cache?

First Thank you for providing this library.
I am trying to store multiple web pages in the app I tried but it replacing with old URL. can you please suggest me how I store multiple pages in the cache.

Cache not working with Swift 3 - Optional wrap

Hi,
i notice that cache directory are not correctly created as localUrl is store in an optional string eg:
Cache/optional("host.domain.tld/index.html")_post_get_arguments
Perhaps you don't unwrap the localUrl string before concatenation with query and cache root ?

Query params not taken into account when caching.

The current caching strategy does not take into account query params. Therefore, a request like

https://snacks.com?search=stuff

and

https://snacks.com?search=otherstuff

would be cached under the same file. To fix this

request.URL?.query

should be included in the path of the cached file if it exists.

PR: #22

URLCACHE_EXPIRATION_AGE_KEY not working

The way that the URLCACHE_EXPIRATION_AGE_KEY works is that it looks at the cached file's last modified date, and if that is too long ago, the cached response is not returned. However, the code in

public override func storeCachedResponse(cachedResponse: NSCachedURLResponse, forRequest request: NSURLRequest) 

actually always modifies the cached file's last modified date even if the file has not changed and/or no server request has been made. In other words, every time the function above is called, if an existing cached request is present in the file system, it is rewritten to be the same value, and its last modified date is changed to the current time.

This is because calling

NSKeyedArchiver.archiveRootObject(cachedResponse, toFile: storagePath)

inside 'storeCachedResponse' will always update the file's NSFileModificationDate, even if the contents are exactly the same

So, if for example, you set your URLCACHE_EXPIRATION_AGE_KEY to be 20 seconds, but you make a request every 5 seconds, then every 5 seconds the cached response's NSFileModificationDate will be changed to the current time because the file is rewritten with the same already cached response. Therefore, the cache will keep returning the same old, expired file.

PR for this problem: #18

Caching not working

Hi,

I have noticed that the amount of data transferred over the network (in XCode network tab) is pretty much the same whether the cache is activated or not.
Since this is quite a vague sentence, I've created a small test project.

CacheWebView.zip

Here I see a difference of 600 kb when activated (3MB to 2.4MB) but I would assume everything should be cached (there is no data coming from a service).

The project is using Swift 4 and run with XCode 9.2 (iOS 11).

Thanks for your support
Franck

caching html file

Hi, I find the library very useful, but just have one issue.
It doesn't seem to be caching the .html file, is this possible, do I need to switch on a parameter, etc..
The file size of the html is 3.4Kb so its not big at all.
I can see it tries to check the precache folder for it, but once it doesnt find it, it just uses the live one.
If i disable internet it comes up blank.

Thanks

PRECACHE not found

Hi there Edwin, this isn't exactly an issue since I'm sure the problem lies with me somewhere. Reading through your existing answers I've seen that you've given friendly replies even to quite rough requests. Therefore I dare ask for your opinion.

I'm trying to write a tutorial for how to cache a privacy policy locally, which your implementation seems to be perfectly suited for. I've recreated your structure for a PreCache folder in the root, then iubenda.com/privacy-policy folder and finally the 8051057 file in the folder (saved as html).

EVURLCache.swift(123) cachedResponse(for:):
PRECACHE not found /var/mobile/Containers/Data/Application/7C2EBF0D-5F1E-40B9-86E0-7DFFCF5240DE/Documents/Cache/www.iubenda.com/privacy-policy/8051057/index.html

05/11/2017 22:37:15:088 App name)[9016:.] EVURLCache.swift(126) cachedResponse(for:):
CACHE not found /var/containers/Bundle/Application/2F199D2F-8228-48CD-8896-EFDD321AC0C4/App name.app/PreCache/www.iubenda.com/privacy-policy/8051057/index.html

Where am I going wrong? Thanks a bunch :)

update: running it again an hour later it gives me:

"CACHE not rewriting stored file" and returns a white page. I think that's is progress. I now only need to find the reason for it returning a white page both off- and online. Also I found almost an identical posting here when googling for CACHE not rewriting stored file. Sorry about that.

Obj-c version?

Hi,
Would you have by any chance an objective-c version of this? I need to create a binding library for Xamarin and swift is not supported.
Thanks

Cache is not updating

Downloading a URL and all folders created in cache. Trying to hit the same url after some update in the content .In that case timestamp is changing but content inside file is not changing.

Not logging anything.

Successfully integrated using pod. Using the version "2.10.0" in Swift 2.2 and Xcode 7.3.1 but not able to cache anything while hitting URL on UIWebView. Upon setting "EVURLCache.LOGGING = true" its not even logging anything on console. Please take a look and respond back. Following is the complete code inside "AppDelegate.swift".

            EVURLCache.LOGGING = true
            EVURLCache.MAX_FILE_SIZE = 26
            EVURLCache.MAX_CACHE_SIZE = 30
            EVURLCache.FORCE_LOWERCASE = true
            EVURLCache.activate()

BTW it is working when integrated in a blank and new project.

EVURLCache and Alamofire

Reported by @nmDejamobile via email.

Hi,

I found your EVURLCache lib and it looks nice as it does all the work for me :)
But I use Alamofire and I can’t figure out how to get the result of the request.
When cache is involed, Alamofire .Success or .Failure callback are not called.
However I well see the log stating that the cached data was loaded by EVURLCache.
Also, after sometime, Alamofire .Failure is triggered by a timeout.

Any ideas on how I could use it ?
Thanks.

storeCachedResponse is never called

Hi,

When creating a request using Alamofire and AlamofireObjectMapper the UVURLCache storeCachedResponse function is never called.

I have the two logs that say no file is found in the precache and cache and that's all.

I checked the folder in the simulator library path indicated in the log and no precache or cache folder are created/

Do I have to do something special to enable the cache file to be written ?

Here are the two log lines that are printed in the log each time I run the request, no other cache log are printed (user name and project infos are replaced):

EVURLCache.swift(85) cachedResponseForRequest:
    PRECACHE not found /Users/me/Library/Developer/CoreSimulator/Devices/DCB21973-3EF7-4EEF-BA45-D78CE0000000/data/Containers/Data/Application/154D180F-46C9-4B0F-BEAC-E5F800000000/Documents/Cache/mysite.com/paginated/index.html

EVURLCache.swift(88) cachedResponseForRequest:
    CACHE not found /Users/me/Library/Developer/CoreSimulator/Devices/DCB21973-3EF7-4EEF-BA45-D78CE0000000/data/Containers/Bundle/Application/8DCAAF7D-DC88-44B0-9A69-368800000000/myapp.app/PreCache/mysite.com/paginated/index.html

Running on simulator or real iPhone 6 device has the same effect. I use iOS 9 as target.

Thanks.

Uppercase characters converted to lowercase

Just noticed if I have 'PreCache/what.ever/js/JavaScript.js' files that are included as <script src="js/JavaScript.js"></script> in the html document I'm still getting cache not found for 'PreCache/what.ever/js/javascript.js'.

If I convert everything else to lowercase the problem goes away.

Google Places Autocomplete not working

Google Places SDK ver. 3.3.0(4.5) autocomplete is not working when using EVURLCache. Requests are being written to cache, and autocomplete seems not to like it, in simply doesn't return any search results. Looking for options to disable caching for Google Places.

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.