I know that Apple supported application running in background mode, what I want to do is sending http request to get data from server then give user some notifications and I want repeat this action during several time using timmer. I'm using XCode-3.2.4 and Iphone4.1 simulator, it dose support background running, but when I made http request to my server, the responding function wasn't called. Anyone can help me how to do this ??? please. Code like this :
- (void)applicationDidEnterBackground:(UIApplication *)application{
UIApplication* app = [UIApplication sharedApplication];
bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ [app endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }]; // Start the long-running task and return immediately. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ // Do the work associated with the task.
responseData = [[NSMutableData data] retain];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.unpossible.com/misc/lucky_numbers.json"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
});
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response { [responseData setLength:0]; NSLog(@"%@",@"Server responded"); }
(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data { [responseData appendData:data]; NSLog(@"%@",@"Server transfered data"); }
(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { NSLog(@"%@",[NSString stringWithFormat:@"Connection failed: %@", [error description]]); }
(void)connectionDidFinishLoading:(NSURLConnection )connection {
[connection release]; NSLog(@"%@",@"Server transfered data"); UIApplication app = [UIApplication sharedApplication]; [app endBackgroundTask:bgTask]; bgTask = UIBackgroundTaskInvalid; }