美文网首页转载
iOS后台下载

iOS后台下载

作者: linwkr | 来源:发表于2016-08-15 16:15 被阅读306次

    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后台下载

        本文链接:https://www.haomeiwen.com/subject/ewmusttx.html