Check if resource at NSURL exists BEFORE downloading - ios

I want to pull down new images from a website that is updated regularly. However, there is no API for that site, so I'm trying to fetch it by url, as the images are numbered. The way I've decided to do this is have the app query the website on first launch and loop through the URLs until I get a 404. Then I store that URL and on next launch loop through till I get another 404, etc. Now I know that is less than ideal, but I'm just building a prototype. Anyway…
Since these are images we're talking about, I can't just download them all on first launch. Waste of bandwidth, could take minutes or even hours… So I just need a way using NSURLSession or whatever to get the http status code for any given image without actually downloading it.

You can do this by getting the "HTTP Headers".
This answer will help, if you're comfortable porting Objective-C to help. I can help you with it if required.

Related

How to show response from GET ajax call after user is offline through service worker?

Basically, I want my PWA to work offline. But on page load of website, there is an GET ajax call which helps in showing some content of the page.
Question is how do I let my PWA work offline as there will be an ajax call on page load which would require me to either store the response in cache?
As the content can be heavy, is it even correct to cache so much data?
Also, I read somewhere that we cannot cache GET requests, so how can I proceed with making PWA work offline?
I have tried looking at the following links, but these do not say me how to cache a dynamic content
https://developers.google.com/web/ilt/pwa/caching-files-with-service-worker
https://vaadin.com/pwa/learn/caching-strategies
https://jslovers.com/dynamic-cache-serviceworkers.html
Of course you can cache "dynamic" content – that's because from the browser's point of view it's just anothe HTTP request :-) It is of course a matter of your application & server logic whether that is useful in any way. For some application caching dynamic content and then showing it to the user at a later time might work completely ok but for some other application it might come with problems. You know, it would be fine to show a rarely updated avatar image but not ok to show old currency info, right?
You could also design the app around these limitations, maybe show the user a notification saying "hey, you're using an offline version and the data is XX hours old!" or something like that.
You can easily store multiple megabytes of network responses into the cache. If you've got more than 50 megs browsers start to limit you. Also, always have error handling ready if the browser tells you the cache is full or whatever.
Does this explanation help you?

Get data from server in iOS

I have a script that takes hours and outputs if it is open or closed and I want to run this on my server, and then from my iOS app get if a certain place is open or closed. And I was wondering how I could do this without generating an JSON file and then parsing that as I would to frequently re-generate the file as the things change from open to closed and such. So is there a way I could make a call to the server and just get the data that I want?
Im not entirely sure whether you are asking specifically for an iOS framework for requests and responses or server side. So I figure I will try to answer both. Here are some swift projects you might be interested in. I'm not incredibly familiar with objective c yet but these resources might prove useful.
Alamofire
This will help you send and receive http requests.
Swifty JSON
This will help with JSON parsing
However if your question is more regarding server side issues. You could look into a REST api and that way you could only request certain entities that you are interested in instead of just sending back the entire batch of information with all of your data, and searching through it on the iOS device.
Hopefully this is somewhat helpful.
If you are only planning on GET-ing some content from the web you can use the NSURL class. NSURL *lru = [NSURL URLWithString:#"http:mywebserver.com"] and then create a new string object from the url with [NSString stringWithContentsOfURL:lru], or any data you would like. If you are trying to hold a session or having more control over the request, I would suggest looking into the NSURLSession class.

How to test/check use of NSURLCache?

I have added the two lines of code to my project as described in this blog post.
However, as nothing different happens in the app itself, is there a way to test this behaviour and check it is working as expected? Is there a profile in instruments? Is there something I can print to the log in Xcode when a response is cached/cached response is used?
It just seems so simple to implement, I want to make sure it is actually having an effect on my app and that I won't have to implement a caching system myself.
Many thanks,
Sam
go offline right after you got the page and then reload the web view. the caching should take effect and the page should load I'd think
You can make sure so by analyzing HTTP traffic through a third party software like Charles.
Whenever app will make a request, HTTP Analyzer will pick the request made. If it is using cache, then same will be reflected in it.
(Check the contents of response to confirm the same)

Do i need an api key to access the new freebase images?

I have a database of people in my iOS app with their images being dowloaded from freebase.com Today i noticed that the images are not downloading any more. I checked and i see that google redesigned and reorganized freebase and now the images include a key. Right now i have image url stored in core data like this
http://img.freebase.com/api/trans/image_thumb/m/03s07kd?maxheight=240&maxwidth=300
To use the new format do i have to get an api key and include that with every image request or i can just get the link and change it in my database? I am not making any other calls to the database just the image from the url.
I just tested and i can change the link to
https://www.googleapis.com/freebase/v1/image/m/03s07kd?maxheight=240&maxwidth=300
which works. I am also noticing now that the images are much lower quality then they were before. How do i get the same quality images that i was getting before?
I want to make sure that this won't happen again and do it properly.
Thanks!
There's a whole set of documentation at: https://developers.google.com/freebase/
Basically, as I understand it, if you don't include your own API key, you'll be working off a small pool of quota shared with all other API users who aren't sending keys with their requests. This means it could work in the morning, but not the evening when quota is exhausted or it works some days, but not others.
Don't forget to include the required attribution to Freebase if you aren't already including it.

HTML contents preloaded into an app and then updated from the Internet

The title says (quite) all: I would like to distribute an app with some HTML pages preloaded into the local Documents folder (they reflect the content of a mini mobile site available on the internet); then, when the contents of the pages are updated, the local HTML files into the app should be updated, so that the user can browse the updated informations also when not connected to the internet.
The app has to work since the first start, thanks to the preloaded pages, and then update itself periodically (I didn't need to check the modify date/time of the single files, it's enough to check and update them when the local copies are older than x days).
The problem: I think I can do it all, but I was asking to myself if is there some framework/class that does it automatically, because it sounds to be a pain :)
Consider using ASIHTTPRequest. Check out this SO question.
Specifically, you might want to look into ASIWebPageRequest:
download complete webpages, including external resources like images
and stylesheets. Pages of any size can be indefinitely cached, and
displayed in a UIWebview / WebView even when you have no network
connection.
I've also used AFNetworking for my own personal projects and it's made my life 10x easier. On the AFNetworking FAQ page, there's a question regarding caching mechanisms for offline viewing. It mentions that NSURLCache in iOS 5 introduced support for caching to disk for offline use - but only for http. If you need to cache https, consider using SDURLCache.
Here's a short additional resource in regards to network caching for iOS.
Read the section titled iOS network caching
If you are looking at pre popping your iOS app with the equivalent of a browser cache then
https://github.com/rs/SDURLCache might be something to look into.
It hooks in with existing NSURLConnection frameworks such as AFNetworking and you just need to set the correct cache policy in your NSURLRequest.
Given its open source you should be able to figure out how where to place your data so it loads it without fetching from the server the first time then just specify when you want the cache to purge itself so it fetches it from the server?

Resources