connectionDidFinishLoading doesn't trigger

Discussion in 'iOS Programming' started by joak, Apr 30, 2014.

  1. joak macrumors newbie

    Mar 7, 2014

    I have one class to download information..... The information download correctly, but why the connectionDidFinishLoading method doesn't trigger?

    My "h file" code

    #import <Foundation/Foundation.h>
    #import "Reachability.h"
    //implementar protocolo
    @protocol RedDelegate <NSObject>
    -(void) terminaDescarga:(NSData*)datos
    -(void) errorEnDescarga:(NSInteger)codigo descrip:(NSString *)descError conId:(NSInteger)id;
    @interface JFRed : NSObject <NSURLConnectionDataDelegate> @property(nonatomic,strong) NSObject<RedDelegate> *delegado;
    @property (nonatomic,strong) NSMutableData *datosDescargados;
    @property (nonatomic,strong) NSURLConnection @property(nonatomic) NSInteger id;
    @property(nonatomic) int httpCodeReturn;
    @property(nonatomic) NSString *httpStringCodereturn;
    -(void) descargar:(NSString*)direccion conId:(NSInteger)id;
    -(void) jsonPostRequest:(NSString *)direccion tipoId:(NSInteger)id postData:(NSString*) postData;
    -(void) comprobarRed:(NSString*)direccion;
    -(NSDictionary*) connectToWSArticulo:(NSString *) lstrIdArticulo;
    The "m file" code, first execute "connectToWSArticulo" method, and then "descargar" method and any trigger the "connectionDidFinishLoading" method . The informartion it's download correct.

    #import "JFRed.h"
    @implementation JFRed
    -(void) descargar:(NSString*)direccion conId:(NSInteger)id
    { = id; //es el número de tarea que se esta bajando
        NSString *dir = direccion;
        NSURL *url = [NSURL URLWithString:dir];
        NSMutableURLRequest *solicitud = [NSURLRequest requestWithURL:url];
        //inicia la descarga asincrona
        self.conexion = [[NSURLConnection alloc] initWithRequest:solicitud delegate:self];
            self.datosDescargados = [NSMutableData data];//inicializa
    -(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
        NSHTTPURLResponse* httpReturn = (NSHTTPURLResponse*)response;
        self.httpCodeReturn = [httpReturn statusCode];
        self.httpStringCodereturn = [NSHTTPURLResponse localizedStringForStatusCode:self.httpCodeReturn];
        self.datosDescargados.length = 0;
    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
        [self.datosDescargados appendData:data];
    -(void) connectionDidFinishLoading:(NSURLConnection *)connection
            [self.delegado terminaDescarga:self.datosDescargados httpCodeReturn:self.httpCodeReturn httpStringCodereturn:self.httpStringCodereturn];
    -(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
        [self.delegado errorEnDescarga:error.code descrip:[error.userInfo objectForKey:NSLocalizedDescriptionKey ]];   
    #pragma mark - connectToWSArticulo
    -(NSDictionary*) connectToWSArticulo:(NSString *) lstrIdArticulo //antes regrasab un nsmutabala array
        NSString *lsStr = @"http://";
        //NSString *lsStr2 = @"/WS_Articulo/ws/ArticulosValorWS/consultaExiInvb?articulo="; Pasada
        NSString *lsStr2 = @"/WS_Cliente/ws/rest/datosArticuloWS/getDatosArticulo?articulo=";
        //Obtengo la dirección IP a realizar la conexión
        //NSString *lstrIp = [self getSettings:@"direccionIP"]; Si es que la IP se encontrara en Settings de la aplicación
        //NSString *lstrIp = @""; IP de la intranet(ya no se deberá de usuar)
        NSString *lstrIp = @"";
        //Concatena variables
        NSString *lsCadena = [NSString stringWithFormat: @"%@%@%@%@",lsStr,lstrIp,lsStr2,lstrIdArticulo];
        NSData *data = [[NSData alloc] initWithContentsOfURL:[NSURL URLWithString:lsCadena]];
        //NSArray *dictionaryArray = [self convertJsonDataDiccionary:data];// Antes regresaba un array
        NSDictionary *diccionario = [self convertJsonDataDiccionary:data];
        return diccionario;
    -(NSDictionary *)convertJsonDataDiccionary:(NSData*)data //antes regresaba un nsarray
        NSError *error = nil;
        NSDictionary *dictionary = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];
        if (error) {
            UIAlertView *alerta = [[UIAlertView alloc] initWithTitle:@"AVISO" message:@"No se puede conectar al servicio, intente de nuevo más tarde, gracias"  delegate: nil cancelButtonTitle:@"Aceptar" otherButtonTitles:nil];
            [alerta show];    }
        //NSArray *array = [dictionary objectForKey:@"ecomExist"]; Aqui convertia el diccionario a array
        return dictionary;
    The code to implement the class..

    JFRed *red = [[JFRed alloc]init];
        red.delegado = self;
        [red descargar:[self.diccioPrincipal valueForKey:@"imgArt"] conId:1];
  2. Dookieman macrumors 6502

    Oct 12, 2009
    Have you tried using NSURLSession and including complete blocks? I know the NSURLConnection methods have been depreciated.
  3. dejo Moderator


    Staff Member

    Sep 2, 2004
    The Centennial State
    They have?
  4. chown33 macrumors 604

    Aug 9, 2009
    Sailing beyond the sunset
    The way I interpret that StackOverflow Q&A, and some of the links I read therefrom, is that the informal protocol is "deprecated", in favor of the formal protocol. This is a general pattern of progress, however, since quite a few informal protocols have been replaced by formal ones.

    The other thing I see in that discussion is a general agreement that the docs are a mess. However, assessment needs to be considered in light of the date when it was posted, which is generally well over a year ago, and sometimes more like two years.

    Furthermore, "deprecated" doesn't mean "removed". Something can be deprecated and still present, meaning "don't use this for new code, but old code will still work". Only when something is actually removed does old code stop working. (Or when a bug is introduced, but that's orthogonal to deprecation or removal.)
  5. Dookieman macrumors 6502

    Oct 12, 2009

    Thanks for pointing that out!

    I realize that deprecated isn't the same as removed and the code will still work, but from what I've read, they are trying to favor NSURLSession over NSURLConnection. Maybe the switch will help OP with his issue?

    Granted I'm still fairly new to this, but I switched to NSURLSession simply to cut back on the amount of code in a couple of my apps.
  6. joshuatbrown, May 7, 2014
    Last edited: May 7, 2014

    joshuatbrown macrumors newbie

    Jun 18, 2011
    I've looked over your code and nothing is jumping out - it looks correct. While I realize you're trying to fix this problem with NSURLConnection, I'll echo what others have said about NSURLSession - you really should look at using that instead, as it's better at handling authentication and you can modify the default behavior of response caching and server redirects. (Source) Not to mention it'll require a lot less code.

Share This Page