3
votes

I use below code for get data from server with post request

NSString *serverUrl=@"http://192.168.1.105:2300/api/customer";
    NSString *post =[NSString stringWithFormat:@"action=%@",actionName];
    NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
    NSString *postLength = [NSString stringWithFormat:@"%lu", (unsigned long)[postData length]];
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:serverUrl]];
    [request setHTTPMethod:@"POST"];
    [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:postData];
    NSError *error = nil; NSURLResponse *response = nil;
    NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

but occur below error :

Error Domain=NSURLErrorDomain Code=-1022 "The resource could not be loaded because the App Transport Security policy requires the use of a secure connection." UserInfo={NSUnderlyingError=0x7f9be35f19a0

I find several post that says add below lines in info.plist and i added , but again i get error

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSExceptionDomains</key>
        <dict>
            <key>http://192.168.1.105:2300</key>
            <dict>
                <key>NSIncludesSubdomains</key>
                <true/>
                <key>NSExceptionAllowsInsecureHTTPLoads</key>
                <true/>
                <key>NSExceptionRequiresForwardSecrecy</key>
                <true/>
                <key>NSExceptionMinimumTLSVersion</key>
                <string>TLSv1.2</string>
                <key>NSThirdPartyExceptionAllowsInsecureHTTPLoads</key>
                <true/>
                <key>NSThirdPartyExceptionRequiresForwardSecrecy</key>
                <true/>
                <key>NSThirdPartyExceptionMinimumTLSVersion</key>
                <string>TLSv1.2</string>
                <key>NSRequiresCertificateTransparency</key>
                <true/>
            </dict>
        </dict>
    </dict>

why ?

2
your_domain_here, did you replace this?tuledev
replace with : 192.168.1.105:2300SajjadZare
Try with this <key>NSAppTransportSecurity</key> <dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict>tuledev
@anhtu : add it but again errorSajjadZare
Try adding only a variable boolean to NSAppTransportSecurity named NSAllowsArbitraryLoads and set it to YES. (Although this is a work-around and the approach that you are using is the correct way to go about it)NSNoob

2 Answers

1
votes

You can ignore all security restrictions like:

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

But this isn't a good solution.

Ref How do I load an HTTP URL with App Transport Security enabled in iOS 9?

0
votes

For more information about NSAppTransportSecurity see the document on this link https://developer.apple.com/library/ios/documentation/General/Reference/InfoPlistKeyReference/Articles/CocoaKeys.html enter image description here

  1. Step: Go to your project info as example (image is showing with green marked will show you after complete following steps).
  2. Step: Add new property NSAppTransportSecurity and Type will be Dictionary .
  3. Step: Add Key NSAllowsArbitraryLoads select property type Boolean and to set YES. And your code will run without any NSAppTransportSecurity exception .