Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

joak

macrumors newbie
Original poster
Mar 7, 2014
27
0
Hi

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

My "h file" code

Code:
#import <Foundation/Foundation.h>
#import "Reachability.h"
//implementar protocolo
@protocol RedDelegate <NSObject>

-(void) terminaDescarga:(NSData*)datos
                  conId:(NSInteger)id
                    httpCodeReturn:(int)code
httpStringCodereturn:(NSString*)codestring;


-(void) errorEnDescarga:(NSInteger)codigo descrip:(NSString *)descError conId:(NSInteger)id;

@end

@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;

@end

The "m file" code, first execute "connectToWSArticulo" method, and then "descargar" method and any trigger the "connectionDidFinishLoading" method . The informartion it's download correct.

Code:
#import "JFRed.h"

@implementation JFRed


-(void) descargar:(NSString*)direccion conId:(NSInteger)id
{
    self.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];

    if(self.conexion!=nil)
    {
        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 conId:self.id httpCodeReturn:self.httpCodeReturn httpStringCodereturn:self.httpStringCodereturn];
}


-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
    [self.delegado errorEnDescarga:error.code descrip:[error.userInfo objectForKey:NSLocalizedDescriptionKey ] conId:self.id];   
}



#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 = @"192.168.1.52:8181"; IP de la intranet(ya no se deberá de usuar)
    NSString *lstrIp = @"189.210.175.215:8085";
    //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;
}

@end

The code to implement the class..

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

Dookieman

macrumors 6502
Oct 12, 2009
390
67
Have you tried using NSURLSession and including complete blocks? I know the NSURLConnection methods have been depreciated.
 

chown33

Moderator
Staff member
Aug 9, 2009
10,747
8,420
A sea of green

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.)
 

Dookieman

macrumors 6502
Oct 12, 2009
390
67
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.)


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.
 

joshuatbrown

macrumors newbie
Jun 18, 2011
8
0
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.
 
Last edited:
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.