zoukankan      html  css  js  c++  java
  • iOS使用cookie

    //
    //  ViewController.m
    //  cookie
    //
    //  Created by 谭启宏 on 16/3/4.
    //  Copyright © 2016年 tqh. All rights reserved.
    //
    
    
    #import "ViewController.h"
    
    static NSString*kLocalCookieName =@"MyProjectCookie";
    static NSString*kLocalUserData =@"MyProjectLocalUser";
    static NSString*kServerSessionCookie =@"JSESSIONID";
    @interface ViewController ()
    
    @end
    
    @implementation ViewController
    
    - (void)viewDidLoad {
        [super viewDidLoad];
        // Do any additional setup after loading the view, typically from a nib.
    }
    
    - (void)didReceiveMemoryWarning {
        [super didReceiveMemoryWarning];
        // Dispose of any resources that can be recreated.
    }
    //保存cookie
    + (void)saveLoginSession {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSArray *allCookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage]cookies];
        NSMutableDictionary *cookieDictionary;
        for (NSHTTPCookie *cookie in allCookies) {
            if ([cookie.name isEqualToString:kServerSessionCookie]) {
                cookieDictionary = [NSMutableDictionary dictionaryWithDictionary:[defaults dictionaryForKey:kLocalCookieName]];
                [cookieDictionary setValue:cookie.properties forKey:@"cookieDict"];
                [defaults setValue:cookieDictionary forKey:kLocalCookieName];
                [self updateSession];
                [defaults synchronize];
                break;
            }else if([cookie.name isEqualToString:@"jsessionId"]){
                
            }
        }
    }
    //更新cookie
    + (void)updateSession {
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        NSDictionary *cookieDictionary = [defaults dictionaryForKey:kLocalCookieName];
        NSLog(@"%@",cookieDictionary);
        NSDictionary *cookieProperties = [cookieDictionary valueForKey:@"cookieDict"];
        if (cookieProperties != nil) {
            NSHTTPCookie *cookie = [NSHTTPCookie cookieWithProperties:cookieProperties];
            [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie];
        }
    }
    //删除cookie
    + (void)removeLoginSession{
        NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
        [defaults removeObjectForKey:kLocalCookieName];
        [defaults synchronize];
    }
    @end
  • 相关阅读:
    初识ES6
    初识NODE
    AJAX详解
    PHP 文件与目录操作函数总结
    PHP封装一个通用好用的文件上传处理类
    PHP基础之 错误处理 及 异常处理
    PHP基础OOP(二) 多态
    PHP基础之 重载 的实现方式
    CCS3的过渡、变换、动画以及响应式布局、弹性布局
    CSS之盒子模型(由浅到深的理解)
  • 原文地址:https://www.cnblogs.com/hxwj/p/5242410.html
Copyright © 2011-2022 走看看