URLSession委托方法未调用

时间:2019-02-05 09:39:11

标签: ios swift swift4 nsurlsession

我下载了歌曲,然后在按钮上播放。此部分中的所有内容均正常运行。但是我想监视下载指示器并暂停并继续下载。但是未调用委托方法。

class ViewController: UIViewController, URLSessionDownloadDelegate, URLSessionTaskDelegate,URLSessionDelegate  {
    @IBOutlet weak var progressBar: UIProgressView!

    let url = URL(string: "https://www.dropbox.com/s/s7zkka3at171wf8/Track01.mp3?raw=1")!

    var audioPlayer : AVAudioPlayer!

    var task: URLSessionDownloadTask!
    let configuration1 = URLSessionConfiguration.default
    var urlSession: URLSession!
    let operationQueue1 = OperationQueue()

    var url1 : URL!

    // MARK: - Life CYcle
    override func viewDidLoad() {
        super.viewDidLoad() 
    }

    // MARK: - IBActions
    @IBAction func playAudio(_ sender: Any) {
        do {
            audioPlayer = try AVAudioPlayer(contentsOf: self.url1)
            audioPlayer.play()
        }
        catch {
            print("error")
        }
    }

    @IBAction func pauseAudio(_ sender: Any) {}

    @IBAction func downloadButton(_ sender: Any) {
        guard let audioUrl = URL(string: "https://www.dropbox.com/s/s7zkka3at171wf8/Track01.mp3?raw=1") else {
            return
        }

        // then lets create your document folder url
        let documentsDirectoryURL =  FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!

        // lets create your destination file url
        let destinationUrl = documentsDirectoryURL.appendingPathComponent(audioUrl.lastPathComponent)
        print(destinationUrl)

        // to check if it exists before downloading it
        if FileManager.default.fileExists(atPath: destinationUrl.path) {
            print("The file already exists at path")
            self.url1 = destinationUrl

            // if the file doesn't exist
        }
        else {

            print("0")
            urlSession = URLSession(configuration: configuration1, delegate: self, delegateQueue: operationQueue1)

            task = urlSession.downloadTask(with: audioUrl) { (location, response, error) in
                guard
                    let location = location else {
                    return
                }

                do {
                    try FileManager.default.moveItem(at: location ,to : destinationUrl)
                    print("File moved to documents folder")
                    self.url1 = destinationUrl

                }
                catch let error as NSError{
                    print(error.localizedDescription)
                }
                print("1")
            }

            task.resume()
            print("2")

        }

        print("3")
    }

    // MARK: -
    func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didFinishDownloadingTo location: URL) {
        print("fineshed download file")
    }

    func urlSession(_ session: URLSession, downloadTask: URLSessionDownloadTask, didWriteData bytesWritten: Int64, totalBytesWritten: Int64, totalBytesExpectedToWrite: Int64) {
        print(totalBytesWritten)
        print(totalBytesExpectedToWrite)
    }
    func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) {
        print("All success")
    }
}

1 个答案:

答案 0 :(得分:0)

尝试一下

let configuration = URLSessionConfiguration.default
let session = URLSession(configuration: configuration, delegate: self, delegateQueue: OperationQueue.main)