2015-04-10 6 views
17

ile AFNetworking ben 3 yöntemleri, UserLogin, Giriş ve LogoutButtonPressed vardır:NSURLCredential

UserLogin: Ben NSURLCredential kullanarak Windows Doğrulanmış URL'ye bağlanmak için AFNetworking kullanıyorum: Bu yöntem tarafından çağrılan

-(void)UserLogin:(NSString *)user andPassWordExists:(NSString *)password completionHandler:(void (^)(NSArray *resultsObject, NSError *error))completionHandler 
{ 

    NSURL *url = [NSURL URLWithString:kIP]; 
    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 

    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] 
             initWithRequest:request]; 

    NSURLCredential *credential = [NSURLCredential 
            credentialWithUser:user 
            password:password 
            persistence:NSURLCredentialPersistenceForSession]; 

    [operation setCredential:credential]; 


    [[NSOperationQueue mainQueue] addOperation:operation]; 

    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 

     if (completionHandler) { 
      completionHandler(responseObject, nil); 
     } 

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 

     if (completionHandler) { 
      completionHandler(nil, error); 
     } 

    }]; 

    [operation start]; 

} 

Oturum yöntemi:

- (void)Login 
{ 
    NSString *rawString = [self.idTextField text]; 
    NSCharacterSet *whitespace = [NSCharacterSet whitespaceAndNewlineCharacterSet]; 
    [self.idTextField setText:[rawString stringByTrimmingCharactersInSet:whitespace]]; 


    [userName UserLogin:self.idTextField.text andPassWordExists:self.passwordTextField.text completionHandler:^(id responseObject, NSError *error) { 
     if (responseObject) { 

       [self.idTextField removeFromSuperview]; 
       [self.passwordTextField removeFromSuperview]; 
       [self.loginButton removeFromSuperview]; 
       self.idTextField = nil; 
       self.passwordTextField = nil; 
       //self.loginButton = nil; 


       [self CreateMenu]; 



       [indicatorView stopAnimating]; 
       [indicatorView removeFromSuperview]; 
       indicatorView = nil; 
       [loadingView removeFromSuperview]; 
       loadingView = nil; 
     }else{ 


      [self CustomAlert:@"Sorry Login Failed, User and/or Passsword Incorrect"]; 

      [indicatorView stopAnimating]; 
      [indicatorView removeFromSuperview]; 
      indicatorView = nil; 
      [loadingView removeFromSuperview]; 
      loadingView = nil; 

     } 
    }]; 

} 

Ve LogoutButtonPressed ile benim oturumu temizlemek için çalışıyorum:

- (void)LogoutButtonPressed 
{ 

    //@TODO: Fix Logout 

    NSDictionary *credentialsDict = [[NSURLCredentialStorage sharedCredentialStorage] allCredentials]; 

    if ([credentialsDict count] > 0) { 
     NSEnumerator *protectionSpaceEnumerator = [credentialsDict keyEnumerator]; 
     id urlProtectionSpace; 

     while (urlProtectionSpace = [protectionSpaceEnumerator nextObject]) { 
      NSEnumerator *userNameEnumerator = [[credentialsDict objectForKey:urlProtectionSpace] keyEnumerator]; 
      id userNameCred; 

      while (userNameCred = [userNameEnumerator nextObject]) { 
       NSURLCredential *cred = [[credentialsDict objectForKey:urlProtectionSpace] objectForKey:userNameCred]; 
       NSLog(@"cred to be removed: %@", cred); 
       [[NSURLCredentialStorage sharedCredentialStorage] removeCredential:cred forProtectionSpace:urlProtectionSpace]; 
      } 
     } 
    } 
} 

Bu örnekten bu kodu var: http://www.springenwerk.com/2008/11/i-am-currently-building-iphone.html

Şimdi yaşıyorum benim sorun olduğunu ben çıkış, hala giriş yapabilirsiniz YOK kimlik bilgilerini kullanarak giriş yöntemi tetiklemek sonra goto çıkış düğmesi tetikleyebilir ve ne zaman sonra 2 - 3 dakika bekleyin ve NO CREDENTIALS ile giriş yapamıyorum. Neden bu şekilde davranıyor, neredeyse krediler hala kayıtlı gibi. Lütfen yardım et.

benim LogoutButtonPressed iç önbellek, çerezler ve creds temizlemek için çalıştık

GÜNCELLEME:

NSURLCache *sharedCache = [NSURLCache sharedURLCache]; 
[sharedCache removeAllCachedResponses]; 

NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; 
NSArray *cookies = [cookieStorage cookies]; 
id cookie; 
for (cookie in cookies) { 
    [cookieStorage deleteCookie:cookie]; 
} 

NSDictionary *credentialsDict = [[NSURLCredentialStorage sharedCredentialStorage] allCredentials]; 
if ([credentialsDict count] > 0) { 
    NSEnumerator *protectionSpaceEnumerator = [credentialsDict keyEnumerator]; 
    id urlProtectionSpace; 
    while (urlProtectionSpace = [protectionSpaceEnumerator nextObject]) { 
     NSEnumerator *userNameEnumerator = [[credentialsDict objectForKey:urlProtectionSpace] keyEnumerator]; 
     id userNameCreds; 
     while (userNameCreds = [userNameEnumerator nextObject]) { 
      NSURLCredential *cred = [[credentialsDict objectForKey:urlProtectionSpace] objectForKey:userNameCreds]; 
      [[NSURLCredentialStorage sharedCredentialStorage] removeCredential:cred forProtectionSpace:urlProtectionSpace]; 
     } 
    } 
} 

ve hala işe yaramadı.

Ben de AuthorizationHeader ve cancelingAllOperations ve hala hiçbir şey takas çalıştı, ben hala yanlış ile giriş yapabilirsiniz duyuyorum ya bir çıkıştan sonra hiçbir creds:

NSURLCache *sharedCache = [NSURLCache sharedURLCache]; 
    [sharedCache removeAllCachedResponses]; 

    NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage sharedHTTPCookieStorage]; 
    NSArray *cookies = [cookieStorage cookies]; 
    id cookie; 
    for (cookie in cookies) { 
     [cookieStorage deleteCookie:cookie]; 
    } 

    NSDictionary *credentialsDict = [[NSURLCredentialStorage sharedCredentialStorage] allCredentials]; 
    if ([credentialsDict count] > 0) { 
     NSEnumerator *protectionSpaceEnumerator = [credentialsDict keyEnumerator]; 
     id urlProtectionSpace; 
     while (urlProtectionSpace = [protectionSpaceEnumerator nextObject]) { 
      NSEnumerator *userNameEnumerator = [[credentialsDict objectForKey:urlProtectionSpace] keyEnumerator]; 
      id userNameCreds; 
      while (userNameCreds = [userNameEnumerator nextObject]) { 
       NSURLCredential *cred = [[credentialsDict objectForKey:urlProtectionSpace] objectForKey:userNameCreds]; 
       [[NSURLCredentialStorage sharedCredentialStorage] removeCredential:cred forProtectionSpace:urlProtectionSpace]; 
      } 
     } 
    } 

    NSURL *url = [NSURL URLWithString:kIP]; 

    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 
    AFHTTPRequestSerializer <AFURLRequestSerialization> * requestSerializer = manager.requestSerializer; 
    [requestSerializer clearAuthorizationHeader]; 

    AFHTTPRequestOperationManager *httpClient = [[AFHTTPRequestOperationManager alloc] initWithBaseURL:url]; 
    [[httpClient operationQueue] cancelAllOperations]; 
+0

Bir kesme noktası eklediniz ve kodunuzda 'LogoutButtonPressed' içinde adım attınız mı? Ne gözlemliyorsun? Beklediğinizden farklı olan nedir? –

+0

Evet. Kredilerin kaldırıldığını görebiliyorum. ama tekrar giriş yaptığımda (kredi olmadan) giriş yapabilirim. Bir hata bekliyorum. – user979331

+0

İlginç. Belki de yanıt önbellekleniyor? İkinci denemenizde ne [[NSURLCache sharedURLCache] cachedResponseForRequest: request] 'ifadesinin ne olduğunu görebiliyor musunuz? –

cevap

5
Bu sorun sonuna bir rastgele sayı ekleyerek kolayca çözülebilecek

URL:

-(void)UserLogin:(NSString *)user andPassWordExists:(NSString *)password completionHandler:(void (^)(NSArray *resultsObject, NSError *error))completionHandler 
{ 
    NSInteger randomNumber = arc4random() % 999; 

    NSString *requestURL = [NSString stringWithFormat:@"%@?cache=%ld",kIP,(long)randomNumber]; 

    NSURL *url = [NSURL URLWithString:requestURL]; 

    NSURLRequest *request = [NSURLRequest requestWithURL:url]; 
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] 
             initWithRequest:request]; 

    NSURLCredential *credential = [NSURLCredential 
            credentialWithUser:user 
            password:password 
            persistence:NSURLCredentialPersistenceForSession]; 


    [operation setCredential:credential]; 
    operation.responseSerializer = [AFJSONResponseSerializer serializer]; 
    [[NSOperationQueue mainQueue] addOperation:operation]; 


    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) { 

     if (completionHandler) { 
      completionHandler(responseObject, nil); 
     } 

    } failure:^(AFHTTPRequestOperation *operation, NSError *error) { 

     if (completionHandler) { 
      completionHandler(nil, error); 
     } 

    }]; 

    [operation start]; 

} 

Ve aradığınız tüm URL'lerin sonunda rastgele bir numara olduğundan emin olun.

+0

AFNetworking 3.0'da "NSURLCredential" nasıl eklenir, lütfen bana yardım edin? – Sudheesh

+0

Çalışıyor, Teşekkürler .. –