On passing the filename with spaces encoded with stringByAddingPercentEncodingWithAllowedCharacters to [NSFileManager fileExistsAtPath:], the path is not converted back to the decoded file name and returning false.

We have a Push-To-Talk application, which also allow user's to share the PDF file documents.

On receiving a PDF file document, which has a space in its file name. On saving the document in the DB with space. When user is trying to access the PDF file, in order to rule out issues where there can be special character's in its file name, we are encoding the file name using stringByAddingPercentEncodingWithAllowedCharacters, and URLPathAllowedCharacterSet is the character set used which converts space character (" ") to %20.

Later, path of the same encoded file name is sent to fileURLWithPath:. When the encoded URL is passed to [NSFileManager fileExistsAtPath:] the file is not found in the since in DB, file is saved with a space, but in the URL %20 is been swapped in-place of space character.

Issue case: Here, on passing the same encoded URL path of the PDF file to [NSFileManager fileExistsAtPath:] is returning false;

Query: On passing the filename with spaces encoded with stringByAddingPercentEncodingWithAllowedCharacters to [NSFileManager fileExistsAtPath:], the path is not converted back to the decoded file name and returning false

We have used a work around on this case, by forming the URL of the PDF file without encoding and passing it to fileURLWithPath, issue is not seen here. We have a query here, i.e. will fileURLWithPath will be able to handle different special characters without encoding.

We have also raised a Feedback Ticket on same: https://feedbackassistant.apple.com/feedback/16049504

The approach you’re using seems… well… strange. There are two much simpler options:

  • The best option is to store a bookmark rather than a URL. The advantage of using a bookmark is that it’ll do the right thing if the path to your container changes.

  • The second option is to store a path rather than a URL. Get that using the path property and reconstruct the user using +fileURLWithPath:.

IMPORTANT The drawback to the path approach is that you’ll need to explicitly handle the case where the path to your container has changed.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@" + "apple.com"

On passing the filename with spaces encoded with stringByAddingPercentEncodingWithAllowedCharacters to [NSFileManager fileExistsAtPath:], the path is not converted back to the decoded file name and returning false.
 
 
Q