- @property (weak, nonatomic) IBOutlet UIProgressView *progressView;
- - (IBAction)start:(id)sender;
- @property (nonatomic) NSURLSession *session;
- @property (nonatomic) NSURLSessionDownloadTask *downloadTask;
- @property (strong, nonatomic) UIDocumentInteractionController *documentInteractionController;
- @interface ViewController : UIViewController < NSURLSessionDelegate, NSURLSessionTaskDelegate, NSURLSessionDownloadDelegate,UIDocumentInteractionControllerDelegate>
- static NSString *DownloadURLString = @"https://developer.apple.com/library/ios/documentation/Cocoa/Reference/Foundation/ObjC_classic/FoundationObjC.pdf";
- self.session = [self backgroundSession];
- self.progressView.progress = 0;
- self.progressView.hidden = YES;
- - (NSURLSession *)backgroundSession {
- static NSURLSession *session = nil;
- static dispatch_once_t onceToken;
- dispatch_once(&onceToken, ^{
- NSURLSessionConfiguration *configuration = [NSURLSessionConfiguration backgroundSessionConfiguration:@"com.example.apple-samplecode.SimpleBackgroundTransfer.BackgroundSession"];
- session = [NSURLSession sessionWithConfiguration:configuration delegate:self delegateQueue:nil];
- });
- return session;
- }
- - (IBAction)start:(id)sender {
- if (self.downloadTask) {
- return;
- }
- NSURL *downloadURL = [NSURL URLWithString:DownloadURLString];
- NSURLRequest *request = [NSURLRequest requestWithURL:downloadURL];
- self.downloadTask = [self.session downloadTaskWithRequest:request];
- [self.downloadTask resume];
- self.progressView.hidden = NO;
- }
- - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite {
- if (downloadTask == self.downloadTask) {
- double progress = (double)totalBytesWritten / (double)totalBytesExpectedToWrite;
- NSLog(@"DownloadTask: %@ progress: %lf", downloadTask, progress);
- dispatch_async(dispatch_get_main_queue(), ^{
- self.progressView.progress = progress;
- });
- }
- }
- - (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didFinishDownloadingToURL:(NSURL *)downloadURL {
- NSFileManager *fileManager = [NSFileManager defaultManager];
- NSArray *URLs = [fileManager URLsForDirectory:NSDocumentDirectory inDomains:NSUserDomainMask];
- NSURL *documentsDirectory = [URLs objectAtIndex:0];
- NSURL *originalURL = [[downloadTask originalRequest] URL];
- NSURL *destinationURL = [documentsDirectory URLByAppendingPathComponent:[originalURL lastPathComponent]];
- NSError *errorCopy;
- // For the purposes of testing, remove any esisting file at the destination.
- [fileManager removeItemAtURL:destinationURL error:NULL];
- BOOL success = [fileManager copyItemAtURL:downloadURL toURL:destinationURL error:&errorCopy];
- if (success) {
- dispatch_async(dispatch_get_main_queue(), ^{
- //download finished - open the pdf
- self.documentInteractionController = [UIDocumentInteractionController interactionControllerWithURL:destinationURL];
- // Configure Document Interaction Controller
- [self.documentInteractionController setDelegate:self];
- // Preview PDF
- [self.documentInteractionController presentPreviewAnimated:YES];
- self.progressView.hidden = YES;
- });
- } else {
- NSLog(@"Error during the copy: %@", [errorCopy localizedDescription]);
- }
- }
- -(void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didResumeAtOffset:(int64_t)fileOffset expectedTotalBytes:(int64_t)expectedTotalBytes {
- }
- - (void)URLSession:(NSURLSession *)session task:(NSURLSessionTask *)task didCompleteWithError:(NSError *)error {
- if (error == nil) {
- NSLog(@"Task: %@ completed successfully", task);
- } else {
- NSLog(@"Task: %@ completed with error: %@", task, [error localizedDescription]);
- }
- double progress = (double)task.countOfBytesReceived / (double)task.countOfBytesExpectedToReceive;
- dispatch_async(dispatch_get_main_queue(), ^{
- self.progressView.progress = progress;
- });
- self.downloadTask = nil;
- }
- - (void)URLSessionDidFinishEventsForBackgroundURLSession:(NSURLSession *)session {
- AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
- if (appDelegate.backgroundSessionCompletionHandler) {
- void (^completionHandler)() = appDelegate.backgroundSessionCompletionHandler;
- appDelegate.backgroundSessionCompletionHandler = nil;
- completionHandler();
- }
- NSLog(@"All tasks are finished");
- }
- #import "AppDelegate.h"
- @property (strong, nonatomic) UIWindow *window;
- @property (copy) void (^backgroundSessionCompletionHandler)();
- - (void)application:(UIApplication *)application handleEventsForBackgroundURLSession:(NSString *)identifier
- completionHandler:(void (^)())completionHandler {
- self.backgroundSessionCompletionHandler = completionHandler;
- //add notification
- [self presentNotification];
- }
- -(void)presentNotification{
- UILocalNotification* localNotification = [[UILocalNotification alloc] init];
- localNotification.alertBody = @"Download Complete!";
- localNotification.alertAction = @"Background Transfer Download!";
- //On sound
- localNotification.soundName = UILocalNotificationDefaultSoundName;
- //increase the badge number of application plus 1
- localNotification.applicationIconBadgeNumber = [[UIApplication sharedApplication] applicationIconBadgeNumber] + 1;
- [[UIApplication sharedApplication] presentLocalNotificationNow:localNotification];
- }
- application.applicationIconBadgeNumber = 0;
联系客服