NSURLConnection request seems to disappear into thin air
Posted
by ibergmark
on Stack Overflow
See other posts from Stack Overflow
or by ibergmark
Published on 2010-05-25T14:59:14Z
Indexed on
2010/05/25
15:01 UTC
Read the original article
Hit count: 484
iphone
|nsurlconnection
Hi Everybody!
I'm having trouble with a NSURLConnection request. My app calls a routine called sayHello to identify a user's device. This code works fine for all devices I've tested it on, but for some devices the request just seems to disappear into thin air with no errors or popups on the device.
One specific device that fails is an iPod Touch 2G running OS 3.1.3. The app start's fine and doesn't crash, and none of my error popup messages are displayed.
I just can't understand why my server never receives the request since the call to initWithRequest returns a pointer.
Any help is much appreciated.
Thanks.
Here's the relevant header info:
@interface Globals : NSObject
{
UserItem *userData;
NSURLConnection *urlConnection;
NSString *imageCacheLocation;
NSOperationQueue *opQueue;
}
Here's the implementation of sayHello:
- (void)sayHello:(BOOL)updateVisits;
{
NSString *updString;
if (updateVisits)
updString = @"Y";
else
updString = @"N";
NSDictionary *dataDict = [[NSDictionary alloc] initWithObjectsAndKeys:
[[UIDevice currentDevice] uniqueIdentifier], @"id",
kProgVersion, @"pv",
@"1", @"pr",
userData.tagID, @"tg",
updString, @"uv",
nil];
NSString *urlString = [[NSString alloc] initWithString:kHelloURL];
urlConnection = [self localPOST:dataDict toUrl:urlString delegate:self];
[dataDict release];
[urlString release];
}
- (NSURLConnection *)localPOST:(NSDictionary *)dictionary toUrl:(NSString *)urlString delegate:(id)delegate
{
NSString *myBounds = [[NSString alloc] initWithString:@"0xKmYbOuNdArY"];
NSMutableData *myPostData = [[NSMutableData alloc] initWithCapacity:10];
NSArray *formKeys = [dictionary allKeys];
for (int i = 0; i < [formKeys count]; i++)
{
[myPostData appendData:[[NSString stringWithFormat:@"--%@\n", myBounds]
dataUsingEncoding:NSUTF8StringEncoding]];
[myPostData appendData:[[NSString stringWithFormat:
@"Content-Disposition: form-data; name=\"%@\"\n\n%@\n",
[formKeys objectAtIndex:i],
[dictionary valueForKey:[formKeys objectAtIndex:i]]]
dataUsingEncoding:NSUTF8StringEncoding]];
}
[myPostData appendData:[[NSString stringWithFormat:@"--%@--\n", myBounds]
dataUsingEncoding:NSUTF8StringEncoding]];
NSURL *myURL = [[NSURL alloc] initWithString:urlString];
NSMutableURLRequest *myRequest = [[NSMutableURLRequest alloc] initWithURL:myURL
cachePolicy:NSURLRequestReloadIgnoringCacheData
timeoutInterval:30];
NSString *myContent = [[NSString alloc] initWithFormat:
@"multipart/form-data; boundary=%@", myBounds];
[myRequest setValue:myContent forHTTPHeaderField:@"Content-Type"];
[myRequest setHTTPMethod:@"POST"];
[myRequest setHTTPBody:myPostData];
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:myRequest delegate:delegate];
if (!connection)
{
[[Globals sharedGlobals] showAlertWithTitle: NSLocalizedString(
@"Connection failed",
@"alert title - connection failed")
message: NSLocalizedString(
@"Could not open a connection.",
@"alert message - connection failed")];
}
[myBounds release];
[myPostData release];
[myURL release];
[myRequest release];
[myContent release];
return connection;
}
- (void)handleNetworkError:(NSError *)error
{
if (networkErrorAlert)
return;
networkErrorAlert = YES;
[[Globals sharedGlobals] showAlertWithTitle: NSLocalizedString(
@"Network error", @"alert title - network error")
message: [NSString stringWithFormat: NSLocalizedString(
@"This app needs a network connection to function properly.",
@"alert message - network error")]
otherButtons:nil
delegate:self];
}
- (void) connectionDidFinishLoading:(NSURLConnection *)connection
{
[urlConnection release];
urlConnection = nil;
}
- (void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
[self handleNetworkError:error];
[urlConnection release];
urlConnection = nil;
}
© Stack Overflow or respective owner