2015-04-09 3 views
11

Ich verwende NSURLCredentials in dieser Methode:Objective-C klar NSURLCredential - NSURLCredentialPersistenceForSession

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

Und jetzt ich suche das NSURLCredentials in einer Logout-Methode zu löschen, wie so:

-(void)logout 
{ 
    //Clear NSURLCredentialPersistenceForSession 
} 

Wie würde Ich gehe das machen? Sollte ich die NSURLCredentials zurücksetzen oder ist es da, um sie zu löschen?

UPDATE

ich diese Lösung gefunden:

NSURLCredentialStorage *credentialStorage = [NSURLCredentialStorage sharedCredentialStorage]; 
    NSDictionary *credentialsDicationary = [credentialStorage allCredentials]; 


    for (NSURLProtectionSpace *space in [credentialsDicationary allKeys]) { 

     NSDictionary *spaceDictionary = [credentialsDicationary objectForKey:space]; 

     for (id userName in [spaceDictionary allKeys]) { 

      NSURLCredential *credential = [spaceDictionary objectForKey:userName]; 

      [credentialStorage removeCredential:credential forProtectionSpace:space]; 

     } 

    } 

Von hier aus: Using NSURLCredentialPersistenceForSession while request and then clearing the credentials on logout still persist the cerdentials

Ist dies der beste Weg? Ich habe nur 1 Berechtigungsnachweis gespeichert. Auch dieser Code gibt mir eine Warnung in dieser Zeile:

und hier ist die Warnung ... Wie entferne ich diese Warnung?

/Users/jsuske/Documents/SSiPad(Device Only)ios7/SchedulingiPadApplication/ViewControllers/LHLoginController.m:496:73: Local declaration of 'userName' hides instance variable 

UPDATE

Okay, ich habe 3 Methoden: Userlogin, Login und LogoutButtonPressed

Userlogin: Ich AFNetworking bin mit zu einer Windows-authentifizierten URL verbinden NSURLCredential, wie oben gezeigt:

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

} 

Diese Methode wird von der Login-Methode aufgerufen wird:

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

     } 
    }]; 

} 

Und ich versuche meine Sitzung mit dem LogoutButtonPressed zu löschen:

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

Ich habe diesen Code aus diesem Beispiel: http://www.springenwerk.com/2008/11/i-am-currently-building-iphone.html

Jetzt ist mein Problem, das ich habe, ist, dass, wenn ich auslösen die Logout-Button und dann die Login-Methode auslösen OHNE KREDITIALE Ich kann mich noch einloggen, wenn ich mich abmelde, dann 2 - 3 Minuten warten und mich mit KEINEN NOTRUF anmelden, kann ich mich nicht anmelden. Warum verhält es sich so, es ist fast so, als wären die Kredits noch immer gerettet. Bitte helfen Sie.

UPDATE

ich versucht habe, in meinem LogoutButtonPressed den Cache, Cookies und creds zu löschen:

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

und es immer noch nicht funktioniert hat.

Antwort

5

Dieses Problem kann durch das Hinzufügen einer Zufallszahl an das Ende der URL leicht behoben werden:

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

} 

Und stellen Sie sicher, dass Sie eine Zufallszahl am Ende aller URLs haben Sie anrufen.

+0

Was wird eine Zufallszahl zu tun haben. Nur neugierig zu wissen. –

1

LogoutButtonPressed entfernt die Anmeldeinformationen von NSURLCredentialStorage während der Iteration über dieselben Anmeldeinformationen - NSEnumerator unterstützt dies nicht. Wird im Protokoll die Meldung "Zu entfernendes Guthaben" angezeigt?

Sie könnten stattdessen die Methode _resetCredentials von https://developer.apple.com/library/ios/samplecode/AdvancedURLConnections/Listings/CredentialsController_m.html versuchen.

Unabhängig davon, ob dies einen Unterschied macht, wäre es interessant, die Ausgabe von _dumpCredentials vor und nach dem Aufruf von resetCredentials zu untersuchen - oder besser noch den Dump in Ihrer Anmeldemethode und sehen, ob es eine Änderung gibt unmittelbar nach dem Reset und dann 2-3 Minuten später.

Wird self.passwordTextField.text gelöscht? In dem Fall, in dem keine Anmeldung erfolgt, gibt NSURLCredential credentialWithUser: Ihnen NULL?

+0

Ich sehe die 'NSLog (@" cred entfernt werden:% @ ", cred);' Nachricht, ich habe versucht, die Glaubwürdigkeit in meiner Login-Methode und beide Login, UserLogin und logoutbuttonpressed – user979331

+1

Cool. Willst du uns sagen, was die Müllhalde verrät? –

+0

gleiche Ergebnisse ... wenn ich mich abmelde und versuche, mich sofort mit falschen Krediten anzumelden, kann ich mich anmelden, wenn ich 2-3 Minuten warte, kann ich mich nicht einloggen. – user979331