Question

I use IOS7

I start download and i lock screen, and i open screen. My program is shutdown...

Is there some can help me ?

This is major code:

init download url:

NSURL *url = [NSURL URLWithString:dictionaryUrl];


ASIHTTPRequest *request = [[ASIHTTPRequest alloc] initWithURL:url];

set ASIHTTPRequest delegate:

request.delegate = self;

init file save path:

NSString *savePath = [path stringByAppendingPathComponent:name];

bool b=[ fileManager createFileAtPath :savePath contents : nil attributes : nil ];
if (b){
    fileHandle=[ NSFileHandle fileHandleForWritingAtPath :savePath];
}

[request setAllowResumeForFileDownloads:NO];
[request setDelegate:self];
[request setDidFinishSelector:@selector(requestDone:)];
[request setDidFailSelector:@selector(requestWentWrong:)];

[request setDataReceivedBlock :^( NSData * data){
    [fileHandle seekToEndOfFile ];
    [fileHandle writeData :data];
    [ label setText :[ NSString stringWithFormat : @"downloading...%.1f %%" , process . progress * 100 ]];

}];

[request setDownloadProgressDelegate:process];

add to ASINetworkQueue:

[self.netWorkQueue addOperation:request];

relase request:

[request release];
Was it helpful?

Solution

First, you need to enable the request to run at background. add this :

[request setShouldContinueWhenAppEntersBackground:YES];

for quick task it's possible, works like charm. but for long running task, just like you wanted, to keep continue download, you need to add a bit code, to enable this request from being terminated because run too long.

I've answered question for location update by adding timer and fire API every x seconds indefinitely. using UIBackgroundTaskIdentifier and NSTimer. This should give you an idea. should be same concept of what you are doing.

How do I get a background location update every n minutes in my iOS application?

OTHER TIPS

When your screen gets lock your app goes to background.You can perform tasks for a limited time after your application is directed to go to the background, but only for the duration provided. Running for longer than this will cause your application to be terminated. See the "Completing a Long-Running Task in the Background" section of the iOS Application Programming Guide for how to go about this.

For iOS7 you can check this link https://stackoverflow.com/a/19355437/1372368.

And the code snippet to run task in background you can find various links like:How to keep an iPhone app running on background fully operational

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top