SSL Error in Connection to Server through iPhone
Asked Answered
E

8

36

I am trying to establish a HTTPS connection to a server using my app. But the connection fails due to following error

Error Domain=NSURLErrorDomain Code=-1200 "An SSL error has occurred and a secure connection to the server cannot be made." UserInfo=0x612eb30 {NSErrorFailingURLStringKey=https:myURL.com/signup, NSLocalizedRecoverySuggestion=Would you like to connect to the server anyway?, NSErrorFailingURLKey=https:myURL.com/signup, NSLocalizedDescription=An SSL error has occurred and a secure connection to the server cannot be made., NSUnderlyingError=0x612eb70 "An SSL error has occurred and a secure connection to the server cannot be made."}

The code to connect to server is

-(IBAction) handleEvents:(id)sender
 {
    if ((UIButton*)sender == submit) {

    [UIApplication sharedApplication].networkActivityIndicatorVisible = YES;


    NSLog(@"Begin");
    NSData *urlData;
    NSURLResponse *response;
    NSError *error;

    NSString *url =[[NSString alloc]initWithFormat:@"%@signup",baseURL];
    NSURL *theURL =[NSURL URLWithString:url];
    NSMutableURLRequest *theRequest =[NSMutableURLRequest requestWithURL:theURL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:0.0f];
    [theRequest setHTTPMethod:@"POST"];
    NSString *theBodyString = [NSString stringWithFormat:@"emailId=%@&mobileNumber=%@&appId=%@&password=%@&firstName=%@&lastName=%@"
                               ,@"[email protected]",@"919879876780",@"bf1c7a6b3d266a7fe350fcfc4dda275211c13c23" ,@"qwerty" , @"Dev" , @"Sri"];
    NSData *theBodyData = [theBodyString dataUsingEncoding:NSUTF8StringEncoding];

    [theRequest setHTTPBody:theBodyData];
    urlData = [NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];
    }
}

my delegate methods are

- (void)handleError:(NSError *)error
{
NSLog(@"----->%@",error);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
  [UIApplication sharedApplication].networkActivityIndicatorVisible = NO;   

 }

- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
    return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust];
   }

- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge (NSURLAuthenticationChallenge *)challenge {  
    NSLog(@"check auth");
    [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge];
   }

I am stuck over here and could not find any way out.

Any form of help would be greatly appreciated.

thanks in advance!!

Ellsworth answered 18/2, 2011 at 11:7 Comment(0)
E
15

Since it has been left unanswered for long time and my research and current development indicates that the code is perfectly fine for the connection, its the certificate at the server that was not signed by an authorized CA. so anyone having such kind of problem check that the certificate is valid at server end or not.

Hope this would help!!

Ellsworth answered 29/5, 2011 at 8:27 Comment(1)
Not CA's problem, just because SSL's version is too low.Settlings
M
44

iOS 9 forces connections that are using HTTPS to be TLS 1.2 to avoid recent vulnerabilities. In iOS 8 even unencrypted HTTP connections were supported, so that older versions of TLS didn't make any problems either. As a workaround, you can add this code snippet to your Info.plist:

  <key>NSAppTransportSecurity</key>  
  <dict>  
  <key>NSAllowsArbitraryLoads</key>  
  <true/>  
  </dict>  

Thereby you're disabling the App Transport Security. Hope that's helpful.

Midvictorian answered 30/6, 2015 at 12:17 Comment(2)
If you're nervous about turning off App Transport Security for all domains, you can do it for specific domains: studio76.pro/…Grosvenor
For development purpose its ok, but when its go to app store submission this not acceptable.Kendall
E
15

Since it has been left unanswered for long time and my research and current development indicates that the code is perfectly fine for the connection, its the certificate at the server that was not signed by an authorized CA. so anyone having such kind of problem check that the certificate is valid at server end or not.

Hope this would help!!

Ellsworth answered 29/5, 2011 at 8:27 Comment(1)
Not CA's problem, just because SSL's version is too low.Settlings
L
9

Maybe your device has a wrong date & time :)

Leonteen answered 20/1, 2016 at 12:12 Comment(3)
fantastic. I checked all the possible solution and forgot to check the device date. Thanks for putting up this. Sometime we miss very basic things and try to debug in complicated way.Sokil
Could you elaborate on why a wrong date would cause this issue?Basiliabasilian
If the date of your device is before or after the expiry, then it is expired.Palocz
S
4

Take a look at this page: https://github.com/vinhnx/iOS-issues/issues/1

In a nutshell: The reason is, that starting from iOS9 and OSX 10.11 all apps built on XCode7 will require TLS 1.2 for SSL connection, and fails for earlier protocols.
There are several methods to overcome this issue.
"NSAppTransportSecurity -> NSAllowsArbitraryLoads" approach is not good, because it will disable TLS 1.2 for all connections from your app, and this can lead to rejection of your app by Apple.
"Per-Domain Exceptions" approach is much more better.

Slocum answered 10/9, 2015 at 10:33 Comment(1)
Great answer. I had the issue for Weibo integration and once I entered TLSv1.0 it worked. Thanks.Dysgenics
G
0

I was using iOS 9.3.1 and was using https when I hit this problem. It worked fine through the simulator, but failed on my iPad. The reason is because my iPad had WiFi enabled and had connected to my company's guest network, but I hadn't received the pop-up website where I accept to join the network. After accepting everything worked fine again.

Guenevere answered 20/4, 2016 at 15:57 Comment(0)
L
0

Please, make sure the version of TLS is 1.2,not TLS 1.0.

Apps built on XCode8 will require TLS 1.2 for SSL connection

Lenhart answered 23/11, 2016 at 6:10 Comment(0)
N
-2

Even I was facing the same issue.

This issue can be occur if the SSL certificate over server is a private certificate. In such scenario you can solve it using this.

Neely answered 4/5, 2016 at 6:8 Comment(0)
S
-5

Try the web address with safari from phone.

Soper answered 12/4, 2012 at 9:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.