Sandbox profile

Each app in iOS has a separate folder, which is called a sandbox. Sandbox is used to store local app files, such as audio, video, and image files. In addition, each app’s sandbox is independent, that is, the current app does not have access to the sandbox of other apps, so sandbox storage is more secure.

Sandbox hierarchy

Documents: files that are used for a long time Library: files that are stored in the system TMP: temporary files. Files in this directory will be cleared after the APP is restarted

Sandbox path

Sandbox home: homePath = NSHomeDirectory();

The Documents directory: [homePath stringByAppendingPathComponent: @ “Documents”);

The library directory: [homePath stringByAppendingPathComponent: @ “library”].

TMP: [homePath stringByAppendingPathComponent: @ “TMP”]

Path is a string of a string, stringByAppendingPathComponent will sandbox home directory path joining together a folder name, which means that the sand box every points directory home directory.

Three ways to store sandbox data

  • Plist storage

Plist file storage is generally used to access dictionaries and arrays, write plist files directly, and store them in the application sandbox. Plist storage is only available in ios, it’s ios specific storage.

// Save data - (void)saveForPlist {NSDictionary *dataDict = @{@"name" : @"xiaoming"The @"age": @ 24}; NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)[0]; / / stitching a filename nsstrings * dict = [path stringByAppendingPathComponent: @"data.plist"]; //dictPath = storage path, Yes = thread protection [dataDict writeToFile:dictPath atomically: Yes]; } // Read data - (void)readForPlist { NSString *path = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES)[0]; / / data path nsstrings * dataPath = [path stringByAppendingPathComponent: @"data.plist"];
    NSDictionary *dataDict = [NSDictionary dictionaryWithContentsOfFile:path];
    NSLog(@"% @", dataDict);
}
Copy the code
  • Preference

NSUserDefaults Stores preferences, which do not require paths and are generally used to store information such as accounts and passwords.

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; // Store data [defaults]setObject:@"xiaoming" forKey:@"name"];
    [defaults setInteger:24 forKey:@"age"]; // Write the method to the file immediately. If you do not write this method, you will start writing it asynchronously. } // Read data - (void)readForPreference {

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    
    NSString *name = [defaults objectForKey:@"name"];
    NSInteger *age = [defaults integerForKey:@"age"];
    
    NSLog(@"%@-------%ld",name,age); 
}

Copy the code

Preferences store new data, do not overwrite the original file, will continue in the original file.

  • NSKeyedArchiver (archive)

Archiving is generally used to save custom objects. Because plist files cannot hold custom objects. If a dictionary holds custom objects, if the dictionary is written to a file, it will not generate a PList file.

- (void)saveForNSKeyedArchiver {Person * Person = [Person alloc] init]; person.name = @"Xiao Ming"; person.age = 24; NSString *tempPath = NSTemporaryDirectory(); NSString *filePath = [tempPath stringByAppendingPathComponent:@"Person.data"];
    NSLog(@"% @",tempPath); [NSKeyedArchiver archiveRootObject:per toFile:filePath]; } // Read data: - (void)readNSString *tempPath = NSTemporaryDirectory(); ForNSKeyedArchiver {// Get the sandbox directory NSString *tempPath = NSTemporaryDirectory(); NSString *filePath = [tempPath stringByAppendingPathComponent:@"Person.data"];

    Person *person = [NSKeyedUnarchiver unarchiveObjectWithFile:filePath];
    NSLog(@"%@-----%li",per.name,per.age);
}

Copy the code