iphone - 将用户在文本字段中输入的内容存储在文本文件中?

标签 iphone objective-c ios xcode

我正在尝试使用文本文件来存储用户输入的密码来制作一个简单的密码保护应用程序。我想将文本字段中的内容存储在文件中,并最终将该文件中的内容与用户在另一个文本字段中输入的内容进行比较。这是我所拥有的:

 //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/

相关文章:

iphone - 播放短音效

ios - 如何制作在 iOS Facebook Messenger 应用程序而不是应用程序内网络浏览器的 Safari 中打开的 url

ios - 标准附件 View 可以位于 UITableViewCell 中的不同位置吗?

ios - Xcode6 和 ios8 应用程序企业分发

ios - 实现 SlideView 动画 SlideFromTop 和 SlideFromBottom

iphone - 在我的 ios 应用程序中哪里显示免责声明

iphone - 处理 UIViewController 中的粘贴事件

objective-c - 我拖到 xcode 的文本文件在 iphone/模拟器上放在哪里?

ios - 如何取消多个延迟的 performSelector 调用

objective-c - 无法使用 NSFileManager 创建新目录,所有者为 `root`