2016-10-15 2 views
0

В alomofire 3.5 следующий код работал хорошоСкачать аудио файл, используя SessionManager в Alamofire 4 в Swift 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, учебник для Swift 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.download вместо загрузить в SessionManager

+0

Если я хочу использовать SessionManager, чтобы иметь возможность фона, как я могу заставить его работать? – SergeH

Смежные вопросы