Offline html5 in a UIWebView - ios

I will have to create an offline app from a received html5 file (sort of just display it in a UIWebView, I have no idea wether it will be multiple pages or just one). I have a very limited knowledge about html programming and such, and unfortunately due to a limited time frame I can't spend much time reading up about it. Are there any limitations to trying to display this offline in a webview or maybe other 'catches' that I need to be aware of?
Thank you in advance.

If you are not changing the contents of the webpage, you can embed the HTML and all files and you won't have to worry about HTML5 since it's all already included
So create the HTML file and keep it in a folder (let's call it "index.html" and the folder "code" for this example) and drag it into your Supporting Files folder in xcode. Click the "Copy items..." checkbox and "create folder references..."
Then use this code in your ViewDidLoad:
NSString* filePath = [[NSBundle mainBundle] pathForResource:#"index"
ofType:#"html"
inDirectory:#"code"];
NSURL* fileURL = [NSURL fileURLWithPath:filePath];
NSURLRequest* request = [NSURLRequest requestWithURL:fileURL];
[webView loadRequest:request];
IF YOU WANT TO PULL A FILE OFF THE WEB:
to begin the HTML5 file, start it with
<!DOCTYPE HTML>
(seriously, that's all that's needed for "html5")
Then to make it so you can view the files offline, create a "manifest" file, so next add the line
<html manifest="example.manifest">
Then in a text editor list all the items to be included for offline (index.html, logo.jpg, page2.html, logo2.jpg, etc)
boom, done
These 2 links are good resources for offline content
[http://ofps.oreilly.com/titles/9781449383268/chapOfflineApplicationCache.html][1]
http://www.html5rocks.com/en/tutorials/appcache/beginner/
[1]: http://ofps.oreilly.com/titles/9781449383268/chapOfflineApplicationCache.html [removed from web]

Related

Read pdf while its downloading in iPhone using a PDF Reader

I'am developing a newsstand application and i want to read the content of the pdf while its downloading. Currently the user has to wait until the pdf downloads to read it.
In some newsstand apps i have seen the users can read while the pdf pages are downloading.
I just want to know from where do i start.
It would be great if anyone could guid me on this.
I found this library : https://github.com/vfr/Reader and i tested it with a big pdf file (Very large PDF-files) so i think that's what are you searching for.
There are some interesting options so you can enable or disable some buttons or some visual effects
Edit : Using web view to load Pdf File
You can also use a web view to load the pdf file. To do that :
UIWebView *webView = [[UIWebView alloc] initWithFrame:CGRectMake(x,x,x,x)];
NSURL *targetURL = [NSURL URLWithString:#"http://developer.apple.com/iphone/library/documentation/UIKit/Reference/UIWebView_Class/UIWebView_Class.pdf"];
NSURLRequest *request = [NSURLRequest requestWithURL:targetURL];
[webView loadRequest:request];
I solved my problem by keeping my large pdf as splited pdf files. Then download the pdf to the iphone and combined the pdf files i downloaded.
So until the next pdf downloads user can read the downloaded pdf.Once the other pdf is downloaded the new part is combined to the first pdf to create a new pdf.
Thanks.

iOS Cache a UIWebView

I've written an iOS app that loads up an HTML5 web application inside a UIWebView. I'm trying to make the app cache everything in the UIWebView for use while on a 3G connection rather than load it all again.
It needs to follow all links while on a WiFi connection and download all pages and images. When on a 3G connection it should then load everything from the cache.
Is this possible? Any ideas on where I should start? Do I need to make my HTML5 application load all pages in the background rather than have the app do it?
Thanks for any help!
Firstly I would use Reachability to detect if there is a WIFI connection and your server is accessible. This way you can handle both 3G connectivity and no internet connection in the same way. (accessing the cached pages). It also means you can use the cache if you have WIFI but still cannot reach your server.
Reachability sample code
I would either download a zip file of your web pages and all the associated assets (if it is a fairly static website) and unzip and store them in the local file system. You can then point your UIWebView to the local directory when there is not WIFI.
You could use something like Zip Archive to handle this
ZipArchive GitHub page
If this is not an option then you could get the HTML from the page when you load it in the UIWebView. The problem with doing this is you are reliant on the user accessing pages in order to cache them.
You can use this to get the HTML code and store it locally:
// Determile cache file path
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *filePath = [NSString stringWithFormat:#"%#/%#", [paths objectAtIndex:0],#"index.html"];
// Download and write to file
NSURL *url = [NSURL URLWithString:#"http://www.google.co.uk"];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];
// Load file in UIWebView
[web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];
You could use this code to get all the web pages in the background as well. To do this you will either need to hard code all the required URLs and look them up individually or better still download a list of web pages from your site first.
You could create a web service (JSON is probably your best option for this) giving a list of URLs to download. You could then parse through this JSON and use the URLs to feed into the code above and cache the HTML locally.
Just remember to be careful with your hyperlinks and links to any images and CSS within your HTML as this will break your cached website if they are looking for your full online URL rather than a relative path.

UIwebview loading remote html with bundle/local images

Well, i searched everywhere but i didn't have any luck with my situation.
I am loading my webpage with UIWebView with the following code:
NSString *fullURL;
fullURL=#"http://domain.com";
NSURL *url=[NSURL URLWithString:fullURL];
NSURLRequest *requestObj=[NSURLRequest requestWithURL:url];
[_webView loadRequest:requestObj];
I want to load the remote HTML file but load the images from the bundle resourses.
The HTML file looks like this:
<img src="http://domain.com/images/image.png" width="20px" height="20px"/>
Can this be done? The majority of the posts over the internet(and here) are for loading local HTML with local images/resources which is different in my case.
Any help with my code?
Thanks
It would probably be better to use some more "loosely coupled" solution - not to edit the HTML code itself in some hacky way (regexp, not regexp... changing the HTML code manually is still pretty hacky).
As a matter of fact, I believe it can be done. The iOS just needs to be somehow informed that some of the assets are available offline, from the bundle.
Basically, when UIWebView is loading a page, first thing that happens behind the scene is downloading the main *.html page. Then all the graphics/css'es/js'es etc are being downloaded.
You can let the html file be downloaded "as is", but intercept those requests that are going to download graphics (+ other assets) and provide them from local bundle. Please refer to this post:
http://robnapier.net/blog/offline-uiwebview-nsurlprotocol-588
and apply appropriate changes. The big win here is that from the webview's (and code that loads / maintain via JavaScript calls it's content) perspective - nothing has changed at all. Only the custom NSURLProtocol itself knows that some data was loaded from local storage.
Had the exact same problem. Subclassing NSURLCache to redirect the cache to cache images from local storage worked like a charm.
Here is the writeup that I followed:
http://www.cocoawithlove.com/2010/09/substituting-local-data-for-remote.html
TL;DR Complicated, avoid if you can, but possible.
If you still want to do it: don't do a UIWebView :loadRequest on the URL itself since it will trigger the start of downloading images very rapidly so modifying the images sources using Javascript will likely happen too late.
What you instead have would have to do is to download the contents of the URL on the native side and iterate the image tags there replacing the sources (quite complicated, don't use Regular Expressions to parse HTML btw, there are libraries for that), then injecting the modified HTML using UIWebView loadHTMLString:baseURL:.

Local content URL from UIWebView

I'm writing an ios app that uses a UIWebView to display local html content as a help subsystem. The user can follow links to other local html content and I want to be able to tell what page the UIWebView is currently displaying.
I have tried webView.request.URL.absoluteString and similar variants but it gives me the app bundle and not the local html file within. How can I get the name of the local file being accessed?
try this
assuming your html file resides in your bundle.
[WebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"Your_File_Name" ofType:#"html"]isDirectory:NO]]];
hope to be helpful to you.

iOS - PDF Loaded in UIWebView then save to iBooks using UIDocumentInteractionController

I am loading a PDF from the web using a UIWebView, which works great and lets me interact with the PDF how I want.
Next I would like to be able to save that PDF to iBooks, so I am using a UIDocumentInteractionController. In reading the docs it seems that you can only use UIDocumentInteractionController with local files rather than a remote file like I have.
My question is, that PDF that UIWebView loads must be cached somewhere, so I really have to do another call to download that same file, just to have UIDocumentInteractionController be able to load it as a local file? Or can I somehow use that same file that UIWebView has already loaded?
You can't directly access the data that the UIWebView caches. If you only want to download the PDF once (which is the right way to do it), save the file locally yourself. Then load it in your UIWebView like so:
NSURL* url = [NSURL URLWithString:#"path to local file"];
NSURLRequest* urlRequest = [[NSURLRequest alloc] initWithURL:url];
[myWebView loadRequest:urlRequest];
[urlRequest release];

Resources