Skip to content

Instantly share code, notes, and snippets.

@andrewgarn
Created July 24, 2012 14:24
Show Gist options
  • Save andrewgarn/3170207 to your computer and use it in GitHub Desktop.
Save andrewgarn/3170207 to your computer and use it in GitHub Desktop.
Skip backup NSURL category for iOS.
//
// NSURL+AGCategory.m
// AGFoundation
//
// Created by Andrew Garn on 24/07/2012.
// Copyright (c) 2012 Andrew Garn. All rights reserved.
//
#import <Foundation/Foundation.h>
#import <UIKit/UIKit.h>
@interface NSURL (AGCategory)
/**
Adds the skip backup extended attribute to the receiver
This requirement was added by apple with the introduction of iCloud. Files stored in the documents directory that have not been created by the user, or do not contain user data, must not be backed up. For more information see the following: http://developer.apple.com/library/ios/#qa/qa1719/_index.html
@return `YES` if the operation was successful, otherwise `NO`.
*/
- (BOOL)addSkipBackupAttribute;
@end
//
// NSURL+AGCategory.m
// AGFoundation
//
// Created by Andrew Garn on 24/07/2012.
// Copyright (c) 2012 Andrew Garn. All rights reserved.
//
#import "NSURL+AGCategory.h"
#include <sys/xattr.h>
@implementation NSURL (AGCategory)
- (BOOL)addSkipBackupAttribute
{
NSString *systemVersion = [[UIDevice currentDevice] systemVersion];
if ([systemVersion floatValue] < 5.0 || [systemVersion isEqualToString:@"5.0.0"])
return YES; // "do not backup" is not yet supported or required
if ([systemVersion isEqualToString:@"5.0.1"])
{
const char *filePath = [[self path] fileSystemRepresentation];
const char *attrName = "com.apple.MobileBackup";
u_int8_t attrValue = 1;
int result = setxattr(filePath, attrName, &attrValue, sizeof(attrValue), 0, 0);
return result == 0;
}
else
{
NSError *error = nil;
BOOL success = [self setResourceValue:[NSNumber numberWithBool:YES]
forKey:NSURLIsExcludedFromBackupKey error:&error];
if(!success)
NSLog(@"Error excluding %@ from backup %@", [self lastPathComponent], error);
return success;
}
return NO;
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment