How to manage the data base in iOS
I have to make an iPhone application. I have one button and 开发者_如何学Pythonbutton name is registration in xib if I click the button then registration form will be open
the registration form is created in web server if the user fill the registration form suppose he fill the username and password in registration it will save in server and same username and password should save in or local database also.
You have 3 main options for persisting the login data; Settings, Core Data and SqlLite. For just a username and password you only need to use the Settings as the overhead of the other two options would be excessive and they are more often used for relational data.
To use Settings you need to load them when the application controller initialises by calling;
+ (void) loadDefaultSettings {
NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *pListPath = [path stringByAppendingPathComponent:@"Settings.bundle/Root.plist"];
NSDictionary *pList = [NSDictionary dictionaryWithContentsOfFile:pListPath];
NSMutableArray *prefsArray = [pList objectForKey:@"PreferenceSpecifiers"];
NSMutableDictionary *regDictionary = [NSMutableDictionary dictionary];
for (NSDictionary *dict in prefsArray) {
NSString *key = [dict objectForKey:@"Key"];
if (key) {
id value = [dict objectForKey:@"DefaultValue"];
[regDictionary setObject:value forKey:key];
}
}
[[NSUserDefaults standardUserDefaults] registerDefaults:regDictionary];
}
You can then store your username and password with a method call to this;
+ (void) storePersistedLogin:(NSString *)uid withPassword:(NSString *)pswd {
[[NSUserDefaults standardUserDefaults] setObject:uid forKey:@"UID"];
[[NSUserDefaults standardUserDefaults] setObject:pswd forKey:@"PSWD"];
}
You can populate the UITextField inputs using the following method;
+ (void) populatePersistedLogin:(UITextField **)uid andPassword:(UITextField **)pswd {
[*uid setText:(NSString *)[[NSUserDefaults standardUserDefaults] objectForKey:@"UID"]];
[*pswd setText:(NSString *)[[NSUserDefaults standardUserDefaults] objectForKey:@"PSWD"]];
}
I have these public methods in a separate SettingsUtils class so I can make sure my keys are the same. You could also declare the keys as NSString objects instead to reduce the chance of mis-typing them.
精彩评论