I am setting up a blog app to save the webviews as HTML files in the app's Documents Directory:
I check the directory, and it does properly save the files. I then set up a new TableView Class with XIB to display the files in tableview using this code after setting an NSArray property in the .h file called 'files'
I have just two questions on how to go from here. First question: when I save a new article, and then go to the TableView, it will not show the file just saved until after I restart the app...How do I set it to reload the data when I view the TableView to search for new files that may be in there? Second question: what should I put in the didSelectRowAtIndexPath so that when I click on a file it opens up the webview and load that selected HTML in a UIWebView?
Thanks
Code:
-(IBAction)save {
self.titleofURL = [_webView stringByEvaluatingJavaScriptFromString:@"document.title"];
NSString *urlString = _webView.request.URL.absoluteString;
NSURL *url = [NSURL URLWithString:urlString];
NSData *file = [NSData dataWithContentsOfURL:url];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *pdfPath = [documentsDirectory stringByAppendingPathComponent:[self.titleofURL stringByAppendingString:@".html"]];
[file writeToFile:pdfPath atomically:YES];
}
Code:
- (void)viewDidLoad {
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectoryPath = [paths objectAtIndex:0];
NSFileManager *manager = [NSFileManager defaultManager];
self.files = [manager contentsOfDirectoryAtPath:documentsDirectoryPath error:nil];
[super viewDidLoad];
}
#pragma mark -
#pragma mark Table view data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
// Return the number of sections.
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
// Return the number of rows in the section.
return [self.files count];
}
// Customize the appearance of table view cells.
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
static NSString *CellIdentifier = @"Cell";
UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
if (cell == nil) {
cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:CellIdentifier] autorelease];
}
cell.textLabel.text = [self.files objectAtIndex:indexPath.row];
return cell;
}
Thanks