evermeer / evurlcache Goto Github PK
View Code? Open in Web Editor NEWa NSURLCache subclass for handling all web requests that use NSURLRequest
License: Other
a NSURLCache subclass for handling all web requests that use NSURLRequest
License: Other
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.
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)"
}
}
IOS 7 doesn't like the version of reachability being used. Any plans on updating?
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?
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.
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?
I only want my client to cache content from specific endpoints.
I have a url need to be cached forever and want to keep it updated, is it possible?
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
Error message:
-[NSURLResponse statusCode]: unrecognized selector sent
Suggested fix:
if(httpResponse != nil && [httpResponse respondsToSelector:@selector(statusCode:)] && httpResponse.statusCode >=
400)
{
NSLog(@"CACHE Do not cache error %li page for: %@", (long)httpResponse.statusCode, request.URL);
return;
}
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.
When caching response data, the currentDiskUsage property does not appear to update. Does cached content get evicted if the maximum disk size is exceeded?
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()
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
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.
Hi, there i see this warning in Xcode:
MobileCoreServices has been renamed. Use CoreServices instead.
Could we change it to CoreServices?
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.
I need activate EVURLCache in first webview, but i want deactivate EVURLCache in second webview. How do I do this?
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.
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.
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!
Page are not cached in ios version 12.0.
When my cached data expires it is not cleaned. Is this is correct behaviour.
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
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
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.
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.
Hi!@evermeer
If I use
AVPlayerItem * item = [AVPlayerItem playerItemWithURL: [NSURL URLWithString: @ "https://..."]]. Methods play video,
can cache the video like this?
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?
Hi,
The compiler is complaining that the Functions class is missing.
Thank you!
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.
hi
the app crashes trying to loadRequest from https://www.sbk.org
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 ?
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
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
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.
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
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
See previous issue. Still doesn't appear to be working, even in the demo.
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.
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
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.
I've been trying EVURLCache to cache a html5 game(http://game.zorropk.com/gamenow/xiao5haiyanglixianji/) in iOS, but some kind of cached file can't be handled correctly, such as json, plist, fnt and tmx file, the UIWebView will be just black and showed noting. If i change the code to skip caching these kind of files, the game will run properly.
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.
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.
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.
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 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.
Hi, i am using this in my app and i would like to know how can i force reload a request to skip any existing caches and update the content view with the latest content?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.