How can I save pdf's to my app resources folder, and access them in run-time? - iphone

I have an app I'm designing that will allow for lots of PDF viewing. There are a lot of different languages available, and so if I were to include all of them in the app, it would be like 100+ mb in size which just won't fly.
So I'm thinking that I am going to put the pdf's on my server, and access them with a direct download link like this:
http://mysite.com/pdfs/thepdf.pdf
Which will return the exact pdf I want. So I'm wondering how I can go about accessing these resources as I download them on the fly?
I imagine I need to save the pdf's to the app resources folder? And then when a tableView row for the pdf is selected, I check if the pdf is in the resources folder (how do I do that?), and if not, pull it down off the server, and load it into my view?
I think I have an okay idea of what I need to do, just not very clear on the code to do it. Can anybody post the code for accessing the resources folder (if that's actually what I need to be doing), and maybe the code for how to check if something is in the resources folder?
Thanks!

Have you considered using a UIWebView to view the PDF instead of downloading and loading it yourself? UIWebView should take care of caching, so you won't have to worry about that.
Assuming that a UIWebView won't work, to download PDFs and see if they exist, you need to store it in the Documents folder. The resources folder cannot be altered after you submit your app to Apple, but the Documents folder in your app is completely fine. To access it, I would actually recommend ConciseKit, which can be found on GitHub. It gives you a helper method to access your app's document directory. The helper method is
[$ documentPath];
Then you can get the path for a file by doing
[[$ documentPath] stringByAppendingPathComponent:#"file.pdf"];
So that is how you get a path to a file, to check if it exists, you want to use NSFileManager.
[[NSFileManager defaultManager] fileExistsAtPath:#"path from above"];

Related

iphone update some files in application

I'm creating one iphone application that uses and xml to get some data from and a few images. Lets say that originally these files will be in the application bundle. But then I want to make my application to get updates from a web service. So lets say I download a new xml and new Images.
Where do I save them? I think I will be able to save them in the application "cache" right?
But then How do I make my application check if this resource exists in the cache, then load that one...else load the one in the application bundle?
iOS to be used 4.3 but if it makes it easier we can go to 5.
You will have to use NSFileManager to save the file to and load it from the document directory. Check out the following link, which actually contains all the info you need:
http://www.friendlydeveloper.com/2010/02/using-nsfilemanager-to-save-an-image-to-or-loadremove-an-image-from-documents-directory-coding/
If [UIImage imageWithContentsOfFile:fullPath]; (fullPath being the NSString with contains the path to your image in your document directory) returns nil, then the image is not present, so you'll have to load the default image from your bundle.
You can access your default bundle files using [[NSBundle mainbundle] pathForResource:#"yourFileNameHere" ofType:#"yourTypeHere"] This will return the path to your file in your bundle and you can just pass it to [UIImage imageWithContentsOfFile:]
This example only applies to UIImage, but you can easily adapt it to use other classes. I think the UIImage example is enough for illustrative purposes though.
You'll probably want to create a versioning scheme. After the app starts (or periodically) call the web service for a very cheap call to ask what version it has. You'll know what version you have (write in a plist or have the version number in the folder structure). Another options is pushing a notification.
The next thing you'll have to think about is how the app reacts to getting a new data set. You could do it only on start up but that might block the start up experience if you're bounding it to a web call. The other option is to allow the dataset to change while the app is open. You could have a model that uses NSNotificationCenter to notify your views and controllers that the data has changed. I would probably version the storage as well (folder per) to help with the transition.
As for how, you can make web requests with something like ASIHttpRequest, NSFileManager to write to the documents directory, and plists to save settings like version.

How can I save a html file with external resources using AFNetworking?

I would like to save a .html webpage with AFNetworking, but would also like to save the resources (such as .css files, .js files, images etc) within the webpage so that the whole webpage can be viewed offline.
Is this possible with AFNetworking, and how would I do it? Could a short example be posted please?
Thanks!
AFNetworking is not necessary to do this. Instead, what you want to do is use an NSURLCache subclass that supports disk cacheing (such as Peter Steinberger's fork of SDURLCache). With that in place, just load up a URL using a UIWebView (this may not necessarily have to be displayed to a user), and subsequent loads should use that local cache.
At the very least, do not waste your time trying to write something on your own to download assets on a webpage. This process requires a web browser (which UIWebView qualifies as) to determine everything needed to load.

Accessing files in Documents folder via jqtouch in iphone app

I was able to save some files in the Documents folder on my app. How can I access them on the phonegap side of things with jqtouch/javascript/html. I want to do something like:
$('#intro').attr('src','../../Documents/logo.png');
But, apparently, it's not letting me go up all those directories outside of www.
How can I resolve this?
There is a bundle file reader plugin, but it seems to read the contents, not reference it.
https://github.com/phonegap/phonegap-plugins/tree/master/iPhone/BundleFileReader
Not sure you can do what you are asking about. Perhaps you could use the PhoneGap File API to save the files somewhere within your www dir instead? Then they would be available via html/js.

Is it possible to limit iOS file sharing functionality to a subfolder in the documents directory?

My image editing app is saving some important data in the documents directory. In a tutorial I was reading this:
iTunes will then display anything you
save to the Documents directory in
your app to the user, when they go to
the “Apps” page in iTunes and scroll
to the bottom:
I have a subfolder called userImages and it would be clever to restrict file sharing only to that folder and not to everything in documents. Otherwise the user would accidently (or on purpose) mess around with files that the app depends on to work properly. This would be bad.
Is there a way to restrict it to a subdirectory in documents?
No, what you should do instead is store anything you do not want users seeing in the "Library" directory for the app. Check here for a list of places you can store data:
How can I get a writable path on the iPhone?

Move a folder to Documents directory

The iPhone application I'm working on comes bundled with 20MB of images in the application directory and I need to write new images to the documents directory over time.
Ideally I'd like to move the initial folder to the documents directory, copying the folder would mean wasting 20mb of the users disk space.
Failing that I'll create the required structure in the documents directory and leave the initial images in place, but that would mean checking in two places every time I want to display an image.
Is there a way of automagically searching the application bundle and documents directory when building an image path?
To answer your first concern: you can’t move anything out of the application bundle. Any modification of said bundle invalidates the signature and will render your app non-functional on all but jailbroken iOS devices.
Now, onto the challenge of copying images out: To find something in the main bundle, you’d do this:
NSString *imagePath = [[NSBundle mainBundle] pathForResource:#"myImage"
ofType:#"png"];
For an image, though, you’re better off doing this:
UIImage *imageFromBundle = [UIImage imageNamed:#"MyImage.png"];
That will do caching for you in a way that loading the image directly won’t.
I don’t know why you’d want to copy the image out, but it seems that maybe you have an initial set of images that may or may not be replaced. I’d recommend, then, instead of copying out of the bundle, look for the image first in the Documents folder, and if it isn’t there, copy it out of the bundle. That allows you to replace the images without copying from the bundle or modifying the bundle.
AFAIK the bundle is read only. So while your "move" idea is a good one, I am fairly certain Apple does not allow for it currently.
Is there a way of automagically
searching the application bundle and
documents directory when building an
image path?
I can say that I have done this for images but you have to create the magic yourself. I wrote a class that I would call with the name of the image as a parameter. That class would first look in the documents directory, then look in the bundle and finally look to a website for the image. When it found the image it returned the UIImage. A bit of a pain to initially setup, but once in place it is quite useful and I am glad I did it.