简体   繁体   中英

AFNetworking using HTTPS instead of HTTP

Im working on an iOS app and am trying to use AFNetworking. Im trying to go to an http site and my request fails stating that it failed as an https request

NSString *urlString = [NSString stringWithFormat:@"http://api.openweathermap.org/data/2.5/weather?lat=%f&lon=%f&APPID=%@",lat, lon, [plistData objectForKey:@"weather"]];
NSURL *url = [[NSURL alloc] initWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
operation.responseSerializer = [AFJSONResponseSerializer serializer];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {

and the error response

Printing description of error:
Error Domain=NSURLErrorDomain Code=-1004
"Could not connect to the server."
UserInfo=0x7fa104a253d0 {
NSUnderlyingError=0x7fa104820950 "Could not connect to the server.", 
NSErrorFailingURLStringKey=https://api.openweathermap.org/data/2.5/weather?lat=[removed]&lon=[removed]&APPID=[removed], 
NSErrorFailingURLKey=https://api.openweathermap.org/data/2.5/weather?lat=[removed]&lon=[removed]&APPID=[removed], _kCFStreamErrorDomainKey=1, _kCFStreamErrorCodeKey=61,
NSLocalizedDescription=Could not connect to the server.}

on testing the site its the https part thats failing the site. As I'm not putting it in as https and explicitly putting in http I don't know whats wrong.

I'm using AFNetworking 2.5.1

To be clear, I am not trying to connect via https! I don't know why its assumed I am. Please read the whole question before posting an answer or suggesting duplication.

As Apple has changed the requirements for App building, I needed to whitelist the URL

    <key>NSAppTransportSecurity</key>
<dict>
    <key>NSExceptionDomains</key>
    <dict>
        <key>api.openweathermap.org</key>
        <dict>
            <key>NSIncludesSubdomains</key>
            <true/>
            <key>NSExceptionAllowsInsecureHTTPLoads</key>
            <true/>
        </dict>
    </dict>
</dict>

The above represents the addition to the info.plist necessary to whitelist a link This worked to allow me to use an HTTP link. You can also allow arbitrary loads, but this is discouraged by Apple. If you want to know more view the developer session from Apple for "Networking with NSURLSession"

Use this :

operation.securityPolicy.allowInvalidCertificates = YES;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM