Xcode:下载 mp3 文件 [英] Xcode: Download mp3 file
本文介绍了Xcode:下载 mp3 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在编写一个应用程序,我可以在其中输入来自网络的 mp3 文件的 URL,当我单击下载时,该应用程序将在应用程序中下载它.有人能帮我吗 ?我是ios开发的新手.我正在努力学习 Swift
I am writing an app where, I can enter URL of a mp3 file from web and when i click download, the app will download it in the app. Can someone help me ? I am new to ios development. I am trying to learn Swift
推荐答案
Xcode 8 • Swift 3
if let audioUrl = URL(string: "http://freetone.org/ring/stan/iPhone_5-Alarm.mp3") {
// 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")
// if the file doesn't exist
} else {
// you can use NSURLSession.sharedSession to download the data asynchronously
URLSession.shared.downloadTask(with: audioUrl) { location, response, error in
guard let location = location, error == nil else { return }
do {
// after downloading your file you need to move it to your destination url
try FileManager.default.moveItem(at: location, to: destinationUrl)
print("File moved to documents folder")
} catch {
print(error)
}
}.resume()
}
}
这篇关于Xcode:下载 mp3 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文