iOS使用NSHTTPCookieStorage 把用户登陆名等写入到浏览器.在登出的时候删除

一:设置cookie


    NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];                  

      //设置cookie名
       [cookieProperties setObject:@"ccoo" forKey:NSHTTPCookieName];
      //设置cookie值
        [cookieProperties setObject:[NSString stringWithFormat:@"uid=%@&username=%@&nickname=%@&roleimg=%@&usiteid=%@",app.appUserId,app.appUserName,app.appRoleName,app.appUserLogo,app.appUsiteId] forKey:NSHTTPCookieValue];
         //设置cookie的路径,cookieHost 是一个url地址
        [cookieProperties setObject:[cookieHost host] forKey:NSHTTPCookieDomain];
        //设置cookie 过期时间
        [cookieProperties setValue:[NSDate dateWithTimeIntervalSinceNow:3600*3600] forKey:NSHTTPCookieExpires];
        //设置cookie相对地址
        [cookieProperties setObject:@"cnrainbird.com" forKey:NSHTTPCookieOriginURL];
        //这个一定要写,一般都写“/”
        [cookieProperties setObject:@"/" forKey:NSHTTPCookiePath];
        //默认版本cookie 0
        [cookieProperties setObject:@"0" forKey:NSHTTPCookieVersion];
        //使用cookieProperties字典初始化NSHTTPCookie
        NSHTTPCookie *cookie1 = [NSHTTPCookie cookieWithProperties:cookieProperties];
        //NSHTTPCookieStorage 管理器去设置cookie
        [[NSHTTPCookieStorage sharedHTTPCookieStorage] setCookie:cookie1];

 二:查看cookie

            //查看cookie

            NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];

                 for (NSHTTPCookie *cookie in [cookieJar cookies]) {

                NSLog(@" \n  login 查 看cookies ========logoin=%@\n", cookie);

            }

            

  三:清空cookie

           NSHTTPCookieStorage *cookieJar = [NSHTTPCookieStorage sharedHTTPCookieStorage];

            NSArray *_tmpArray = [NSArray arrayWithArray:[cookieJar cookies]];

            for (id obj in _tmpArray) {

                [cookieJar deleteCookie:obj];

            }

            


你可能感兴趣的:(iOS使用NSHTTPCookieStorage 把用户登陆名等写入到浏览器.在登出的时候删除)