Search code examples
cocoapostnsurlconnectionnsmutableurlrequest

Cocoa POST issue


I´m newbie with Cocoa. I have one issue when I want to send data to my WS via POST

I have RequestPost program to inherit on all my projects

//
//  RequestPost.h
//
//  Created by Roberto on 10/01/13.
//  Copyright (c) 2013 CEM. All rights reserved.
//
#import <Foundation/Foundation.h>

@protocol DelegadoRedPost <NSObject>

-(void) terminaDescarga:(NSData*)datos conID:(NSInteger) id;
-(void) errorDescarga:(NSInteger)codigo conID:(NSInteger) id;

@end

@interface RequestPost : NSObject <NSURLConnectionDelegate>

@property (strong, nonatomic) NSObject <DelegadoRedPost> *delegado;
@property (nonatomic) NSInteger id;
@property (nonatomic, strong) NSMutableData *buffer;
@property (nonatomic, strong) NSURLConnection  *conexion;

-(void)descargar:(NSString*)direccion datosPost:(NSString*)datos conId:(NSInteger)id;


@end

//
//  RequestPost.m
//
//  Created by Roberto on 10/01/13.
//  Copyright (c) 2013 CEM. All rights reserved.
//

#import "RequestPost.h"

@implementation RequestPost

-(void)descargar:(NSString*)direccion datosPost:(NSString*)datos conId:(NSInteger)id
{
    self.id = id;

    NSURL *url            = [NSURL URLWithString:direccion];

    NSMutableURLRequest *req = [NSMutableURLRequest requestWithURL:url];
    //NSString *strLength = [NSString stringWithFormat:@"%d", datos.length]; aqui comento 18 abr 2016
    NSString *strLength = [NSString stringWithFormat:@"%lu", (unsigned long)datos.length];

    [req addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

    [req addValue:strLength forHTTPHeaderField:@"Content-Length"];
    [req setHTTPMethod:@"POST"];
    [req setHTTPBody:[datos dataUsingEncoding:NSUTF8StringEncoding]];


    self.conexion = [[NSURLConnection alloc] initWithRequest:req delegate:self];

    if(self.conexion){
        self.buffer = [NSMutableData data];
    }
}

#pragma mark - Métodos del Delegado de NSURLConnection

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    self.buffer.length = 0;
}

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data{

    [self.buffer appendData:data];
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection{
    [self.delegado terminaDescarga:self.buffer conID:self.id];
}

-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error{

    [self.delegado errorDescarga:error.code conID:self.id];
}

@end

Now, when I want to inherit last files I have gotten an error .... Incompatible pointer types assigning to NSObject ... In the line request.delegado = self;

This is the code when inherit

-(void) request
{
    RequestPost *request = [[RequestPost alloc] init];
    request.delegado = self;
    NSString *postStr = [NSString stringWithFormat:@"datos=%@",self.json];
    NSString *strUrl = @"http://www.futho7.com/WebService/subir_datos.php";
    [request descargar:strUrl datosPost:postStr conId:100];
}

How can I fix it?

Thanks & Regards


Solution

  • In the .m file that contains the request method, you need to indicate that the class conforms to the DelegadoRedPost protocol and implement the required protocol methods.

    Add this just before the @implementation line:

    @interface WhateverClassNameThisIs () <DelegadoRedPost>
    
    @end
    

    Obviously replace WhateverClassNameThisIs with the actual name of this class.

    As a side note, you should change the declaration of the delegado property from:

    @property (strong, nonatomic) NSObject <DelegadoRedPost> *delegado;
    

    to:

    @property (weak, nonatomic) id<DelegadoRedPost> *delegado;
    

    Note the two changes - delegates should normally be weak, not strong. This avoids reference cycles. And the type should be with id, not NSObject. The protocol itself extends the NSObject protocol.