Save web pages with all their images, CSS, and other resources - objective-c

I need to download a few web pages for later usee in my application, and I can't find an easy way to accomplish this task. I would prefer a solution where I don't need to parse the HTML to get the URLs of the images and other resources, but rather download these somehow automatically.

OK guys, here is my solution:
created my own cache object, derived from NSURLCache
added to it a "state" enum variable, with the possible states of: SAVING, LOADING, NOTHING
overwritten cachedResponseForRequest to do things according to the state
SAVING: created a NSMutableDictionary to store every download request
Downloaded the file in the request to a flat file, added the path to the file to the dictionary as an object, with the URL as the key
LOADING: used this dictionary as they did in this example to load back the stored content: http://cocoawithlove.com/2010/09/substituting-local-data-for-remote.html
set my cache object as the shared cache object using [NSURLCache setSharedURLCache:myCacheObject];
After this, when I want to save something I set the cache's state to SAVING, and load a request to an UIWebView. After this I set the state back to LOADING, load a request to an UIWebView, and if I stored my request previously, my cache will load it from the disk.

I think ASIHTTPRequest framework can be useful for you - try ASIWebPageRequest and see if it supports all features you need.

Related

How to cache whole next.js's HTML page in Redis?

I'm new to next.js. I'm trying to cache the whole HTML page in next.js. So, that I can reduce the response time in the next call for that page.
I tried by creating a custom server and then save the response that came from rendertoHTML()/render(). But it didn't return any response.
Redis is not for caching whole HTML pages or any other complex object types for that matter. It's key–value database. In Redis you should keep your object simple.
For your case it's best to use stale-while-revalidate (SWR) cache-control headers in combination with getServerSideProps for server-rendering in NextJS.
See here for example.

Quickly-editable config file, not visible to public visitors?

I am in the middle of working with, and getting a handle on Vuejs. I would like to code my app in a way that it has some configurable behaviors, so that I could play with parameter values, like you do when you edit your Sublime preferences, but without having to compile my app again. Ideally, I would want a situation where I could have my colleagues be able to fiddle with settings all day long, by editing a file over FTP maybe, or over some interface....
The only way I know how to do it now, is to place those settings in a separate file, but as the app runs in the client, that file would have to be fetched via another HTTP request, meaning it's a publicly readable file. Even though there isn't any sensitive information in such a configuration file, I still feel a little wonky about having it public like that, if it can be avoided in any way...
Can it be avoided?
I dont think you can avoid this. One way or another your config file will be loaded into the vuejs application, therefore being visible to the end user (with some effort).
Even putting the file outside of the public folder wouldnt help you much, because then it is unavailable for HTTP to request the file. It would only be available to your compile process in this case.
So a possible solution could be to have some sort of HTTP request that requests GET example.com/settings and returns you a JSON object. Then you could have your app make a cookie like config_key = H47DXHJK12 (or better a UUID https://en.wikipedia.org/wiki/Universally_unique_identifier) which would be the access key for a specific config object.
Your application must then request GET example.com/settings (which should send the config_key cookie), and your config object for this secret key will be returned. If the user clears his cookies, a new request will return an empty config.

Bind image from isolated storage and application directory

in my WP7 application I have a listbox containing an image. I include several images within my application, but if an image is not found it should be retrieved from internet and then of course be stored in Isolated Storage. Now i have 1-2 questions:
1) On initial application start, should I copy all images into IsolatedStorage so that Isolated Storage contains all images (and therefore the images from images-folder of application are availabe twice: In application image directory and in isolated storage)?
2) Is it possible within the listbox to display images in one case from IsoloatedStorage and in the other from application file directory?
many thanks!
P.S. Code examples are welcome, especially in vb.net.
1 - No. Why waste time and storage?
2 - Possible solution - write a class that implements IValueConverter. In your Convert method, if the value is Uri with IsAbsoluteUri=true and Scheme="isostore", you read the file from isolated store, and return a BitmapImage, as described here. Otherwise, you just return unconverted value from your Convert method. And, you specify your converter in the binding.
Sorry I don't have code examples to share.
P.S. For your task, I'd recommend a 3-rd party lib called "Kawagoe Toolkit". The only drawback is the license that obliges you to mention them in your about page. If using Kawagoe, you could just define a property "imageSource" returning object, and return either Uri for images from resources/XAP, or delay loaded ImageSource object obtained from Kawagoe's ImageCache.Default.Get() method, which will eventually load itself from either isolated store of the Internets. They already have the downloading and caching code you need.
Yes, u'd better copy them to unify location.
What for ? Just show images from IsolatedStorage. Initial images are copied there, and new images are downloaded from internet and put to IsolatedStorage (of course u'd have to write this code).

Yii: Does renderPartial caches content of view files?

I need to render and display instances of different models(comments, polls, etc) on the same page. Those instances are sorted by date, so it is possible that there will be couple comments then poll then another commetns and once more poll and so on.
So I am calling renderPartial in a loop. I am afraid that this can work slow as each renderPartial needs to read file from hdd.
So my question is: does renderPartial caches content of the file somewhere in the memory during one http request? So calling renderpartial multiple times will not touch hdd every time.
Yii will not cache anything until you instruct it to do so. You can cache whole page, or just a page part, it's up to you really, you should follow Yii's cache tutorial to find out how to achieve that.
I'm not a admin or something, but I'm pretty sure that OS will cache the file, so you don't need to worry about that.
Here is 1 alternative: you can prepare a set of all the data(as array for example, or some custom type of structure/object). Then change you code in the view to check for this array and loop trough it. This way you can send the output in single renderPartial call.

Preventing RestKit mapping from auto save

I'm using RestKit to map an XML file. In that file are several items that map to a class, let's call it ListItem. These listitems have a reference to a thumbnail image file. As soon as the mapping is done I'm starting the download of the thumbnails so I can be sure they are available when I need them.
Now I'd like to make sure these files are downloaded properly. Checking for the existance of the files in the cache won't be a problem, but I'd like the context only to be saved when the download of all files was successful.
I just can't figure out at which point the saving is being done. After I call the mapping method
[objectManager loadObjectsAtResourcePath:#"data.xml" delegate:self];
the mapping is being done and I get the result in
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects
This is the point where I start the image download, but at that point the context has already been saved. I don't know how to prevent this behavior.
The only idea I came up with is duplicating the sql file and using the duplicate-file for the objectStore to perform the sync. When the sync is successful overriding the original sql with the duplicate.
Does anybody know a better way to do this? Did I miss a method that is being called in the mapping process?