iOS.Lover
iOS.Lover

Reputation: 6051

Issue with URL string with special characters

I downloaded a mp3 file from internet and then save it to document directory, now I tried to play it , but file has some characters like Space and [] ,and that makes app crash because it cannot locate the file. The original file name is like this :

Artist - Track [320].mp3

so the URL is :http://sample.com/Artist%20-%20Track%20[320].mp3

But my file downloader url string replace that standard url with this :

http://sample.com/Artist%20-%20Track%20%5D320%5D.mp3

As you can see the [] characters has been replaced with %5D!. What is the proper way to get url string ?

Getting URL from UIWebView :

   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
    }

Upvotes: 1

Views: 1338

Answers (1)

Nikita Gaidukov
Nikita Gaidukov

Reputation: 771

It is not very clear where do you have a problem. But if you need to decode your string and remove percent encoding you could do this:

let encodedString = "http://sample.com/Artist%20-%20Track%20%5D320%5D.mp3"
let decodedString = encodedString.removingPercentEncoding

Upvotes: 6

Related Questions