Database Reference
In-Depth Information
Next we need to modify our viewDidLoad so that we can initialize our entries NSMutableArray and
call a new method that we will create shortly:
- (void)viewDidLoad
{
[super viewDidLoad];
_entries = [NSMutableArray array];
[self reload];
}
Now, just above your UICollectionViewDatasource methods we need to create this reload method.
It is very simple. We will remove all the objects from our entries array and call another method called
loadLocal that we haven't created yet.
-(void)reload {
[_entries removeAllObjects];
[self loadLocal];
}
In the section we created called Data Methods we need to create our loadLocal method:
-(void)loadLocal {
NSArray *localDocuments = [[NSFileManager defaultManager] contentsOfDirectoryAtURL:[AppDelegate
applicationDocumentsDirectory] includingPropertiesForKeys:nil options:0 error:nil];
[localDocuments enumerateObjectsUsingBlock:^(NSURL *fileURL, NSUInteger idx, BOOL *stop) {
if([[fileURL pathExtension] isEqualToString:CT_EXTENSION]){
[self loadDocumentAtFileURL:fileURL];
}
}];
[self.navigationItem.rightBarButtonItem setEnabled:YES];
}
The first line in this method gets an array of URLs inside our application's documents directory by
calling contentsOfDirectoryAtURL:includingPropertiesForKeys:options:error: . We then use the
enumeration method enumerateObjectsUsingBlock: on our NSArray to loop through the URLs. We
compare the extension of each URL with the file extension that we defined in our CTDocument header
file. If the NSURL has our document extension we call the method loadDocumentAtFileURL: and pass
it the file URL. Let's write that method now.
-(void)loadDocumentAtFileURL:(NSURL *)fileURL {
CTDocument *document = [[CTDocument alloc] initWithFileURL:fileURL];
[document openWithCompletionHandler:^(BOOL success) {
if(!success){
NSLog(@"Unable to open document at %@",fileURL);
return;
}
 
Search WWH ::




Custom Search