博客> iOS持久化Cookies与清空(可以用来保存登陆状态和退出登陆)
iOS持久化Cookies与清空(可以用来保存登陆状态和退出登陆)
2017-12-10 08:02 评论:0 阅读:411 yangdongming
iOS保持网页登陆状态/退出登陆 cookies持久化与清理 cookies

//本地拿到存储的cookies

NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"cookiesInfo" ofType:@"plist"];
NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
NSArray *cookies = [data objectForKey:@"cookiesKey"];
NSURL *url = [NSURL URLWithString:HomeBseUrl];
if (cookies) {
    if (isClearCookies) {
        //清除cookies
        NSHTTPCookie *cookie;
        NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];
        for (cookie in [storage cookies])
        {
            [storage deleteCookie:cookie];
        }
        //缓存web清除
        [[NSURLCache sharedURLCache] removeAllCachedResponses];
        NSURLCache * cache = [NSURLCache sharedURLCache];
        [cache removeAllCachedResponses];
        [cache setDiskCapacity:0];
        [cache setMemoryCapacity:0];
        if (url) {//清除所有cookie
            NSArray *cookies = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:url];
            for (int i = 0; i < [cookies count]; i++) {
                NSHTTPCookie *cookie = (NSHTTPCookie *)[cookies objectAtIndex:i];
                [[NSHTTPCookieStorage sharedHTTPCookieStorage] deleteCookie:cookie];
            }
        }
    }else{
       //设置cookies 
        NSMutableDictionary *cookieProperties = [NSMutableDictionary dictionary];
        [cookieProperties setObject:[cookies objectAtIndex:0] forKey:NSHTTPCookieName];
        [cookieProperties setObject:[cookies objectAtIndex:1] forKey:NSHTTPCookieValue];
        [cookieProperties setObject:[cookies objectAtIndex:3] forKey:NSHTTPCookieDomain];
        [cookieProperties setObject:[cookies objectAtIndex:4] forKey:NSHTTPCookiePath];
        NSHTTPCookie *cookieuser = [NSHTTPCookie cookieWithProperties:cookieProperties];
        [[NSHTTPCookieStorage sharedHTTPCookieStorage]  setCookie:cookieuser]; 
    } 
}

//设置完后再加载webView loadingrequest

//获取cookies 并持久化

NSHTTPCookie *cookie;
NSHTTPCookieStorage *nCookies = [NSHTTPCookieStorage sharedHTTPCookieStorage];
NSArray *cookiesURL = [nCookies cookiesForURL:[NSURL URLWithString:HomeBseUrl]];
for (id c in cookiesURL)
{
    if ([c isKindOfClass:[NSHTTPCookie class]])
    {
        cookie=(NSHTTPCookie *)c;
        if ([cookie.name isEqualToString:@"PHPSESSID"]) {
            NSNumber *sessionOnly = [NSNumber numberWithBool:cookie.sessionOnly];
            NSNumber *isSecure = [NSNumber numberWithBool:cookie.isSecure];
            NSArray *cookies = [NSArray arrayWithObjects:cookie.name, cookie.value, sessionOnly,          cookie.domain, cookie.path, isSecure, nil];
            NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"cookiesInfo" ofType:@"plist"];
            NSMutableDictionary *data = [[NSMutableDictionary alloc] initWithContentsOfFile:plistPath];
            [data setObject:cookies forKey:@"cookiesKey"];
            NSLog(@"________%@", data);//直接打印数据。
            [data writeToFile:plistPath atomically:YES]; 
            break;
        }
    }
}

//如果存取不成功,可以新建一个名为cookiesInfo的plist文件

用的话自己调整一下存cookies 的地方 我目前在viewWilldisappear 和 应用程序退出时存的

收藏
1
sina weixin mail 回到顶部