2016-08-02 2 views
-1

У меня есть следующий код Как разобрать ответ?Как отправить API через NSURLSession?

NSError *error; 

NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration]; 
NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration]; 
NSURL *url = [NSURL URLWithString:@ "API-URL"]; 
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url 
                 cachePolicy:NSURLRequestUseProtocolCachePolicy 
                timeoutInterval:60.0]; 

[request addValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
[request addValue:@"application/json" forHTTPHeaderField:@"Accept"]; 

[request setHTTPMethod:@"POST"]; 

NSDictionary *mapData = [[NSDictionary alloc] initWithObjectsAndKeys: @"key", @"76658b01d08e43f65c6930933f69f1", 
         nil]; 
NSData *postData = [NSJSONSerialization dataWithJSONObject:mapData options:0 error:&error];  

NSURLSessionDataTask *postDataTask = [session dataTaskWithRequest:request completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {  
}]; 

[postDataTask resume]; 

Как получить выход из словаря после получения ответа от dataTaskResponse?

ответ

1

Пожалуйста, используйте следующий код.

NSError* error; 
NSDictionary* json = [NSJSONSerialization JSONObjectWithData:data 
                options:kNilOptions 
                 error:&error]; 
NSLog(@"json: %@", json); 
Смежные вопросы