Workaround:
Add key to Info.plist
<key>NSAppTransportSecurity</key>
<dict>
<key>nsallowsarbitraryloads</key >
<true/>
</dict>
Let me introduce you to the HTTP and HTTPS protocols in iOS.
Recently done a project, began to use the HTTP protocol to achieve client and server-side interaction, and later need to change to HTTPS protocol. Some problems were identified during the modification process, and the solution was as follows:
HTTP:
NSString *urlstring =[nsstring stringwithformat:@ "https://127.0.0.1/default.aspx?user=%@", @ "D"];
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];
HTTPS
Event triggers
{nsstring *urlstring =[nsstring stringwithformat:@ "https://127.0.0.1/default.aspx?user=%@", @ "111"]; Nsmutableurlrequest *request = [[Nsmutableurlrequest alloc] Initwithurl:[nsurl urlwithstring:urlstring] CachePolicy:
Nsurlrequestreloadignoringlocalcachedata Timeoutinterval:5];
Set the request mode to get [request sethttpmethod:@ ' get '];
Add user session ID [request addvalue:@ "text/html" forhttpheaderfield:@ "Content-type"];
Connection Send request finished = false;
Nsurlconnection *conn = [[Nsurlconnection alloc] initwithrequest:request delegate:self]; Blocking threads, waiting for End while (!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 to server-side single HTTPS authentication, IOS client ignores certificate validation. -(BOOL) connection: (Nsurlconnection *) connection canauthenticateagainstprotectionspace: (Nsurlprotectionspace *) protectionspace {return [Protectionspace.authenticationmethod isequaltostring:
Nsurlauthenticationmethodservertrust]; }-(void) connection: (Nsurlconnection *) connection Didreceiveauthenticationchallenge: (nsurlauthenticationchallenge *) Challenge {NSLog (@ "Didreceiveauthenticationchallenge%@%zd", [[Challenge Protectionspace] authenticationmethod], (
ssize_t) [Challenge Previousfailurecount]); if ([Challenge.protectionSpace.authenticationMethod isequaltostring:nsurlauthenticationmethodservertrust]) {[ Challenge Sender] Usecredential:[nsurlcredential CredentialForTrust:challenge.protectionSpace.serverTrust]
Forauthenticationchallenge:challenge];
[[Challenge Sender] continuewithoutcredentialforauthenticationchallenge:challenge]; } NSLog (@ "Get" whole RESPONSE ");
[Receiveddata setlength:0]; }//Processing data-(void) connection: (Nsurlconnection *) connection didreceivedata: (NSData *)