我是Xcode的新手,我需要知道如何从属性列表中检索数据进行登录。我会使用它来登录吗?有什么办法让代码变得更好?我给了你所有的代码,希望能找到一种方法。
我的.h文件
@interface Register : UITableViewController{
IBOutlet UITextField *usernameEntered;
IBOutlet UITextField *passwordEntered;
NSString *userName;
NSMutableArray *password;
}
@property (nonatomic, retain) UITextField *usernameEntered;
@property (nonatomic, retain) UITextField *passwordEntered;
@property (nonatomic, retain) NSString *userName;
@property (nonatomic, retain) NSMutableArray *password;
- (IBAction) saveData;
- (IBAction) textFieldReturn:(id)textField;
我的.m文件
@implementation Register
// we use this to dismiss the keyboard when the return key is pressed
- (IBAction) textFieldReturn:(id)textField
{
[textField resignFirstResponder];
}
@synthesize userName;
@synthesize password;
@synthesize usernameEntered, passwordEntered;
- (IBAction) saveData {
// get paths from root direcory
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"];
// set the variables to the values in the text fields
self.userName = usernameEntered.text;
self.password = [[NSMutableArray alloc] initWithCapacity:3];
[password addObject:passwordEntered.text];
// create dictionary with values in UITextFields
NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: usernameEntered, passwordEntered, nil] forKeys:[NSArray arrayWithObjects: @"Username", @"Password", nil]];
// create dictionary with values in UITextFields
NSString *error = nil;
// create NSData from dictionary
NSData *plistData = [NSPropertyListSerialization dataFromPropertyList:plistDict format:NSPropertyListXMLFormat_v1_0 errorDescription:&error];
// check is plistData exists
if(plistData)
{
// write plistData to our Data.plist file
[plistData writeToFile:plistPath atomically:YES];
}
else
{
NSLog(@"Error in saveData: %@", error);
[error release];
}
}
-(void)viewDidLoad
{
[super viewDidLoad];
// Data.plist code
// get paths from root direcory
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"];
// check to see if Data.plist exists in documents
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
// if not in documents, get property list from main bundle
plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
}
// read property list into memory as an NSData object
NSData *plistXML = [[NSFileManager defaultManager] contentsAtPath:plistPath];
NSString *errorDesc = nil;
NSPropertyListFormat format;
// convert static property liost into dictionary object
NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization propertyListFromData:plistXML mutabilityOption:NSPropertyListMutableContainersAndLeaves format:&format errorDescription:&errorDesc];
if (!temp)
{
NSLog(@"Error reading plist: %@, format: %d", errorDesc, format);
}
// assign values
self.userName = [temp objectForKey:@"Username"];
self.password = [NSMutableArray arrayWithArray:[temp objectForKey:@"Password"]];
// display values
usernameEntered.text = userName;
passwordEntered.text = [password objectAtIndex:0];
}
答案 0 :(得分:1)
NSDictionary包含一个名为writeToFile的方法:atomically:它只是自动将字典写入plist,所以你不必自己动手。您可以将saveData方法更改为
-(void)saveData {
// get paths from root direcory
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"];
// set the variables to the values in the text fields
self.userName = usernameEntered.text;
self.password = [[NSMutableArray alloc] initWithCapacity:3];
[password addObject:passwordEntered.text];
// create dictionary with values in UITextFields
NSDictionary *plistDict = [NSDictionary dictionaryWithObjects: [NSArray arrayWithObjects: usernameEntered, passwordEntered, nil] forKeys:[NSArray arrayWithObjects: @"Username", @"Password", nil]];
// create dictionary with values in UITextFields
[plistDict writeToFile:plistPath atomically:YES]; // write data to plist
}
将viewDidLoad更改为以下内容:
-(void)viewDidLoad
{
[super viewDidLoad];
// Data.plist code
// get paths from root direcory
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
// get documents path
NSString *documentsPath = [paths objectAtIndex:0];
// get the path to our Data/plist file
NSString *plistPath = [documentsPath stringByAppendingPathComponent:@"Data.plist"];
// check to see if Data.plist exists in documents
if (![[NSFileManager defaultManager] fileExistsAtPath:plistPath])
{
// if not in documents, get property list from main bundle
plistPath = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"];
}
NSDictionary *temp = [NSDictionary dictionaryWithContentsOfFile:plistPath];
if (!temp)
{
NSLog(@"Error reading plist.");
}
// assign values
self.userName = [temp objectForKey:@"Username"];
self.password = [NSMutableArray arrayWithArray:[temp objectForKey:@"Password"]];
// display values
usernameEntered.text = userName;
passwordEntered.text = [password objectAtIndex:0];
}
您也可能需要考虑加密密码。有关详细信息,请参阅此处:AES Encryption for an NSString on the iPhone