我正在尝试使用文本文件来存储用户输入的密码来制作一个简单的密码保护应用程序。我想将文本字段中的内容存储在文件中,并最终将该文件中的内容与用户在另一个文本字段中输入的内容进行比较。这是我所拥有的:
//Setting the string to hold the password the user has entered
NSString *createPassword1 = passwordSet.text;
//creating a muttable array to store the value of createPassword1
NSMutableArray *passwordArray = [NSMutableArray array];
//storing createpassword1 into the first element of the array
[passwordArray addObject:createPassword1];
NSLog(@"%@",[passwordArray objectAtIndex:0]);//seeing if it is stored correctly (it is)
//path for searching for the file
NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
//my filename
NSString *fileName = @"PasswordFile.txt";
NSString *fileAndPath = [path stringByAppendingPathComponent:fileName];
if (![[NSFileManager defaultManager] fileExistsAtPath:fileAndPath]) {
[[NSFileManager defaultManager] createFileAtPath:fileAndPath contents:nil attributes:nil];
}
[[[passwordArray objectAtIndex:0] dataUsingEncoding:NSUTF8StringEncoding] writeToFile:fileAndPath atomically:YES];
任何帮助将不胜感激,谢谢。
最佳答案
你做的事情太复杂了。 为什么使用 NSMutableArray(“passwordArray”)来存储单个密码? 为什么要将其转换为 NSData 并将其写入文件? 只需使用一个字符串并使用它的 writeToFile 方法即可。 或者使用 NSArray 的 writeToFile 方法。
或者,也是我个人最喜欢的:使用 NSUSerDefaults à la:
[[NSUserDefaults standardUserDefaults] setValue: myPasswordString forKey:@"appPassword"]];
编辑以回应一些评论: 上述内容仅适用于需要以非常低级别的方式进行密码保护的“普通”应用程序。任何保护真正敏感数据的事情都应该以不同的方式处理。 原发帖者明确指出
I want to take whats in a text field store it in a file and ultimately compare whats in that file to what the user enters in another text field.
因此,人们可以假设高级安全性在这里不是问题。
关于iphone - 将用户在文本字段中输入的内容存储在文本文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13920633/