Cookie 的读取
将它放在 webViewDidFinishLoad 开始后执行
- NSArray *nCookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookies];NSHTTPCookie *cookie;
- for (id c in nCookies)
- {
- if ([c isKindOfClass:[NSHTTPCookie class]]){
- cookie=(NSHTTPCookie *)c;
- NSLog(@"%@: %@", cookie.name, cookie.value);}
- }
2 存储长效数据
我要读取 COOKIE,为了将 COOKIE 保存下来,在下次进入 APP 的时候,再取出来使用,我写的
下面两个方法,可以存取 NSMutableDictionary 类型的数据,这种类型的数据,可以理解为键值对,而且可以很方便的通过下面两行代码读取和修改一个健的值
- 读取
- NSString *cookieValue = [cookieData valueForKey:cookieName];
- 设置 / 增加
- NSMutableDictionary *jsonCookie = [[NSMutableDictionary alloc] init];[jsonCookie setValue:cookieValue forKey:cookieName];
- // 保存数据到本地
- - (BOOL) SetLocalData:(NSString *)dataFile dataObject:(NSMutableDictionary *)dataObject{
- // 设置路径,并保存
- NSString *savePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
- NSString *saveFile = [savePath stringByAppendingPathComponent:dataFile];[NSKeyedArchiver archiveRootObject:dataObject toFile:saveFile];
- return YES;
- }
- // 读取本地保存的数据
- - (NSMutableDictionary *) GetLocalData:(NSString *)dataFile {
- // 按文件名来读取数据
- NSString *savePath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory,NSUserDomainMask, YES) objectAtIndex:0];
- NSString *saveFile = [savePath stringByAppendingPathComponent:dataFile];return [NSKeyedUnarchiver unarchiveObjectWithFile: saveFile];
- }