Reputation: 89
I been trying to execute or request a url, inside de applicationWillTerminate event, but when i do it, the other functions, like:
- (NSURLRequest *)connection:(NSURLConnection *)connection
willSendRequest:(NSURLRequest *)request redirectResponse:(NSURLResponse *)redirectResponse {
[baseURL autorelease];
baseURL = [[request URL] retain];
return request;
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
they doesn't get executed, i guess because, the last function or event executed it is applicationWillTerminate. Is there a way to reach or do it.
Upvotes: 3
Views: 1167
Reputation: 11
I faced the same issue yesterday, and solved it using the snippet of code below:
NSURLResponse* response = nil;
NSString* requestUrlString = <your URL>;
NSMutableURLRequest *syncRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:requestUrlString] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30];
[NSURLConnection sendSynchronousRequest:syncRequest returningResponse:&response error:nil];
Upvotes: 1
Reputation: 12106
A useful technique at this point is to simply save a flag that says "on the next startup, the app needs to run this URL request." Then, the next time the app starts, if the flag is true, run the URL request. That might not be appropriate for every situation, but it has the advantage of having enough time to run.
Upvotes: 0
Reputation: 9093
Apple's documentation for the applicationWillTerminate delegate method for UIApplication states that your implementation of this method has approximately five seconds to perform tasks and return. Calling a method that uses the network and will run for an undetermined amount of time seems to violate the intent of the method.
Upvotes: 0
Reputation: 3133
I would be very surprised indeed if you were able to pull this off. applicationWillTerminate
isn't lying! Sure you can set up a request and send it, but once that method returns there's no way your delegate(s) are going to be hanging around long enough to receive and respond to callbacks.
I'm afraid that you'll probably have to find another way to achieve what you need.
Upvotes: 0