转自:http://blog.csdn.net/huang2009303513/article/details/41309043
hone中在本地存储数据简单的说有三种方式:数据库、NSUserDefaults和文件。
NSUserDefaults用于存储数据量小的数据,例如用户配置。并不是所有的东西都能往里放的,只支持:NSString,NSNumber, NSDate, NSArray, NSDictionary,详细方法可以查看类文件。
NSUserDefaultsstandardUserDefaults用来记录一下永久保留的数据非常方便,不需要读写文件,而是保留到一个 NSDictionary字典里,由系统保存到文件里,系统会保存到该应用下的/Library/Preferences /gongcheng.plist文件中。需要注意的是如果程序意外退出,NSUserDefaultsstandardUserDefaults数据不 会被系统写入到该文件,不过可以使用[[NSUserDefaultsstandardUserDefaults] synchronize]命令直接同步到文件里,来避免数据的丢失。一、将数据存储到NSUserDefaults:
//UISwitch - (IBAction)switchChanged:(id)sender{ NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; [userDefaults setBool:_theSwitch.on forKey:@"switchValue"]; } //UITextField - (IBAction)inputChanged:(id)sender{ NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; [userDefaults setObject:_textField.text forKey:@"inputValue"]; }
二、读取NSUserDefaults中的数据:
//UISwitch NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; BOOL sw = [userDefaults boolForKey:@"switchValue"]; [_theSwitch setOn:sw]; //UITextField NSString *str = [userDefaults stringForKey:@"inputValue"]; [_textField setText:str];
registerDefaults:方法是注册偏好设置的子集,它是不写入到plist文件中的,但在ND中取确实能取到。
也就是说plist文件中看到的数据是你显示的设置进去的。
也就是说plist文件中看到的数据是你显示的设置进去的。
比如调用setxxx方法
异步请求:
- -(BOOL)getOnlyKey1
- {
- NSString *myUUIDStr = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
- __block bool isTrue = false;
- AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
- manager.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
- NSString *urlstr = [NSString stringWithFormat:@"http://122.225.89.70:28080/try/check"];
- NSURL *url = [NSURL URLWithString:urlstr];
- NSDictionary *dic = @{@"imei":myUUIDStr,@"av":AppVersion};
- [manager POST:urlstr parameters:dic success:^(AFHTTPRequestOperation *operation, id responseObject) {
- MyLog(@"%@", operation.responseString);
- NSRange range = [operation.responseString rangeOfString:@""msg":"0""];
- if (range.location != NSNotFound) {
- isTrue = true;
- }
- if (!isTrue) {
- SHOWALERT(@"错误", @"您需要联系开发人员");
- }
- } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
- MyLog(@"返回失败结果:%@", error.localizedFailureReason);
- SHOWALERT(@"错误", @"请求开发人员服务器失败");
- isTrue = true;
- }];
- return isTrue;
- }
同步请求:
- -(BOOL)getOnlyKey2
- {
- NSString *myUUIDStr = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
- BOOL isTrue = false;
- NSString *urlstr = [NSString stringWithFormat:@"http://122.225.89.70:28080/try/check"];
- NSURL *url = [NSURL URLWithString:urlstr];
- NSMutableURLRequest *urlrequest = [[NSMutableURLRequest alloc]initWithURL:url];
- urlrequest.HTTPMethod = @"POST";
- NSString *bodyStr = [NSString stringWithFormat:@"imei=%@&av=%@",myUUIDStr, AppVersion];
- NSData *body = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
- urlrequest.HTTPBody = body;
- AFHTTPRequestOperation *requestOperation = [[AFHTTPRequestOperation alloc] initWithRequest:urlrequest];
- requestOperation.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];
- [requestOperation start];
- [requestOperation waitUntilFinished];
- MyLog(@"%@",requestOperation.responseString);
- NSRange range = [requestOperation.responseString rangeOfString:@""msg":"0""];
- if (range.location != NSNotFound) {
- isTrue = true;
- }
- if (!isTrue) {
- SHOWALERT(@"错误", @"您需要联系开发人员");
- }
- return isTrue;
- }
原生态的同步请求:
- -(BOOL)getOnlyKey
- {
- NSString *myUUIDStr = [[[UIDevice currentDevice] identifierForVendor] UUIDString];
- //应用版本号
- NSDictionary* infoDict =[[NSBundle mainBundle] infoDictionary];
- NSString* versionNum =[infoDict objectForKey:@"CFBundleVersion"];
- NSString *urlString = [NSString stringWithFormat:@"http://122.225.89.70:28080/try/check"];
- NSURL *url = [NSURL URLWithString:urlString];
- NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
- [request setHTTPMethod:@"POST"];
- NSString *bodyStr = [NSString stringWithFormat:@"imei=%@&av=%@",myUUIDStr, versionNum];
- //将nstring转换成nsdata
- NSData *body = [bodyStr dataUsingEncoding:NSUTF8StringEncoding];
- //MyLog(@"body data %@", body);
- [request setHTTPBody:body];
- NSURLResponse *response = nil;
- NSError *error = nil;
- //第二,三个参数是指针的指针,所有要用取址符,这个方法是同步方法。同步操作没有完成,后面的代码不会执行。
- NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
- // NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
- // MyLog(@"返回结果是:%@", str);
- if (error == nil) { //接受到数据,表示工作正常
- NSString *str = [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
- MyLog(@"%@",str);
- NSRange range = [str rangeOfString:@""msg":"0""];
- if (range.location != NSNotFound) {
- return true;
- }else{
- return false;
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"出错鸟"
- message:@"您需要联系项目开发人员"
- delegate:nil
- cancelButtonTitle:@"确定"
- otherButtonTitles:nil];
- [alert show];
- }
- }
- if(error != nil || response == nil)
- {
- return false;
- UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"错误"
- message:@"登陆失败,网络不稳定"
- delegate:nil
- cancelButtonTitle:@"确定"
- otherButtonTitles:nil];
- [alert show];
- }
- return false;
- }