2016-10-15 259 views
0

下載使用SessionManager一個音頻文件Alamofire 4在alomofire 3.5下面的代碼行之有效斯威夫特3

self.sessionManager.download(.GET, AppConstants.musicFileURL + musicFilename, destination: { (url, response) -> NSURL in 
     let fileManager = NSFileManager.defaultManager() 
     let directoryURL = fileManager.URLsForDirectory(.DocumentDirectory, inDomains: .UserDomainMask)[0] 
     let fileUrl = directoryURL.URLByAppendingPathComponent(musicFilename) 
     return fileUrl 
     }) 
    .progress { bytesRead, totalBytesRead, totalBytesExpectedToRead in 
     self.percentageDownloaded[exhibitId]![artworkId] = (Double(totalBytesRead) ,Double(totalBytesExpectedToRead)) 
     let meanPercentageDone = self.calculatePercentageDoneForExhibit(exhibitId, artworkArray: self.artworkArray) 
     let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate 
     appDelegate.notifyDownloadProgress(meanPercentageDone) 
    } 
    .response { _, _, _, _ in 

     NSLog("Completed downloading audio file,%@ , for artwork %@", musicFilename, artworkId) 
     if DataManager.saveMusicDownloadComplete(artworkId, exhibitId: exhibitId){ 
     let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate 
     appDelegate.notifyMusicDownloadsComplete(exhibitId) 
     self.percentageDownloaded[exhibitId] = [String: (Double, Double)?]() 
     NSLog("when all music files have been downloaded") 
     } 
    } 
} 

如何遷移上面的代碼Alamofire 4.無法看到遷移文檔中的任何評論。

+0

改變,而不是下載試試吧:https://www.raywenderlich.com/121540/alamofire-tutorial-getting-started –

+0

@JamshedAlam,該教程是斯威夫特2.2。可惜不是Swift 3 –

+0

你可以把它轉換成swift 3 –

回答

0
let destination: DownloadRequest.DownloadFileDestination = { _, _ in 
    var documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] 

    documentsURL.appendPathComponent(musicFilename) 
    return (documentsURL, [.removePreviousFile]) 
} 

Alamofire.download(AppConstants.musicFileURL + musicFilename, to: destination) 


    .downloadProgress { progress in 
     print("Download Progress: \(progress.fractionCompleted)") 
    } 

    .responseString(completionHandler: { (response) in 
     print("Success: \(response.result.isSuccess)") 
     print("Response String: \(response.result.value)") 

    }) 

Alamofire.downloadSessionManager

+0

如果我想使用SessionManager來獲得背景功能,我該如何讓它工作? – SergeH