2015-05-04 160 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