2016-05-11 11 views
0

ねえ、私はこのようなIOSのURLからデータを取得する方法

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

https://www.raywenderlich.com/110458/nsurlsession-tutorial-getting-started –

+0

これを取得できますか。たとえば –

+0

これはあなたが探しているものです:https://github.com/skdevil/WeatherApp このプロジェクト用にPODをインストールする知識が必要です。 –

答えて

0
//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]; 
+0

与えられたURLを取得してください... –

+0

編集中!ちょうど辞書を使用する! –

+0

'sendSynchronousRequest'はiOS 9では廃止されました。実際には、' NSURLSession'で置き換えられたので、 'NSURLConnection'を使用しないでください。 – pajevic

1

使用NSUrlSession ...このURLからデータをフェッチして助けてくださいIOSの新たなんだ:

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]; 
関連する問題