2015-05-04 163 views
1

我使用的是Web服务与简单的头认证从http头删除cookie?

​​

,如果我输入了错误的密码,我在为theprevious登录我与

- (void)resetCredintialCache { 
    NSDictionary *credentialsDict = [[NSURLCredentialStorage sharedCredentialStorage] allCredentials]; 
    if ([credentialsDict count] > 0) { 
     // the credentialsDict has NSURLProtectionSpace objs as keys and dicts of userName => NSURLCredential 
     NSEnumerator *protectionSpaceEnumerator = [credentialsDict keyEnumerator]; 
     id urlProtectionSpace; 
     // iterate over all NSURLProtectionSpaces 
     while (urlProtectionSpace = [protectionSpaceEnumerator nextObject]) { 
      NSEnumerator *userNameEnumerator = [credentialsDict[urlProtectionSpace] keyEnumerator]; 
      id userName; 
      // iterate over all usernames for this protectionspace, which are the keys for the actual NSURLCredentials 
      while (userName = [userNameEnumerator nextObject]) { 
       NSURLCredential *cred = credentialsDict[urlProtectionSpace][userName]; 
       [[NSURLCredentialStorage sharedCredentialStorage] removeCredential:cred forProtectionSpace:urlProtectionSpace]; 
      } 
     } 
     NSURLCache *sharedCache = [NSURLCache sharedURLCache]; 
     [sharedCache removeAllCachedResponses]; 
     NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; 

     NSArray *cookies = [cookieStorage cookies]; 
     for (NSHTTPCookie *cookie in cookies) { 

      [cookieStorage deleteCookie:cookie]; 
     } 
    } 
} 

但注销后清零cokies注销用户。如何从HTTP请求的头部删除Cookie?

+0

你有没有通过这个帖子了... http://stackoverflow.com/questions/5285940/correct-way-to-delete-cookies-server-side – Vizllx

+0

如何使用这一个? –

+0

哪一个,你需要澄清。 – Vizllx

回答

1

NSURLRequest有一个cachePolicy属性,它指定请求的缓存行为。

设置下面的缓存策略NSURLRequestReloadIgnoringLocalCacheData当像下面这样做请求的时候会从url中加载数据而不是从缓存中加载数据。

NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:10]; 

NSURLRequestReloadIgnoringLocalCacheData

指定为URL负载中的数据应该从 始发源加载。不应使用现有的缓存数据来满足 URL加载请求。

https://developer.apple.com/library/prerelease/mac/documentation/Cocoa/Reference/Foundation/Classes/NSURLRequest_Class/index.html#//apple_ref/c/tdef/NSURLRequestCachePolicy