iCloud using document based
I have a master detail view controller which shows the files and when i
select a cell it opens up a textview and a save button which saves the
content on the iCloud.But it doesnt seem to work.Here is the code
MasterViewController.h
#import <UIKit/UIKit.h>
#import "Note.h"
#define kFILENAME @"mydocument.dox"
@interface MasterViewController : UITableViewController
@property(strong)Note *doc;
@property(strong)NSMetadataQuery *query;
-(void)loadDocument;
@end
MasterViewController.m
#import "MasterViewController.h"
#import "DetailViewController.h"
@interface MasterViewController () {
NSMutableArray *_objects;
}
@end
@implementation MasterViewController
@synthesize doc=_doc;
@synthesize query=_query;
- (void)awakeFromNib
{
[super awakeFromNib];
}
- (void)viewDidLoad
{
[super viewDidLoad];
[self loadDocument];
// Do any additional setup after loading the view, typically from a nib.
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *addButton = [[UIBarButtonItem alloc]
initWithBarButtonSystemItem:UIBarButtonSystemItemAdd target:self
action:@selector(insertNewObject:)];
self.navigationItem.rightBarButtonItem = addButton;
}
-(void)loadDocument
{
NSMetadataQuery *query=[[NSMetadataQuery alloc]init];
_query=query;
[query setSearchScopes:[NSArray
arrayWithObject:NSMetadataQueryUbiquitousDocumentsScope]];
NSPredicate *pred=[NSPredicate predicateWithFormat:@"%K ==
%@",NSMetadataItemFSNameKey
,kFILENAME];
[query setPredicate:pred];
[[NSNotificationCenter defaultCenter]addObserver:self
selector:@selector(queryDidFinishGathering:)
name:NSMetadataQueryDidFinishGatheringNotification object:query];
[query startQuery];
}
-(void)queryDidFinishGathering:(NSNotification*)notification
{
NSMetadataQuery *query=[notification object];\
[query disableUpdates];
[query stopQuery];
[[NSNotificationCenter defaultCenter]removeObserver:self
name:NSMetadataQueryDidFinishGatheringNotification object:query];
_query=nil;
[self loadData:query];
}
-(void)loadData:(NSMetadataQuery*)query
{
if ([query resultCount]==1) {
NSMetadataItem *item=[query resultAtIndex:0];
NSURL *url=[item valueForAttribute:NSMetadataItemURLKey];
Note *doc=[[Note alloc]initWithFileURL:url];
self.doc=doc;
[self.doc openWithCompletionHandler:^(BOOL success){
if(success)
{
NSLog(@"iCloud Document Opened");
}
else
{
NSLog(@"Failed Opening the document");
NSURL *ubiq=[[NSFileManager
defaultManager]URLForUbiquityContainerIdentifier:nil];
NSURL *ubiquitousPackage=[[ubiq
URLByAppendingPathComponent:@"Documents"]URLByAppendingPathComponent:kFILENAME];
Note *doc=[[Note alloc]initWithFileURL:ubiquitousPackage];
self.doc=doc;
[doc saveToURL:[doc fileURL]
forSaveOperation:UIDocumentSaveForCreating
completionHandler:^(BOOL success){
if (success) {
[doc openWithCompletionHandler:^(BOOL success) {
NSLog(@"new document opened from iCloud");
}];
}
}];
}
}];
}
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (void)insertNewObject:(id)sender
{
if (!_objects) {
_objects = [[NSMutableArray alloc] init];
}
[_objects insertObject:[NSDate date] atIndex:0];
NSIndexPath *indexPath = [NSIndexPath indexPathForRow:0 inSection:0];
[self.tableView insertRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationAutomatic];
}
#pragma mark - Table View
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
return 1;
}
- (NSInteger)tableView:(UITableView *)tableView
numberOfRowsInSection:(NSInteger)section
{
return _objects.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView
cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
UITableViewCell *cell = [tableView
dequeueReusableCellWithIdentifier:@"Cell" forIndexPath:indexPath];
NSDate *object = _objects[indexPath.row];
cell.textLabel.text = [object description];
return cell;
}
- (BOOL)tableView:(UITableView *)tableView
canEditRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the specified item to be editable.
return YES;
}
- (void)tableView:(UITableView *)tableView
commitEditingStyle:(UITableViewCellEditingStyle)editingStyle
forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[_objects removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath]
withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into
the array, and add a new row to the table view.
}
}
/*
// Override to support rearranging the table view.
- (void)tableView:(UITableView *)tableView moveRowAtIndexPath:(NSIndexPath
*)fromIndexPath toIndexPath:(NSIndexPath *)toIndexPath
{
}
*/
/*
// Override to support conditional rearranging of the table view.
- (BOOL)tableView:(UITableView *)tableView
canMoveRowAtIndexPath:(NSIndexPath *)indexPath
{
// Return NO if you do not want the item to be re-orderable.
return YES;
}
*/
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
if ([[segue identifier] isEqualToString:@"showDetail"]) {
NSIndexPath *indexPath = [self.tableView indexPathForSelectedRow];
NSDate *object = _objects[indexPath.row];
[[segue destinationViewController] setDetailItem:object];
}
}
Note.h
#import <UIKit/UIKit.h>
@interface Note : UIDocument
@property (strong) NSString * noteContent;
@end
Note.m
#import "Note.h"
@implementation Note
- (BOOL)loadFromContents:(id)contents ofType:(NSString *)typeName
error:(NSError **)outError
{
if ([contents length] > 0) {
self.noteContent = [[NSString alloc]
initWithBytes:[contents bytes]
length:[contents length]
encoding:NSUTF8StringEncoding];
} else {
// When the note is first created, assign some default content
self.noteContent = @"Empty";
}
return YES;
}
// Called whenever the application (auto)saves the content of a note
- (id)contentsForType:(NSString *)typeName error:(NSError **)outError
{
if ([self.noteContent length] == 0) {
self.noteContent = @"Empty";
}
return [NSData dataWithBytes:[self.noteContent UTF8String]
length:[self.noteContent length]];
}
@end
DetailViewController.h
#import <UIKit/UIKit.h>
@interface DetailViewController : UIViewController
@property (strong, nonatomic) id detailItem;
@property(strong)Note *doc;
- (IBAction)saveClicked:(id)sender;
@property (weak, nonatomic) IBOutlet UILabel *detailDescriptionLabel;
@end
DetailViewController.m
#import "Note.h"
#import "DetailViewController.h"
#define kFILENAME @"mydocument.dox"
@interface DetailViewController ()
- (void)configureView;
@end
@implementation DetailViewController
#pragma mark - Managing the detail item
- (void)setDetailItem:(id)newDetailItem
{
if (_detailItem != newDetailItem) {
_detailItem = newDetailItem;
// Update the view.
[self configureView];
}
}
- (void)configureView
{
// Update the user interface for the detail item.
if (self.detailItem) {
self.detailDescriptionLabel.text = [self.detailItem description];
}
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[self configureView];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)saveClicked:(id)sender {
NSURL *ubiq=[[NSFileManager
defaultManager]URLForUbiquityContainerIdentifier:nil];
NSURL *ubiquitousPackage=[[ubiq
URLByAppendingPathComponent:@"Documents"]URLByAppendingPathComponent:kFILENAME];
Note *doc=[[Note alloc]initWithFileURL:ubiquitousPackage];
self.doc=doc;
[doc saveToURL:[doc fileURL]
forSaveOperation:UIDocumentSaveForCreating completionHandler:^(BOOL
success){
if (success) {
[doc openWithCompletionHandler:^(BOOL success) {
NSLog(@"new document opened from iCloud");
}];
}
else
{
NSLog(@"I dont know");
}
}];
}
@end
No comments:
Post a Comment