Mohammad Hussain
Mohammad Hussain

Reputation: 1

How to fetch data from URL in ios

Hey i'm new in ios please help in fetching data from this URL...

NSString *urlString = @" http://api.openweathermap.org/data/2.5/forecast/daily?q=Indore&mode=json&units=metric&cnt=10&APPID=fc632e2569dbdbb07ca79e239faa0281";

Upvotes: 0

Views: 287

Answers (2)

Harish Pathak
Harish Pathak

Reputation: 1607

//Init the NSURLSession with a configuration
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject delegate: nil delegateQueue: [NSOperationQueue mainQueue]];

//Create an URLRequest
NSURL *url = [NSURL URLWithString:@"yourURL"];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];

//Create POST Params and add it to HTTPBody
NSString *params = @"api_key=APIKEY&[email protected]&password=password";
[urlRequest setHTTPMethod:@"POST"];
[urlRequest setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]];

//Create task
NSURLSessionDataTask *dataTask = [defaultSession dataTaskWithRequest:urlRequest completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    //Handle your response here
}];

[dataTask resume];

Upvotes: 0

pajevic
pajevic

Reputation: 4657

Use NSUrlSession like this:

NSString *urlString = @"http://api.openweathermap.org/data/2.5/forecast/daily?q=Indore&mode=json&units=metric&cnt=10&APPID=fc632e2569dbdbb07ca79e239faa0281";
NSURLSession *session = [NSURLSession sharedSession];
[[session dataTaskWithURL:[NSURL URLWithString:urlString] completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
    // handle the response
}] resume];

Upvotes: 1

Related Questions