O manipulador de conclusão NSURLSessionDataTask dataTaskWithURL não está sendo chamado
Ultimamente, tenho aprendido o objetivo C e decidi tentar conexões.
Tudo foi ótimo com o NSURLConnection, até que descobri que estava desatualizado e tentei trabalhar com o NSURLSession.
Estou tentando um exemplo muito simples, mas não consigo fazer meu aplicativo executar o código dentro do bloco de conclusão.
Aqui está o código usado:
NSURL * url = [NSURL URLWithString:@"http://api.openweathermap.org/data/2.5/weather?q=London,uk"];
NSLog(@"2");
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSLog(@"3");
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject
delegate: nil
delegateQueue: [NSOperationQueue mainQueue]];
NSLog(@"4");
NSURLSessionDataTask * dataTask = [defaultSession dataTaskWithURL:url
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
dispatch_sync(dispatch_get_main_queue(), ^{
NSLog(@"11");
if(error == nil)
{
NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(@"Data = %@",text);
}
NSLog(@"22");
});
}];
NSLog(@"5");
[dataTask resume];
NSLog(@"6");
Eu obtenho todos os números impressos da execução principal, mas o completeHandler nunca é executado. Eu também tentei isso usando um delegado, sem sucesso.
Desde já, obrigado.
EDITAR Conforme sugerido, alterei minha função para o seguinte:
-(void) doGET{
NSURLSessionConfiguration *defaultConfigObject = [NSURLSessionConfiguration defaultSessionConfiguration];
NSURLSession *defaultSession = [NSURLSession sessionWithConfiguration: defaultConfigObject];
NSURLSessionDataTask * dataTask = [defaultSession dataTaskWithURL:[self url]
completionHandler:^(NSData *data, NSURLResponse *response, NSError *error) {
NSLog(@"11");
if(error == nil)
{
NSString * text = [[NSString alloc] initWithData: data encoding: NSUTF8StringEncoding];
NSLog(@"Data = %@",text);
}
NSLog(@"22");
}];
[dataTask resume];
}
Meu gerente de conclusão ainda não está sendo executado. Também tentei com o sharedSession em vez de passar uma configuração, mas também não tive sorte.
Obrigado pela ajuda