Simple example of NSURLSession with authentication

后端 未结 2 1141
眼角桃花
眼角桃花 2021-01-15 08:31

I have written a REST service that serves up some data. It is passcode protected.

I am trying to write a background process that will grab the data and stuff it int

2条回答
  •  时光说笑
    2021-01-15 09:02

    I think your question is vague and underspecified. That said, here's one solution, from here:

    -(void)URLSession:(NSURLSession *)session didReceiveChallenge:(NSURLAuthenticationChallenge *)challenge completionHandler:(void (^)(    NSURLSessionAuthChallengeDisposition disposition, NSURLCredential *credential))completionHandler
    {
        if (_sessionFailureCount == 0) {
            NSURLCredential *cred = [NSURLCredential credentialWithUser:self.userName password:self.password persistence:NSURLCredentialPersistenceNone];        
        completionHandler(NSURLSessionAuthChallengeUseCredential, cred);
        } else {
            completionHandler(NSURLSessionAuthChallengeCancelAuthenticationChallenge, nil);
        }
        _sessionFailureCount++;
    }
    

    I strongly recommend that you read and re-read the Apple docs on this.

提交回复
热议问题