objective c - Can I use NSURLCredentialStorage for HTTP Basic Authentication? -


I have a cocoa class installed which I want to use to connect to a Welsh web service I have decided to use HTTP Basic Authentication on my PHP backend ...

  & lt ;? Php if (! Isset ($ _ SERVER ['PHP_AUTH_USER'])) {header ('WWW-authentication: original realm = "my area"); Header ('http / 1.0 401 unauthorized'); // Stuff which will click on users to exit 'Cancel'; } Other {// Validation code "you have entered information echo."; }? & Gt;  

At this time I am using a synchronous NSURL connection, which I believe is less support for authentication in Apple documentation states.

But is this possible at all? I can cookie authentication very easily without NSURLProtectionSpaces or NSURLCredentials or any certification classes. Also, are there any resources where I can read more about Cocoa Certification Classes?

Thank you.

UPDATE: The code you provided by MikeBadullah (second example) was exactly what I had written. I've checked something else, and discovered that the NSURLConnection error is returning ...

  error domain = NSURLErrorDomain code = -1012 UserInfo = 0x1a5170 "operation could not be completed. ( NSURLErrorDomain Error-1012.) " 

The code matches the NSURLErrorUserCancelledAuthentication. So apparently my code is not accessing NSURLCredentialStorage and is canceling authentication instead. Can this do anything with PHP's HTTP Authentication Functions? I am quite confused at this point.

A synchronous NSURL connection code NSURLCredentialStorage Work with

  • The server responds with 401 responses
  • URL
  • If the URL has provided no complete credentials (username and password), NSURLConnection looks> NSUrConnect NSURLCredentialStorage
  • If the full credentials are not set now , NSURLConnection will send -connection: didReiveiveAuthenticationChallenge: Representative Representative asking for credentials
  • If NSURLConnection is now finally full credentials, then the original authentication data request including.
  • By using the synchronous connection method, you exit only step 5, the ability to provide custom authentication, therefore, you must either pre-authentication credentials in the URL Can provide or put them in the NSURLCredentialStorage before sending the request. E.g.

      NSURLRequest * request = [NSURLRequest request is: Ref: [NSURL URLWithString: @ "http: // user: pass@example.com"]]; [Synchronous request sending NSRR connection: Request is returning response: zero error: zero];  

    or:

      NSURLCredential * credential = [NSURLCredential credentialWithUser: @ "user" password: @ "pass" firmly: NSURLCredentialPersistenceForSession]; NSURLProtectionSpace * protectionSpace = [[NSURLProtectionSpace alloc] initWithHost: @ "example.com" port: 0 protocol: @ "http" field: zero authentication method: zero]; [[NSURLCredentialStorage sharedCredentialStorage] setDefaultCredential: Credential for Security: SecuritySpace]; [Protection Site Release]; NSURLRequest * Request = [NSURLRequest request with url: [NSURL URLWithString: @ "http://example.com"]]; [Synchronous request sending NSRR connection: Request is returning response: zero error: zero];  

    Comments