Search code examples
iosobjective-casihttprequestnsmutableurlrequest

objective-c Replacing NSMutableURLRequest with ASIHTTPRequest


I am trying to replace my NSMutableURLRequest with an ASIHTTPRequest, I have tried the following below, but when I try this my app crashes. I get no errors just a warning:

Incompatible pointer types sending 'ASIHTTPRequest *' to parameter of type 'NSURLRequest *'

How do I fix this, what I tried is below and I have commented out what I was replacing:

receivedData = [[NSMutableData alloc]init];
//NSURL *JSONURL = [NSURL URLWithString:url];

NSURL *url = [NSURL URLWithString:url];
ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
[request setUseSessionPersistence:YES];
[request setUseKeychainPersistence:NO];
[request setUsername:@"username"];
[request setPassword:@"password"];
[request setDomain:@"domain"];
[request startSynchronous];

//NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:JSONURL];
jobsConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[jobsConnection start];

extra code:

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

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

    //NSLog(@"%@" , error);
    communityDictionary = nil;
}

-(void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSError *myError;
    if([connection isEqual:jobsConnection])
    {
        communityDictionary = [[NSDictionary alloc]initWithDictionary:
                               [NSJSONSerialization JSONObjectWithData:receivedData options:kNilOptions error:&myError]];
    }

}

Solution

  • You really don't want to use this code.

    //receivedData = [[NSMutableData alloc]init]; // <-- Not needed
    //NSURL *JSONURL = [NSURL URLWithString:url];
    
    NSURL *url = [NSURL URLWithString:url];
    ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
    [request setUseSessionPersistence:YES];
    [request setUseKeychainPersistence:NO];
    [request setUsername:@"username"];
    [request setPassword:@"password"];
    [request setDomain:@"domain"];
    [request startSynchronous];
    
    // You are already done getting the request, you just need to handle the response.
    NSError *error = [request error];
    if (!error) {
        // Put the response data into your received data object.
        receivedData = [[request responseData] mutableCopy];
        // TODO: copy in the code from -connectionDidFinishLoading:
    } else {
        NSLog(@"%@", error);
    }
    
    // There is no need to do anything with NSURLConnection.
    //NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:JSONURL];
    //jobsConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
    //[jobsConnection start];
    

    [request startSynchronous] already performed the request and received the response, all you needed to do was get the response out of the ASI object. NOTE: none of the -connection:didReceiveResponse:, -connection:didReceiveData:, -connectionDidFinishLoading: callbacks will be made, any work done in -connectionDidFinishLoading: will need to be done in the TODO: section I put above.


    NOTE: This is bad code. -startSynchronous blocks the thread until the request has completed. This will lock up your UI until the response has completed.

    • Don't use -startSynchronous look into -startAsynchronous.
    • Don't use ASIHTTPRequest use AFNetworking.
    • Think about a better way to refactor this solution!