iOS后台下载

iOS后台下载

后台下载是 NSURLSession 带来的一个新特性,区别于以往进入后台只有5分钟完成额外操作

- (void) doUpdate

    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{

        [self beginBackgroundUpdateTask];

        NSURLResponse * response = nil;
        NSError  * error = nil;
        NSData * responseData = [NSURLConnection sendSynchronousRequest: request returningResponse: &response error: &error];

        // Do something with the result

        [self endBackgroundUpdateTask];
    });
}

- (void) beginBackgroundUpdateTask
{
//必须的,否则进入后台超时后,APP会被杀掉
    self.backgroundUpdateTask = [[UIApplication sharedApplication] beginBackgroundTaskWithExpirationHandler:^{
        [self endBackgroundUpdateTask];
    }];
}

- (void) endBackgroundUpdateTask
{
    [[UIApplication sharedApplication] endBackgroundTask: self.backgroundUpdateTask];
    self.backgroundUpdateTask = UIBackgroundTaskInvalid;
}   

iOS7以后允许我们,将下载任务交给系统管理,只要 APP 不是被强制用户杀掉,系统会在下载任务完成后,通过特定的函数通知我们。

//生成一个后台session,当然 APP 在前台时,此session 也和普通 session 一样工作。
let config = NSURLSessionConfiguration.backgroundSessionConfigurationWithIdentifier("downloadSession")
        self.session = NSURLSession(configuration: config, delegate: self, delegateQueue: NSOperationQueue.mainQueue())
func application(application: UIApplication, handleEventsForBackgroundURLSession identifier: String, completionHandler: () -> Void) {
        print(identifier)
        let _ = TaskManager.sharedInstance
        ch = completionHandler
    }
extension TaskManager: NSURLSessionDownloadDelegate{
    func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask,
                    didFinishDownloadingToURL location: NSURL) {
        
        print("URLSession(sessionNSURLSessiondownloadTaskNSURLSessionDownloadTask didFinishDownloadingToURL locationNSURL")
        var fileName = ""
        
        for (index, task) in taskList.enumerate() {
            if task.taskIdentifier == downloadTask.taskIdentifier {
                taskList[index].finished = true
                fileName = task.url.lastPathComponent!
            }
        }
        
        if let documentURL = NSFileManager.defaultManager().URLsForDirectory(NSSearchPathDirectory.DocumentDirectory, inDomains: NSSearchPathDomainMask.UserDomainMask).first {
            
            let destURL = documentURL.URLByAppendingPathComponent(fileName+"123")
            
            do {
                try NSFileManager.defaultManager().moveItemAtURL(location, toURL: destURL)
            }
            catch {
                print(error)
            }
            saveTaskList()
            NSNotificationCenter.defaultCenter().postNotificationName(DownloadTaskNotification.Finish.rawValue, object: downloadTask.taskIdentifier)
        }
        
    }
    
    func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didResumeAtOffset fileOffset: Int64, expectedTotalBytes: Int64) {
        
        
    }
    
    func URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
        
        print("URLSession(session: NSURLSession, downloadTask: NSURLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64)")
        let progressInfo = ["taskIdentifier": downloadTask.taskIdentifier,
                            "totalBytesWritten": NSNumber(longLong: totalBytesWritten),
                            "totalBytesExpectedToWrite": NSNumber(longLong: totalBytesExpectedToWrite)]
        
        NSNotificationCenter.defaultCenter().postNotificationName(DownloadTaskNotification.Progress.rawValue, object: progressInfo)
        
    }
    
    func URLSessionDidFinishEventsForBackgroundURLSession(session: NSURLSession) {
        print("URLSessionDidFinishEventsForBackgroundURLSession")
        (UIApplication.sharedApplication().delegate as! AppDelegate).ch?()
        (UIApplication.sharedApplication().delegate as! AppDelegate).ch = nil
    }
    
}

上述 API 调用顺序如下:


iOS后台下载_第1张图片

你可能感兴趣的:(iOS后台下载)