SoFunction
Updated on 2025-03-09

iOS9 Apple changed the original http protocol to https protocol method

Solution:

Join key

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

Below I will introduce the access to the http and https protocols in ios

Recently, I started to use the HTTP protocol to realize the interaction between the client and the server, and later it needs to be changed to the HTTPS protocol. During the modification process, some problems were found, and the solutions were as follows:

HTTP:

NSString *urlString =[NSString stringWithFormat:@"https://127.0.0.1/?USER=%@",@"111"];
NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"GET"];
NSHTTPURLResponse* urlResponse = nil;
NSError *error = [[NSError alloc] init];
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error];
NSMutableString *result = [[NSMutableString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"The result string is :%@",result); 

HTTPS

Event trigger

{ 
NSString *urlString =[NSString stringWithFormat:@"https://127.0.0.1/?USER=%@",@"111"];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:urlString] cachePolicy:NSURLRequestReloadIgnoringLocalCacheData timeoutInterval:5];
//Set the request method to get[request setHTTPMethod:@"GET"];
//Add user session id[request addValue:@"text/html" forHTTPHeaderField:@"Content-Type"];
//Connection send requestfinished = false;
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
//Clog the thread, wait for the endwhile(!finished) {
[[NSRunLoop currentRunLoop] runMode:NSDefaultRunLoopMode beforeDate:[NSDate distantFuture]];
}
} 
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse*)response 
{}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{
//[_waitingDialog dismissWithClickedButtonIndex:0 animated:NO];
[connection release];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{ 
}
- (BOOL)connectionShouldUseCredentialStorage:(NSURLConnection *)connection{
return NO;
}
//The following two paragraphs are the key points, and the server-side single HTTPS verification is required, while the iOS client ignores certificate verification.- (BOOL)connection:(NSURLConnection *)connection canAuthenticateAgainstProtectionSpace:(NSURLProtectionSpace *)protectionSpace {
return [ isEqualToString:NSURLAuthenticationMethodServerTrust];
} 
- (void)connection:(NSURLConnection *)connection didReceiveAuthenticationChallenge:(NSURLAuthenticationChallenge *)challenge { 
NSLog(@"didReceiveAuthenticationChallenge %@ %zd", [[challenge protectionSpace] authenticationMethod], (ssize_t) [challenge previousFailureCount]);
if ([ isEqualToString:NSURLAuthenticationMethodServerTrust]){
[[challenge sender] useCredential:[NSURLCredential credentialForTrust:] forAuthenticationChallenge:challenge];
[[challenge sender] continueWithoutCredentialForAuthenticationChallenge: challenge];
}
} 
NSLog(@"get the whole response");
//[receivedData setLength:0];
}
// Process data- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{ 
}