0
Run below code in playground

import UIKit
let serverUrl = "smb://example.net/Data/OS/Home/test/untitled folder"
let sharePath = NSURL(string: serverUrl)

sharePath value is returning nil

How to handle this

MacDeveloper
  • 1,334
  • 3
  • 16
  • 49
  • Does this answer your question? [How to encode a URL in Swift](https://stackoverflow.com/questions/24879659/how-to-encode-a-url-in-swift) – RTXGamer Nov 02 '22 at 19:03
  • Normally you don't. Your string is not a valid URL and once you have it, it cannot be safely converted to a valid URL because you don't know which parts are not correctly encoded. You might just replace any space with a `+` char but there might be other problematic characters. To safely create a URL (or a URL string) you have to encode every part separately. In this case you might take everything after the last `/` and encode it as a path component, for example. – Sulthan Nov 02 '22 at 20:10

1 Answers1

0
let serverUrlStr = "smb://example.net/Data/OS/Home/test/untitled folder"
guard let formattedUrl = serverUrlStr.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed) else { return }
let sharePath = NSURL(string: formattedUrl)

Note that it's not generic. Read comments for more context.

tanmoy
  • 1,276
  • 1
  • 10
  • 28
  • Your solution will work for this specific URL, it's very bad as a generic solution. – Sulthan Nov 02 '22 at 20:08
  • Yes, I know. Other `CharacterSet` can handle other cases. – tanmoy Nov 02 '22 at 20:11
  • The allowed characters are not used the way you think they are. Actually, there is rarely a reason to use them at all. They definitely won't help with URL encoding. For example, `urlQueryAllowed` contains `?` and `&` which are *allowed* in a query but they must be encoded if they are present in a query parameter. Nevertheless, you cannot just blindly use percentage-encoding to the whole URL, every component must be encoded separately. – Sulthan Nov 02 '22 at 20:12
  • Can you please give an example of cases that won't work? Is all this is doing internally is `strWithSpace.replacingOccurrences(of: " ", with: "%20")`? Exactly what we see when we paste a URL with space on the browser tab. It might not be a working URL but the structure is right. – tanmoy Nov 02 '22 at 20:26
  • Well, one simple example is trying to encode an URL, that is actually valid (and properly encoded), e.g. `smb://example.net/Data/OS/Home/test/untitled%20folder` or if you try to encode an unencoded character that is actually valid to appear in query but not in query value, e.g. a `+`, full example: `smb://example.net/Data/OS/Home/test?phone=+2001234` - the `+` will be kept unencoded. In summary, URL encoding is context sensitive. You cannot safely encode the whole URL - you have to encode every component separately and you have to know whether it has been already encoded or not. – Sulthan Nov 02 '22 at 20:50
  • Thanks for the clarification. Well, I tested two of your test cases. `smb://example.net/Data/OS/Home/test/untitled%20folder` gets converted to `smb://example.net/Data/OS/Home/test/untitled%2520folder` which is faulty. And it is far from generic. – tanmoy Nov 02 '22 at 21:08
  • How do I know if it is already encoded or not? Should I check if it contains space or '/` or others? But for `smb://example.net/Data/OS/Home%20Folder/test/untitled folder` it has partial encoding in `Home%20Folder` and no encoding in `untitled folder`. Is other no other way than encoding every part separately for a generic solution? – tanmoy Nov 02 '22 at 21:16