0
votes

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.

2
I don't think so, I don't want to connect to https, this person wants to connect to https. for them I'm not sure if its not allowing them to enter https, or if https links aren't working. the api I'm attempting to connect to won't allow https connections, this means I must use http. - DasScooter
probably openweathermap redirects your request to https so your network library fails to connect. You can't just say "I don't want to connect via https" as it is not up to you. - Ozgur Vatansever
if I try to connect in my browser it works via http without redirection. - DasScooter
can you show us how you start operation? - Ozgur Vatansever
completionHandler(weather); } failure:^(AFHTTPRequestOperation *operation, NSError *error) { Weather *weather; completionHandler(weather); }]; [operation start]; } This is after the code above surrounding my success code - DasScooter

2 Answers

1
votes

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"

0
votes

Use this :

operation.securityPolicy.allowInvalidCertificates = YES;