iOS download and save HTML file - iphone

I am trying to download a webpage (html) then display the local html that has been download in a UIWebView.
This is what I have tried -
NSString *stringURL = #"url to file";
NSURL *url = [NSURL URLWithString:stringURL];
NSData *urlData = [NSData dataWithContentsOfURL:url];
if ( urlData )
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory,#"index.html"];
[urlData writeToFile:filePath atomically:YES];
}
//Load the request in the UIWebView.
[web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"]isDirectory:NO]]]; // Do any additional setup after loading the view from its nib.
But this gives a 'SIGABRT' error.
Not too sure what I have done wrong?
Any help would be appreciated. Thanks!

The path passed to the UIWebView is incorrect, like Freerunnering mentioned, try this instead:
// 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.nl"];
NSData *urlData = [NSData dataWithContentsOfURL:url];
[urlData writeToFile:filePath atomically:YES];
// Load file in UIWebView
[web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath]]];
Note: Correct error-handling needs to be added.

NSDocumentDirectory will be backed up to iCloud. You might be better off using NSCachesDirectory https://developer.apple.com/library/ios/#qa/qa1719/_index.html

Your app is a folder with a .app extension on the end. On the iPhone once it is installed you can't change this folder, hence why you save it to the documents directory.
In your example code you save the file to Documents/index.html and ask it to load appname.app/index.html.
[NSBundle mainBundle] doesn't give you the documents directory it gives you (i think) the .app folder (though it might be the folder that contains the app, documents, and other folders).
To give you'll want to change this line.
[web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"]isDirectory:NO]]];
To (providing this is the same method as the rest of the code, else recreate the object 'filePath')
[web loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:filePath isDirectory:NO]]];

Here is the Swift 2.x version:
var paths: [AnyObject] = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)
var filePath: String = "\(paths[0])/\("index.html")"
// Download and write to file
var url: NSURL = NSURL(string: "http://www.google.nl")!
var urlData: NSData = NSData.dataWithContentsOfURL(url)
urlData.writeToFile(filePath, atomically: true)
// Load file in UIWebView
web.loadRequest(NSURLRequest(URL: NSURL.fileURLWithPath(filePath)))

Related

Display Local HTML file from documents directory in a UIWebView on iPhone

How can I display a HTML file from the Documents directory in my app?
Below is the code that I'm using and nothing is showing up in my UIWebView
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [NSString stringWithFormat:#"%#/%#", documentsDirectory, #"Notes.html"];
NSURL *url = [NSURL URLWithString:filePath];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[notesListWeb loadRequest:request];
Why is this not working out?
I've found other answers but they aren't from the documents directory.
Your help is greatly appreciated.
The proper way to create an NSURL object with a file path is:
NSURL *url = [NSURL fileURLWithPath:filePath];
You should also change how you create your filePath:
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:#"Notes.html"];
Also keep in mind that filenames are case sensitive on a real iOS device. Is the file really named Notes.html and notes.html?

How to save filename with space on Iphone

I am trying to save file in document directory using
NSString *documentDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) lastObject];
NSString *filename=[NSString stringWithFormat:#"abc 1.pdf",];
NSString *pdfLocation = [documentDirectory stringByAppendingPathComponent:filename];
I am opening this file using a web view
NSURL *url = [NSURL URLWithString:pdfLocation];
NSURLRequest *urlRequest = [[NSURLRequest alloc] initWithURL:url];
[self.webView loadRequest:urlRequest];
[urlRequest release];
This fails if the the file name has a space.How should I save file so that space in the filename is maintained.
UIWebView expects a file URL in this case. Change the line to:
NSURL *url = [NSURL fileURLWithPath:pdfLocation];

Why is my UIWebView showing lots of HTML instead of the local file?

I have a "download manager" integrated into my iPhone App which allows the user to save a webpage online to their documents folder on their device to view offline and locally. This works perfectly, except the files show with lots of HTML code rather than what they should look like.
Here's the UITableView with files: http://cl.ly/3D2g0o3C3a302J243D3v
Here's the UIWebView with the HTML in it instead of the usual webpage: http://cl.ly/2s1h3H3D1c1t3y1J2G0W
I'm loading my UIWebView like this (looks all fine?):
NSURL *url = [[NSURL alloc] initFileURLWithPath:documentsDirectory];
NSURLRequest *req = [[NSURLRequest alloc] initWithURL:url];
[webView loadRequest:req];
Why is this happening and what can I do to rectify this?
Use this line of code my friend,
[WebView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:#"index" ofType:#"html"]isDirectory:NO]]];
and here is a code that i use before:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *appSettingsPath = [documentsDirectory stringByAppendingPathComponent:#"index.html"];
now, you have the file path in your document directory.
then:
[webview loadHTMLString:SiteString baseURL:[NSURL fileURLWithPath:appSettingsPath]];

Open downloaded files in webview in iphone

I am new to iphone development. I am developing an iphone application in which i am downloading files using web service with following code:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = [paths objectAtIndex:0];
NSString *fileLoc = [basePath stringByAppendingString:#"/filename.pdf"];
NSData *fileData = [self decodeBase64WithString:soapResults];
[fileData writeToFile:fileLoc atomically:YES];
And my file is in path : /Users/myusername/Library/Application Support/iPhone Simulator/4.0/Applications/7A627E64-DEAB-40FA-BE04-35ED50580B65/filename.pdf
How can i open this file using uiwebview??? I tried this:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = [paths objectAtIndex:0];
NSString *fileLoc = [basePath stringByAppendingString:#"/filename"];
[self loadDocument:fileLoc inView:mywebview];
My loadDocument method is:
-(void)loadDocument:(NSString*)documentName inView:(UIWebView*)webView
{
NSString *path = [[NSBundle mainBundle] pathForResource:documentName ofType:#"pdf"];
NSURL *url = [NSURL fileURLWithPath:path];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
}
But it showing error... I think location is the problem.. How can i find the path in simulator as well as in orignal device???
Please help me...
pathForResource:ofType: can only be used to load resource files, which are shipped as part of your application bundle. You're trying to load a downloaded file from your Documents directory. So you should be able to just do
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = [paths objectAtIndex:0];
NSString *fileLoc = [basePath stringByAppendingString:#"/filename.pdf"];
NSURL *url = [NSURL fileURLWithPath:fileLoc];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
[webView loadRequest:request];
What is the error you are getting? What does your loadDocument method look like? You should load the document into the WebView something like this:
[webView loadRequest:[NSURLRequest requestWithURL:[NSURL fileURLWithPath:fileLoc]]];

How to create iPhone application can download content

Now I try to build iPhone application, like magazine online, that can download new magazine form web and store in app (can play offline).
I no idea for implement feature download new magazine form online source and store in the app.
Please help me.
Here's an example how you can download a file to your iPhone and reuse it later (in this case it's stores an HTML document that is then loaded to UIWebView from local store).
// downloading file
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,
NSUserDomainMask, YES);
NSString *dataPath = [paths objectAtIndex:0];
NSString *fileName = [[url path] lastPathComponent];
NSString *filePath = [dataPath stringByAppendingPathComponent:fileName];
if ([[NSFileManager defaultManager] fileExistsAtPath:filePath] == NO) {
NSString *fileString = [NSString stringWithContentsOfURL:url
encoding:NSUTF8StringEncoding
error:nil];
[fileString writeToFile:filePath
atomically:YES
encoding:NSUTF8StringEncoding
error:nil];
}
// use local file
NSURL *loadUrl = [[NSURL alloc] initFileURLWithPath: filePath];
NSURLRequest *request = [[NSURLRequest alloc] initWithURL: loadUrl];
[webView loadRequest: request];
[request release];
[loadUrl release];