如何在iPhone的docs目录中编写xml文件

时间:2012-02-03 13:03:18

标签: iphone xml passwords

我需要将我的用户名和密码以这种格式存储到xml文件中

   <user>abcde</user>
   <pass>123355</pass>

如何编写此文件以便我可以轻松阅读?

1 个答案:

答案 0 :(得分:0)

正如@Mike所建议的那样..你应该使用钥匙串来存储密码..你可以在线浏览。

至于xml存储。 这是代码

     NSData *serializedData;
        NSString *error;
        serializedData = [NSPropertyListSerialization dataFromPropertyList:YourArray(You can use dictionaries.strings..and others too)
        format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
        if (serializedData) {
        // Serialization was successful, write the data to the file system // Get an array of paths.

       NSArray *documentDirectoryPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        NSString *docDir = [NSString stringWithFormat:@”%@/serialized.xml”,
         [documentDirectoryPath objectAtIndex:0]];
        [serializedData writeToFile:docDir atomically:YES];
 }
        else {
        // An error has occurred, log it
        NSLog(@”Error: %@”,error); }
        }