如果您收到驗證質詢,您將看到第二個請求。
你可以,如果你願意,簡單地拒絕授權挑戰,停止第二個請求:如果你想繼續前進,驗證
[manager setTaskDidReceiveAuthenticationChallengeBlock:^NSURLSessionAuthChallengeDisposition(NSURLSession * _Nonnull session, NSURLSessionTask * _Nonnull task, NSURLAuthenticationChallenge * _Nonnull challenge, NSURLCredential *__autoreleasing _Nullable * _Nullable credential) {
return NSURLSessionAuthChallengeCancelAuthenticationChallenge;
}];
或者,你可以這樣做:
[manager setTaskDidReceiveAuthenticationChallengeBlock:^NSURLSessionAuthChallengeDisposition(NSURLSession * _Nonnull session, NSURLSessionTask * _Nonnull task, NSURLAuthenticationChallenge * _Nonnull challenge, NSURLCredential *__autoreleasing _Nullable * _Nullable credential) {
if (challenge.previousFailureCount == 0) {
*credential = [NSURLCredential credentialWithUser:self.user password:self.password persistence:NSURLCredentialPersistenceForSession];
return NSURLSessionAuthChallengeUseCredential;
}
return NSURLSessionAuthChallengeCancelAuthenticationChallenge;
}];
這一切都取決於您如何驗證Web服務上的用戶。
來源
2016-10-21 23:19:33
Rob
搶你是救世主......非常感謝......我正在返回NSURLSessionAuthChallengeUseCredential,它正在悄悄地發出另一個請求......但在更改爲行後返回NSURLSessionAuthChallengeCancelAuthenticationChallenge它工作得很好.... – lifemoveson