1
votes

I am using openweathermap API in my iOS app.

Below is my URL which i am call for get whether info.

http://api.openweathermap.org/data/2.5/weather?lat=21.282778&lon=-157.829444&APPID=MYAPPID&lang=en-US

While open this url in browser i got response properly.

But when i call Web-service from my iOS app i do not get response. I get below error :

{
    cod = 401;
    message = "Invalid API key. Please see http://openweathermap.org/faq#error401 for more info.";
}

I had created API key from this URL : Create API key on Open Wheather

1
MYAPPID is just temperory for here. I am putting actual value. - Vishal Sharma
check my url in above comment i just replaced a valid appid from openweathermap site - Muhammad Adnan
I also tried same thing from URL provided by you. But it has same result. In browser it shows result But showing same error in iOS App. - Vishal Sharma
post your code how you sending request on server. - Muhammad Adnan

1 Answers

1
votes
NSURL *URL = [NSURL URLWithString:@"http://api.openweathermap.org/data/2.5/weather?q=London,uk&appid=b1b15e88fa797225412429c1c50c122a&lang=en-US"];
    NSURLRequest *request = [NSURLRequest requestWithURL:URL];

    NSURLSession *session = [NSURLSession sharedSession];
    NSURLSessionDataTask *task = [session dataTaskWithRequest:request
                                            completionHandler:^(NSData *data, NSURLResponse *response, NSError *error)
    {
        NSError* errorObj;
        NSDictionary* json = [NSJSONSerialization
                              JSONObjectWithData:data
                              options:kNilOptions
                              error:&errorObj];

        NSLog(@" response is %@",json);

    }];

    [task resume];

You app id should be valid. I have hardcoded the url for demonstration.