ios到php EXC_BAD_ACCES数据对象

时间:2012-09-03 14:42:10

标签: php ios exc-bad-access

我有一个问题,

我需要将它发送到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];
}

1 个答案:

答案 0 :(得分:0)

Enable NSZombies以获取有关崩溃的更多诊断信息。 这很可能与过度释放物体有关。

查看您的代码,我认为问题在于:

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

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

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