storing data locally on the iphone

后端 未结 7 1353
鱼传尺愫
鱼传尺愫 2020-12-02 17:45

I am building an app where I\'d like to store user information locally on the device, without using any server database - but everything on the device side. I am looking to

相关标签:
7条回答
  • 2020-12-02 18:10
    // Create Database file
    
    - (void)createEditableCopyOfDatabaseIfNeeded
    {
        // First, test for existence.
        BOOL success;
        NSFileManager *fileManager = [NSFileManager defaultManager];
        NSError *error;
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *documentsDirectory = [paths objectAtIndex:0];
        NSLog(@"%@",documentsDirectory);
        NSString *writableDBPath = [documentsDirectory stringByAppendingPathComponent:@"TestDB.sqlite"];
        success = [fileManager fileExistsAtPath:writableDBPath];
        if (success) return;
    
        // The writable database does not exist, so copy the default to the appropriate location.
    
        NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"TestDB.sqlite"];
        success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
        if (!success) {
            NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
        }
    }
    
    0 讨论(0)
提交回复
热议问题