从http标头中删除cookie?

时间:2015-05-04 08:44:17

标签: ios objective-c xcode cookies http-headers

我正在使用简单标头身份验证的网络服务

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

但是在注销后如果我输入了错误的密码,我将以以前的用户身份登录。如何从HTTP请求的标头中删除cookie?

1 个答案:

答案 0 :(得分:1)

NSURLRequest具有cachePolicy属性,该属性指定请求的缓存行为。

在发出请求时设置以下缓存策略 NSURLRequestReloadIgnoringLocalCacheData ,例如bellow将从网址加载数据,而不是从缓存加载数据。

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

NSURLRequestReloadIgnoringLocalCacheData

     

指定应从中加载URL加载的数据   起源。不应使用现有的缓存数据来满足a   网址加载请求。

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