-1

i have a problem,

I need to send this to a php file. Only i get a EXC_BAD_ACCES.

Maybe it's because i need to release a data object. But i don't know what my data object is.

I copied this from http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html.

Maybe you could help me, its probably a very little change for you, but i don't see it.

Greets

   NSURL *url = [NSURL URLWithString:@"http://www.yourdomain.com/locatie.php"];

NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url
                                                            cachePolicy:NSURLRequestReloadIgnoringCacheData
                                                      timeoutInterval:60];

[theRequest setHTTPMethod:@"POST"];
[theRequest setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];

NSString *postData = [NSString stringWithFormat:@"longitude=%@&latitude=%@&stringFromDate=%@", longitude, latitude, stringFromDate];

NSString *length = [NSString stringWithFormat:@"%d", [postData length]];
[theRequest setValue:length forHTTPHeaderField:@"Content-Length"];

[theRequest setHTTPBody:[postData dataUsingEncoding:NSASCIIStringEncoding]];

NSURLConnection *sConnection = [NSURLConnection connectionWithRequest:theRequest   delegate:self];
[sConnection start];

if (sConnection) {
    // Create the NSMutableData to hold the received data.
    // receivedData is an instance variable declared elsewhere.
    theRequest = [[NSMutableData data] retain];
} else {
    // Inform the user that the connection failed.
}


}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse   *)response
{
// This method is called when the server has determined that it
// has enough information to create the NSURLResponse.

// It can be called multiple times, for example in the case of a
// redirect, so each time we reset the data.

// receivedData is an instance variable declared elsewhere.
}
- (void)connection:(NSURLConnection *)connection
 didFailWithError:(NSError *)error
{
// release the connection, and the data object
[connection release];
// receivedData is declared as a method instance elsewhere

// inform the user
NSLog(@"Connection failed! Error - %@ %@",
      [error localizedDescription],
      [[error userInfo] objectForKey:NSURLErrorFailingURLStringErrorKey]);
}

 - (void)connectionDidFinishLoading:(NSURLConnection *)connection
 {
// do something with the data
// receivedData is declared as a method instance elsewhere
NSLog(@"Succeeded! Received bytes of data");

// release the connection, and the data object
[connection release];
}
David Raijmakers
  • 1,369
  • 1
  • 16
  • 40
  • on which line do you get the EXC_BAD_ACCES? – sergio Sep 03 '12 at 14:46
  • That's also my problem. It doesn't give that error. I just think that i need to release the data object. I just deleted the data object lines because i didn't knew where it was, and what the data object was – David Raijmakers Sep 03 '12 at 14:48

1 Answers1

0

Enable NSZombies in order to get more diagnostic info about the crash. This is highly likely related to over-releasing an object.

Looking at your code, I think that the problem is here:

NSURLConnection *sConnection = [NSURLConnection connectionWithRequest:theRequest   delegate:self];

this is an autoreleased object that will disappear at the end of the scope in which it is declared (the method's).

The fix is storing the NSURLConnection reference in a retain property of your class.

Community
  • 1
  • 1
sergio
  • 68,819
  • 11
  • 102
  • 123