2

I have a button called "Download page" at the bottom of my detailView of SplitView app in iPad. I want to download the corresponding html page on the click of the aforementioned button i.e. I need to add the functionality of the "Ctrl+S" for that button so that I could download and save the page. How can I do that ?

1
  • okay .. I can do that .. but I think my question just pertains as to saving the file in ios. where the file is in html format. Regards. Commented Jun 18, 2012 at 9:12

2 Answers 2

3

You should do this:

//Download data from URL NSData *data = [NSData dataWithContentsOfURL:[NSURL URLWithString:@"yourstringURL"]]; //use this data to write to any path as documentdirectory path + filename.html NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirectory = [paths objectAtIndex:0]; //create file path NSString *htmlFilePath = [documentsDirectory stringByAppendingPathComponent:@"file.html"]; //write at file path BOOL isSucess = [data writeToFile:htmlFilePath atomically:YES]; if (isSucess) NSLog(@"written"); else NSLog(@"not written"); 

You can same htmlFilePath to retrieve html file from document directory

Sign up to request clarification or add additional context in comments.

1 Comment

what would the path for saving of the file be ?? I mean I am running the IPad simulator .. how can I know the path as to where I should save my file ??
2

You can get all the html content inside an NSString and then save it, like so

NSString *allHtml = [webView stringByEvaluatingJavaScriptFromString:@"document.documentElement.outerHTML"]; [allHtml writeToFile:@"YourFilePath" atomically:YES encoding:NSUTF8StringEncoding error:NULL]; 

This will save all the HTML to the path you define

4 Comments

Thanks Omar.a And if I need to upload the same html file using another button called "Upload html", how would I do it ?? I mean what is the corresponding function ??
@zyxwvu you will need to start a request (asynch or synch) and send it to the webservice you want, this will need a new question since uploading is alittle harder, also dont forget to upvote and/or accept the answer if this was useful :)
what would the path for saving of the file be ?? I mean I am running the IPad simulator .. how can I know the path as to where I should save my file ??
@zyxwvu use this NSString *savePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/yourFile.txt"];