Iphone: secure restfull server "The certificate for this server is not valid

I am trying to use a safe sedative service that gives an error

Error = Domain Error = NSURLErrorDomain Code = -1202 "The certificate for this server is not valid. Perhaps you are connecting to a server that is pretending to be" xxx.xxx.xxx.xxx ", which may contain sensitive information at risk.

works on xCode 4.2 where there is no error or any step.

using the following code

RegisterUser.f

@interface RegisterUser : UIViewController<UITextFieldDelegate, UIScrollViewDelegate, NSURLConnectionDelegate> 

RegisterUser.m

 - (IBAction)SubmitBtnAction:(id)sender { NSURL *url = [NSURL URLWithString:@"https://xx.xx.xx.xxx:8223/jaxrs/tunedoorgateway/getCountries"]; NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url]; [NSURLConnection sendAsynchronousRequest:urlRequest queue:[[NSOperationQueue alloc] init] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { if ([data length] >0 && error == nil) { NSLog(@"Data = %@", data); // DO YOUR WORK HERE } else if ([data length] == 0 && error == nil) { NSLog(@"Nothing was downloaded."); } else if (error != nil){ NSLog(@"Error = %@", error); } }]; } - (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace { NSLog(@"This is canAuthenticateAgainstProtectionSpace"); return [protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]; } - (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { // if ([challenge.protectionSpace.authenticationMethod isEqualToString:NSURLAuthenticationMethodServerTrust]) // if ([trustedHosts containsObject:challenge.protectionSpace.host]) [challenge.sender useCredential:[NSURLCredential credentialForTrust:challenge.protectionSpace.serverTrust] forAuthenticationChallenge:challenge]; [challenge.sender continueWithoutCredentialForAuthenticationChallenge:challenge]; NSLog(@"This is didReceiveAuthenticationChallenge"); // [[challenge sender] cancelAuthenticationChallenge:challenge]; } 
+6
source share
3 answers

I feel this may be due to DNS. Something like your server is not registered or not.

try using this for development purposes -

EDIT:

Create the file "NSURLRequest + NSURLRequestSSLY.h" and add these lines to it

 #import <Foundation/Foundation.h> @interface NSURLRequest (NSURLRequestSSLY) +(BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host; @end 

Create the file NSURLRequest + NSURLRequestSSLY.m and add these lines to it

 #import "NSURLRequest+NSURLRequestSSLY.h" @implementation NSURLRequest (NSURLRequestSSLY) +(BOOL)allowsAnyHTTPSCertificateForHost:(NSString*)host { return YES; } @end 

And do not forget to delete it, as your application may be rejected.

+13
source

There is no error in the code. You are using an HTTPS server that does not provide a known certificate. If you set up the server yourself, you need to go and buy a folded certificate from one of the major certification authorities trusted by iOS and most other operating systems.

For development purposes, you can test your REST service by ignoring a certificate that is not trusted. Follow this guide for this: http://www.cocoanetics.com/2009/11/ignoring-certificate-errors-on-nsurlrequest/

But for use in production, I recommend that you do not use this method, since it will lead to a security leak in your application. If you ignore security, you can also just use HTTP instead of HTTPS.

+8
source

I tried several ways and found out that this is related to the apple developer center certificate. Update your training and try again. I tried sending data from iPad, iPhone 5 and 5S, but iPhone 4. When I update my settings and installation on my iPhone 4, now it works with a problem.

0
source

Source: https://habr.com/ru/post/924995/


All Articles