Skip to content

Instantly share code, notes, and snippets.

@jblocksom
Created January 7, 2014 18:02
Show Gist options
  • Save jblocksom/8303676 to your computer and use it in GitHub Desktop.
Save jblocksom/8303676 to your computer and use it in GitHub Desktop.
Some handy snippets for the iCloud chapter
#pragma mark - iCloud
- (BOOL)hasiCloud
{
BOOL hasiCloud = ([self ubiquitousDocumentsURL] != nil);
return hasiCloud;
}
- (void)setUseiCloud:(BOOL)value
{
if (_useiCloud != value)
{
_useiCloud = value;
[[NSUserDefaults standardUserDefaults] setBool:_useiCloud forKey:kUseiCloud];
dispatch_queue_t defaultQ;
defaultQ = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
NSURL *baseURL = (_useiCloud ? [self ubiquitousDocumentsURL] : [self localDocumentsURL]);
for (NSURL *url in [self.documentURLs copy])
{
dispatch_async(defaultQ, ^{
NSURL *destinationURL = [baseURL URLByAppendingPathComponent:[url lastPathComponent]];
NSFileManager *fileManager = [[NSFileManager alloc] init];
NSError *error = nil;
BOOL success = [fileManager setUbiquitous:_useiCloud
itemAtURL:url
destinationURL:destinationURL
error:&error];
if (!success)
{
NSLog (@"%@ unable to save from %@ to %@", self, url, destinationURL);
}
else
{
NSLog (@"%@ successful save from %@ to %@", self, url, destinationURL);
[self addDocumentURL:destinationURL];
}
});
}
[self clearOutAndRefreshDocumentURLs];
}
}
-(UIView *)cloudToolbarView
{
UIView *cloudView = [[UIView alloc] initWithFrame:CGRectMake(0.0, 0.0, 160.0, 33.0)];
UILabel *cloudLabel = [[UILabel alloc] initWithFrame:CGRectMake(0.0, 3.0, 80.0, 27.0)];
cloudLabel.text = NSLocalizedString(@"Use iCloud", @"Use iCloud");
cloudLabel.font = [UIFont boldSystemFontOfSize:[UIFont systemFontSize]];
cloudLabel.textColor = [UIColor whiteColor];
cloudLabel.backgroundColor = [UIColor clearColor];
cloudLabel.shadowColor = [UIColor colorWithWhite:0.0 alpha:0.5];
[cloudView addSubview:cloudLabel];
UISwitch *cloudSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(81.0, 3.0, 79.0, 27.0)];
cloudSwitch.enabled = [[BNRDocumentStorage sharedDocumentStorage] hasICloud];
cloudSwitch.on = [[BNRDocumentStorage sharedDocumentStorage] useiCloud];
[cloudSwitch addTarget:self
action:@selector(changeiCloud:)
forControlEvents:UIControlEventValueChanged];
[self setCloudSwitch:cloudSwitch];
[cloudView addSubview:cloudSwitch];
return cloudView;
}
-(void)awakeFromNib
{
self.navigationItem.leftBarButtonItem = self.editButtonItem;
UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:NULL];
UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithCustomView:[self cloudToolbarView]];
UIBarButtonItem *item3 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:NULL];
self.toolbarItems = [NSArray arrayWithObjects:item1, item2, item3, nil];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment