如何使用addSkipBackupAttributeToItemAtURL API?

Mom*_*omi 7 iphone xcode objective-c ipad ios

我的申请因此问题被驳回:

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

我的应用程序是一个使用SQL DB,书签等的字典...

所以我从appDelegate.m中的Apple文档复制了这段代码:

- (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;
}
Run Code Online (Sandbox Code Playgroud)

但使用如下:

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

    [self addSkipBackupAttributeToItemAtURL:[NSURL URLWithString:@"<myApplication>/Library/Caches"]];
}
Run Code Online (Sandbox Code Playgroud)

但因此原因崩溃:

断言失败:([[的NSFileManager defaultManager] fileExistsAtPath:[URL路径]),功能 - [AppDelegate中addSkipBackupAttributeToItemAtURL:],文件/ IDATA /开发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
Run Code Online (Sandbox Code Playgroud)

}

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

[self addSkipBackupAttributeToItemAtURL:[NSURL fileURLWithPath:dbClass.getDBPath]];
Run Code Online (Sandbox Code Playgroud)

现在应用程序午餐很好没有崩溃

Mic*_*ann 4

您的应用程序正在“崩溃”,因为您在网上点击了该断言:

assert([[NSFileManager defaultManager] fileExistsAtPath: [URL path]]);
Run Code Online (Sandbox Code Playgroud)

我怀疑你的问题实际上在于你如何设置 URL:

[NSURL URLWithString:@"<myApplication>/Library/Caches"];
Run Code Online (Sandbox Code Playgroud)

你如何获得“”的路径<myApplication>

您需要获取应用程序的真实缓存文件夹,可以通过以下方式完成:

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

或者

NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);(对于 NSString 形式的路径)

因此,最终,您需要做的不是断言密钥是否已设置为默认值,而是需要正确设置要保存的文件的文件 URL。

使用以下内容创建 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"];
}
Run Code Online (Sandbox Code Playgroud)

  • 并且您必须使用“NSURL fileURLWithPath:”,而不是“NSURL URLWithString:”从文件路径创建“NSURL”。 (4认同)