[英]Issue with URL string with special characters
我從互聯網上下載了一個mp3文件,然后將其保存到文檔目錄中,現在我嘗試播放該文件,但是該文件包含一些字符,例如Space
和[]
,這使應用程序崩潰,因為找不到文件。 原始文件名是這樣的:
Artist - Track [320].mp3
因此網址為: http://sample.com/Artist%20-%20Track%20[320].mp3
: http://sample.com/Artist%20-%20Track%20[320].mp3
但是我的文件下載器URL字符串用以下代碼替換了該標准URL:
http://sample.com/Artist%20-%20Track%20%5D320%5D.mp3
如您所見, []
字符已替換為%5D
!。 獲取url字符串的正確方法是什么?
從UIWebView獲取URL:
func requestIsDownloadable( request: URLRequest) -> Bool
{
let requestString : NSString = (request.url?.absoluteString)! as NSString
let fileExtention : String = requestString.pathExtension.lowercased()
fileTypes.fileTypeIcons(path: fileExtention as NSString , icon: fileType)
fileName.text = requestString.lastPathComponent as String
//******************************//
fileURL.text = request.url?.absoluteString
//******************************//
let isDownloadable : Bool = (
fileExtentions.contains(fileExtention)
)
return isDownloadable
}
func webView(_ webView: UIWebView, shouldStartLoadWith request: URLRequest, navigationType: UIWebViewNavigationType) -> Bool
{
if requestIsDownloadable(request: request)
{
initializeDownload(download: request)
return false
}
//urlTextField.text = webView.request?.url?.absoluteString
return true
}
目前尚不清楚您在哪里遇到問題。 但是,如果您需要解碼字符串並刪除百分比編碼,則可以執行以下操作:
let encodedString = "http://sample.com/Artist%20-%20Track%20%5D320%5D.mp3"
let decodedString = encodedString.removingPercentEncoding
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.