0

ユーザーが入力したパスワードを保存するテキストファイルを使用して、簡単なパスワードで保護されたアプリを作成しようとしています。テキストフィールドの内容をファイルに保存し、最終的にそのファイルの内容をユーザーが別のテキストフィールドに入力した内容と比較したいと思います。これが私が持っているものです:

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

どんな助けでも大歓迎ですありがとう。

4

1 に答える 1

1

あなたがすることはあまりにも複雑です。NSMutableArray( "passwordArray")を使用して単一のパスワードを保存するのはなぜですか?なぜそれをNSDataに変換し、これをファイルに書き込むのですか?文字列を使用し、そのwriteToFileメソッドを使用するだけです。または、NSArrayのwriteToFileメソッドを使用します。

または、私の個人的なお気に入り:NSUSerDefaultsàlaを使用します。

[[NSUserDefaults standardUserDefaults] setValue: myPasswordString forKey:@"appPassword"]];

いくつかのコメントに応じて編集する:上記は、非常に低レベルの方法でパスワード保護を必要とする「些細な」アプリで使用される場合にのみ適用されます。本当に機密データを保護するものはすべて、別の方法で処理する必要があります。元のポスターは明示的に述べています

テキストフィールドの内容をファイルに保存し、最終的にそのファイルの内容をユーザーが別のテキストフィールドに入力した内容と比較したいと思います。

したがって、ここでは高レベルのセキュリティは問題ではないと想定できます。

于 2012-12-17T19:21:29.347 に答える