NSURLConnection switch to NSURLSession returning 404 response

41 views Asked by At

I am attempting to convert my project from using NSURLConnection to NSURLSession. However, after making the switch I cannot seem to get a response from the server. The returned response from the server is always 404.

My original code, using NSURLConnection:

@implementation RecorderManager

- (void)sendRequestToURL:(NSString*)url withData:(NSData*)postData forScreen:(NSString *)inScreenName
{
    RecorderAsynchSubmit* delegate = [[RecorderAsynchSubmit alloc] initWithScreenName:inScreenName andURL:url];
    NSURLConnection *urlConnection = [[NSURLConnection alloc] initWithRequest:request delegate:delegate];
}


@implementation RecorderAsynchSubmit
// Implementing NSURLConnectionDelegate, NSURLConnectionDataDelegate

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

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [httpResponse setLength:0];
    httpResponseCode = [((NSHTTPURLResponse *) response) statusCode];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)httpdata
{
    [httpResponse appendData:httpdata];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSString *responseString = [[[NSString alloc] initWithData:httpResponse encoding:NSUTF8StringEncoding] copy];
    if (httpResponseCode == STATUS_CODE_VALID) { // response code for entry submitted
        responseString = @"Submit Succeeded";
    } else {
        responseString = [NSString stringWithFormat:@"Your entry could not be submitted. Data has been stored locally. Error Message: %@", responseString];
    }
    [[NSNotificationCenter defaultCenter]
     postNotificationName:@"submissionCompleteEvent"
     object:[[RecorderNotificationMessage alloc] initWithStatusMessage:responseString detailedMessage:responseString]];
}

My updated code using NSURLSessionTask:

@implementation RecorderManager

- (void)sendRequestToURL:(NSString*)url withData:(NSData*)postData forScreen:(NSString *)inScreenName
{
    RecorderAsynchSubmit* delegate = [[RecorderAsynchSubmit alloc] initWithScreenName:inScreenName andURL:url];
    NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
    NSURLSession *session = [NSURLSession sessionWithConfiguration:configuration delegate:delegate delegateQueue:nil];
    NSURL *URL = [NSURL URLWithString:url];
    NSURLSessionTask *task = [session dataTaskWithURL:URL];
    [task resume];
}


@implementation RecorderAsynchSubmit
// Implementing NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDataDelegate

- (void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask didReceiveResponse:(NSURLResponse *)response
completionHandler:(void (^)(NSURLSessionResponseDisposition disposition))completionHandler {
    [httpResponse setLength:0];
    httpResponseCode = [((NSHTTPURLResponse *) response) statusCode];
    completionHandler(NSURLSessionResponseAllow);
}

-(void)URLSession:(NSURLSession *)session dataTask:(NSURLSessionDataTask *)dataTask
   didReceiveData:(NSData *)data {
    [httpResponse appendData:data];
}

- (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task
didCompleteWithError:(NSError *)error {
    if (error) {
        // Handle error
    } else {
        NSString *responseString = [[[NSString alloc] initWithData:httpResponse encoding:NSUTF8StringEncoding] copy];
        if (httpResponseCode == STATUS_CODE_VALID) {
            responseString = @"Submit Succeeded";
        } else {
            responseString = [NSString stringWithFormat:@"Your entry could not be submitted. Data has been stored locally. Error Message: %@", responseString];
        }
        [[NSNotificationCenter defaultCenter]
         postNotificationName:@"submissionCompleteEvent"
         object:[[RecorderNotificationMessage alloc] initWithStatusMessage:responseString detailedMessage:responseString]];
    }
}

The original code is working fine and returns the expected result, however the updated code always returns a 404 response from the server.

0

There are 0 answers