简体   繁体   中英

iOS background download doesnt work

I am implementing a multiple images download for my app (at least 4000+ images), to do this, i have been looking for a best practice for a while, the solution that i found is to use a NSURLSession with a background configuration, i did all that i found in examples, but, sill not working in my real device.

I made a test showing a local notification when the downloads were completed, when i am in debuggin mode the notification is shown perfectly, but when i unplug the device, the notification doesnt work.

Note: The memory ussage in debug is low, but the energy impact is very high, it is possible that the operative system kill the process to prevent batery loss?

Any ideas?

For downloading images, I'd highly recommend SDWebImage . It's a very robust library that's been maintained for a long time and should be able to do everything you're looking for.

Even just for downloading images, it's as easy as this:

SDWebImageDownloader *downloader = [SDWebImageDownloader sharedDownloader];
[downloader downloadImageWithURL:[NSURL URLWithString:@"https://www.example.com/example.png/"]
                         options:0
                        progress:^(NSInteger receivedSize, NSInteger expectedSize) {
                            NSLog(@"Image download %.02f%% complete.", (CGFloat)receivedSize/(CGFloat)expectedSize * 100);
                        }
                       completed:^(UIImage *image, NSData *data, NSError *error, BOOL finished) {
                           if(image && finished) {
                               NSLog(@"The image has finished downloading!");

                               //Perform your image related code here
                           }
                       }];

You could use it along with background tasks for your use case.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM