进击的UI------------网络判断
生活随笔
收集整理的這篇文章主要介紹了
进击的UI------------网络判断
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
轉自:http://blog.csdn.net/huang2009303513/article/details/41309043 NSUserDefaults standardUserDefaults的使用 hone中在本地存儲數據簡單的說有三種方式:數據庫、NSUserDefaults和文件。
也就是說plist文件中看到的數據是你顯示的設置進去的。 比如調用setxxx方法 AFN同步異步請求-(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;?? } ?
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文件中看到的數據是你顯示的設置進去的。 比如調用setxxx方法 AFN同步異步請求
異步請求:
?
[objc] view plaincopy?
?
[objc] view plaincopy?
?
[objc] view plaincopy轉載于:https://www.cnblogs.com/sharkHZ/p/4984060.html
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的进击的UI------------网络判断的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: CSS左中右布局,规范案例
- 下一篇: (软件工程复习核心重点)第八章面向对象方