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 ?
- 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.user425243– user4252432012-06-18 09:12:30 +00:00Commented Jun 18, 2012 at 9:12
Add a comment |
2 Answers
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
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
Omar Abdelhafith
@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 :)
Omar Abdelhafith
@zyxwvu use this NSString *savePath = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents/yourFile.txt"];