Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
461 views
in Technique[技术] by (71.8m points)

iphone - How to use addSkipBackupAttributeToItemAtURL API?

my application rejected due to this issue :

http://i.imgur.com/yajQh.png

My application is a dictionary which uses SQL DBs , with bookmarking and etc ...

so I copied this code from Apple docs in appDelegate.m :

- (BOOL)addSkipBackupAttributeToItemAtURL:(NSURL *)URL
{
    assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);

    NSError *error = nil;
    BOOL success = [URL setResourceValue: [NSNumber numberWithBool: YES]
                                  forKey: NSURLIsExcludedFromBackupKey error: &error];
    if(!success){
        NSLog(@"Error excluding %@ from backup %@", [URL lastPathComponent], error);
    }
    return success;
}

but used like this :

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{

    [self addSkipBackupAttributeToItemAtURL:[NSURL URLWithString:@"<myApplication>/Library/Caches"]];
}

but crashes with this reason :

Assertion failed: ([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]), function -[AppDelegate addSkipBackupAttributeToItemAtURL:], file /iData/Development 2011/My iPhone Project/APPNAME/APPNAME/AppDelegate.m, line 27.

According to that picture is this my only problem for rejection ? because this is my first time I work with data base .

Thanks . ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

EDITED :

- (NSString *) getDBPath
{
    NSInteger kValue = [[[NSUserDefaults standardUserDefaults] stringForKey:@"Bv"] intValue];

    NSString *documentsDir = [[NSString alloc] init];
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory , NSUserDomainMask, YES);
    documentsDir = [paths objectAtIndex:0];


    switch (kValue) {

            case 0:
            documentsDir = [NSString stringWithFormat:@"%@/eng-per.sqlite", documentsDir];
            break;
        case 1:
            documentsDir = [NSString stringWithFormat:@"%@/eng-per.sqlite", documentsDir];
            break;

        case 2:
            documentsDir = [NSString stringWithFormat:@"%@/per-eng.sqlite", documentsDir];
            break;
}

return documentsDir

}

and then changed to this in app delegate.m :

[self addSkipBackupAttributeToItemAtURL:[NSURL fileURLWithPath:dbClass.getDBPath]];

now app lunches fine without crash

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your app is "crashing" because you're hitting that assert on line:

assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);

And I suspect your problem is actually in how you are setting up the URL:

[NSURL URLWithString:@"<myApplication>/Library/Caches"];

How are you getting the path to "<myApplication>"?

You need to get your application's true Cache's folder, which you can do via:

[NSFileManager defaultManager] URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask]; (for the location passed back via file URL's)

or

NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); (for the path in the form of a NSString)

So ultimately, what you need to do is not assert if the key has already been set in defaults and you need to properly set the file URL for the file you're saving.

Create your URL (a file URL, to be precise) using something like:

NSArray * arrayOfURLs = [[NSFileManager defaultManager] URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask];
// make certain there's at least one file url returned by the above call
if([arrayOfURLs count] > 0)
{
    // and this would just be the URL to the cache directory...
    NSURL * cacheDirectoryPath = [arrayOfURLs objectAtIndex: 0];

    // ... to create a file url to your actual dictionary, you'd need to add a
    // filename or path component.

    // Assuming you save this as a property within your object
    self.cacheFileURL = [cacheDirectoryPath URLByAppendingPathComponent: @"myDatabase.db"];
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...