I have an ipad app that has a download that takes about 30 min. If the device is plugged in it works fine, but if not the connection seems to get lost and the app stops after the ipad "sleeps" from inactivity. How can I keep it awake or keep the connection going while the device sleeps?
connection code:
connection code:
Code:
// Create the request.
NSURLRequest *theRequest=[NSURLRequest requestWithURL:url
cachePolicy:NSURLRequestUseProtocolCachePolicy
timeoutInterval:60.0];
// create the connection with the request
// and start loading the data
NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
if (theConnection) {
// Create the NSMutableData to hold the received data.
// receivedData is an instance variable declared elsewhere.
[[NSFileManager defaultManager] createFileAtPath:filepath contents:nil attributes:nil];
} else {
// Inform the user that the connection failed.
}
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
// release the connection, and the data object
[connection release];
// inform the user
if (kDebugMode) NSLog(@"Connection failed! Error - %@ %@",
[error localizedDescription],
[[error userInfo] objectForKey:NSLocalizedFailureReasonErrorKey]);
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
NSFileHandle *file = [NSFileHandle fileHandleForUpdatingAtPath: filepath];
[file seekToEndOfFile];
progressView.progress = (float)[file offsetInFile]/(float)[response expectedContentLength];
[file writeData: data];
[file closeFile];
}