1. 程式人生 > >iOS中清除web的cookie方法和快取

iOS中清除web的cookie方法和快取

NSHTTPCookie *cookie;

 NSHTTPCookieStorage *storage = [NSHTTPCookieStorage sharedHTTPCookieStorage];

 for (cookie in [storage cookies])

    {

        [storage deleteCookie:cookie];

    }

 //快取web清除

    [[NSURLCache sharedURLCache] removeAllCachedResponses];

    [self.webView loadRequest:[NSURLRequest

 requestWithURL:url]];

 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];

        }

    }

 //清除某一特定的cookie方法

 NSArray * cookArray = [[NSHTTPCookieStorage sharedHTTPCookieStorage] cookiesForURL:url];

 for (NSHTTPCookie*cookie in cookArray) {

 if ([cookie.name isEqualToString:@"cookiename"]) {

            [[NSHTTPCookieStorage sharedHTTPCookieStorage

] deleteCookie:cookie];

        }

    }