【发布时间】:2012-12-17 19:16:45
【问题描述】:
我正在尝试使用文本文件创建一个简单的受密码保护的应用程序来存储用户输入的密码。我想将文本字段中的内容存储在一个文件中,并最终将该文件中的内容与用户在另一个文本字段中输入的内容进行比较。这是我所拥有的:
//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];
任何帮助将不胜感激。
【问题讨论】:
-
制作
.plist文件而不是.txt文件..... !!并为它做谷歌,有很多示例代码......祝你好运! -
将明文密码写入文本文件是一个糟糕的想法(非常不安全)。你真的应该为此使用钥匙串。从
GenericKeychain示例应用程序中获取KeychainItemWrapper类。 -
是的,同意...钥匙链更好!
-
“我正在尝试制作一个简单的受密码保护的应用程序” - 对于这个简化的用途(我猜这个应用程序没有处理任何国家机密)我认为密码学并不是真正必要的,它就足够了将密码存储在文档目录中。
-
@Mario 也许——但指出更好的方法是有帮助的。 OP 可能不了解他们的决定的影响。需求随时间而变化。使用钥匙串可能是更好的长期选择。
标签: iphone objective-c ios xcode