AFNetworking with NSURLCredential

前端 未结 1 1301
迷失自我
迷失自我 2021-02-07 07:01

I have 3 methods, UserLogin, Login and LogoutButtonPressed:

UserLogin: I am using AFNetworking to connect to a Windows Authenticated URL using NSURLCredential:



        
相关标签:
1条回答
  • 2021-02-07 07:36

    This issue can be fixed easily by adding a random number to the end of the 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];
    
    }
    

    And make sure you have a random number at the end of all URLs you are calling.

    0 讨论(0)
提交回复
热议问题