I need to parse a xml file from a password protected URL I tried the following
NSURLCredential *credential = [NSURLCredential credentialWithUser:@\"admin\"
With Xcode 6, my app would not download a large number of files on IOS 8 platforms eventhough it worked fine on earlier IOS. My code was essentially as you have it below, but the error kept appearing when an authentication challenge came.
I had to change the code to
if ([challenge previousFailureCount] == 0) {
NSURLCredential *newCredential;
newCredential=[NSURLCredential credentialWithUser:@"username" password:@"password" persistence:NSURLCredentialPersistenceForSession];
[[challenge sender] useCredential:newCredential
forAuthenticationChallenge:challenge];
} else {
NSURLCredential *newCredential;
newCredential=[NSURLCredential credentialWithUser:@"username" password:@"password" persistence:NSURLCredentialPersistenceForSession];
[[challenge sender] useCredential:newCredential
forAuthenticationChallenge:challenge];
}
Which of course makes the IF statement superfluous (it can be removed). I have no explanation but without responding to the challenge with username and password even on a persistent session causes an error (-1012). Now it works as on the earlier IOS
In my case the issue was caused by a firewall/proxy requiring a client certificate for all HTTPS calls. I resolved this issue by providing a NSURLProtocol subclass to handle authentication challenges and provide this certificate.
[NSURLProtocol registerClass:self];
//implement these methods
- (void)didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge;
- (void)resolveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge withCredential:(NSURLCredential *)credential;
While using a NSURLRequest
to access a server, the delegate methods of NSURLConnection
provides a way to be notified when an authentication is challenged.
This below sample will show one approach to handle URLs that asks credentials.
- (void)CallPasswordProtectedWebService
{
NSURL *url = [NSURL URLWithString:@"your url"];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
urlConnection = [[NSURLConnection alloc] initWithRequest:urlRequest delegate:self];
}
//when server asks the credentials this event will fire
- (void)connection:(NSURLConnection *)connection
didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
if ([challenge previousFailureCount] == 0) {
NSURLCredential *newCredential;
newCredential=[NSURLCredential credentialWithUser:@"username" password:@"password" persistence:NSURLCredentialPersistenceNone];
[[challenge sender] useCredential:newCredential
forAuthenticationChallenge:challenge];
} else {
[[challenge sender] cancelAuthenticationChallenge:challenge];
}
}
//when connection succeeds this event fires
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSLog(@"Successfuly connected ");
}
//when connection fails the below event fires
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
NSLog(@"connection failed");
}
Hope this helps