Url string return nil iOS swift issue

Solution:

 If your filename had space then it returns nil while convert it into URL.So use the addingPercentEncoding to replace the space to %20 then it will works good.

        let fileManager = FileManager.default
        let folderPath = localPath

        let inputFilePath = documentsDirPath.appendingFormat(folderPath)
        
            var urlString = fileName.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)
            urlString = "/".appending(urlString!)
            let toURL = URL(string: documentsDirPath.appendingFormat(urlString!))
            let fileURL = URL(string: inputFilePath.appendingFormat(urlString!))
                print(toURL)
                print(fileURL)

Comments

Popular posts from this blog

Invalid bundle error while upload the app to the app Store

Saved Image in document directory and save path in coredata not able to fetch the image file iOS swift