-1

我有个问题,

我需要将其发送到 php 文件。只有我得到一个 EXC_BAD_ACCES。

也许是因为我需要释放一个数据对象。但我不知道我的数据对象是什么。

我从http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html复制了这个。

也许你可以帮助我,这对你来说可能是一个很小的变化,但我看不到。

问候

   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];
}
4

1 回答 1

0

启用 NSZombies以获得有关崩溃的更多诊断信息。这很可能与过度释放对象有关。

查看您的代码,我认为问题出在此处:

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

这是一个自动释放的对象,它将在声明它的范围(方法的)结束时消失。

修复将 NSURLConnection 引用存储在retain类的属性中。

于 2012-09-03T14:50:06.187 回答