Tuy nhiên NSUserDefaults lưu data ra file, không có bảo mật, nên user có thể dễ dàng sửa file, làm game chạy sai.
Dùng class này thì sẽ chống được việc sửa data trực tiếp từ file. Nó hoạt động theo cơ chế tạo thêm data hash cho data cần lưu, rồi lưu luôn vào cùng với data trong NSUserDefaults.
Hạn chế của cách trên là nó không che giấu được data, nó chỉ chống không cho sửa. Do vậy chỉ có thể dùng để lưu param, setting… chứ không lưu username, pass được.
https://github.com/matthiasplappert/Secure-NSUserDefaults
// Configuring user defaults. It is recommended that you do this
// immediately after the app did launch.
[NSUserDefaults setSecret:@"shh, this is secret!"];
// Write secure user defaults
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
[defaults setSecureBool:YES forKey:@"IsRegistered"];
// Read secure user defaults
BOOL valid = NO;
BOOL registered = [defaults secureBoolForKey:@"IsRegistered" valid:&valid];
if (!valid) {
// the property has been modified, handle this situation
} else {
// Valid property, do whatever you need to do
}
Nguồn
No comments:
Post a Comment