Pages

Wednesday, November 16, 2011

[Code Snippet] Accesing to SSL server trust web services

This code snippet completes the two previous post that says how to connect to a REST service and consume it synchronous and asynchronously.

When we have to access a HTTPS web service first thing that we should think is how to store the trusted root certificate in our iPhone or iPad. Our device has installed some of the most used certificates (you can check the list here) and for this snippet we asume that the server uses Verisign, GoDaddy or any other common SSL certificate, so we have yet installed it.

To accomplish this task you should implement the next functions of NSURLConnection delegate:


- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection
{
    return TRUE;}
-(void)connection:(NSURLConnection *)connection didCancelAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
    NSLog(@"Challenge cancelled");}
-(BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace
{
   // Server Trust authentication method
    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];}
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge
{
  
    //Here we told the OS that validate the server certificate against our credential storage.
    [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge];       
    NSLog(@"Reto superado");}

If we just want to bypass the authentication, instead of useCredential, we should use continueWithoutCredential method in didReceiveAuthenticationChallenge.

    //Bypass without credentials
    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];

I hope this code snippet could be useful for you, enjoy it!

NOTE: This can be used only with NSURLConnection, just for asynchronous request.

Working in progress...

Hello everyone.

We're sorry for the lack of updates of the last weeks but J.A.Gallego was travelling round Spain with Windows Phone Roadshow and I was also very busy with a suite of nature oriented new iPhone (and maybe iPad optimized in the future) apps.

We're also creating a new web site, exclusively for our apps and games with the help of Virginia, our graphics designer, we'll keep this blog only for code snippets and geek stuff at blogspot for those who has it in their bookmarks and create a separately web for apps and projects to give them more visibility.

At last we are in the way to port Vinos de Cigales and Car Charge to Windows Phone and Android OS, so it's possibly that in the next days two new guys will be added to Five Flames Mobile staff...

These are the causes of the lack of updates, Twitter has been updated frequently but the blog wasn't so we apologize and will try to recover our weekly updates.

Thanks to all of our followers!