2015-05-04 6 views
1

Я использую веб-службу с аутентификацией простого заголовкаУдалить файлы cookie из http-заголовка?

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { 
    NSString *userName = [_usernameTextField.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];; 
    NSString *passWord = [_passwordTextfield.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]]; 
    if ([challenge previousFailureCount] == 0) { 
     //Creating new credintial 
     NSURLCredential *newCredential = [NSURLCredential credentialWithUser:userName 
                    password:passWord 
                   persistence:NSURLCredentialPersistenceForSession]; 
     [[challenge sender] useCredential:newCredential forAuthenticationChallenge:challenge]; 
    } 
    else { 
     CommonCode*objCommon=[[CommonCode alloc]init]; 
     [_activityIndicator stopAnimating]; 
     [objCommon showAlert:@"Invalid Password, or no user found with this Email Address"]; 
    } 
} 

На выходе из системы я расчистка cokies с

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

Но после выхода из системы, если я введу неверный пароль Я войти в системе как theprevious пользователь. Как удалить файлы cookie из заголовка HTTP-запроса?

+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

Смежные вопросы