4

So I have a url like this:

let remoetURL = "http://xxx-test.img-cn-hangzhou.aliyuncs.com/materials/talk_-XXXXX-XXXXXXX/STEM RULE.pdf"

As you can see, at then end of the url, there is a white space, so I need to get rid of it to have a valid encoded url.

After doing some research, I realized I might use

let escapedString = remoteURL.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLPathAllowedCharacterSet())

But this does not returned the expected working url, because it encodes the ":" after "http" too

http%3A//xiaobandeng-staging.img-cn-hangzhou.aliyuncs.com/talk_materials/talk_-K4yjX4-238Ku74WVIJk/STEM%20RULE.pdf

I have also tried URLHostAllowedCharacterSet, but no luck. So I wonder if it is because I don't have www here, so it does not recognise which part is the host correctly. If so, what might be the elegant solution? I know I could replace white spaces with %20 by calling stringByReplacingOccurrencesOfString, but that seems just a bit fragile.

Thank you in advance.

Chenglu
  • 844
  • 1
  • 9
  • 17

2 Answers2

9

Try this used by SwiftyJSON

let urlString = remoteURL.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLQueryAllowedCharacterSet())

Swift 3:

let urlString = remoteURL.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed)

Michael J. Calkins
  • 32,082
  • 15
  • 62
  • 91
Dhaval Parmar
  • 18,812
  • 8
  • 82
  • 177
  • This is the right answer. Previously, I was having some misunderstanding towards those `NSCharacterSet ` methods. For `URLQueryAllowedCharacterSet `, I thought it only deals with the query part of a url, but it turned out to be dealing with the whole url conforming to a valid query format, which will parse spaces, but not slashes. – Chenglu Jun 20 '16 at 17:02
  • This is not really correct, for example this will escape the `#` anker (e.g. http://blub/index#hello). I suspect you have to first parse that invalid URL format coming in according to the rules you set, and then construct a proper URL from it. – hnh Apr 30 '19 at 12:40
0

Have you try this

let urlPath = NSString(format: remoetURL).stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding)!

For iOS 9

let encodedHost = NSString(format: remoetURL).stringByAddingPercentEncodingWithAllowedCharacters(.URLQueryAllowedCharacterSet())

Hope this will help you

Nirav D
  • 71,513
  • 12
  • 161
  • 183
  • Thank you! This seems to be the right answer. I actually saw this method before, but don't know why some said it is a deprecated method. But no warning for me, so I guess I should always try before I make a conclusion. – Chenglu Jun 18 '16 at 07:33
  • [stringByAddingPercentEscapesUsingEncoding](http://stackoverflow.com/questions/34015750/stringbyaddingpercentescapesusingencoding-was-deprecated-in-9-0-how-to-do-this) was deprecated in iOS 9. – Midhun MP Jun 18 '16 at 07:37
  • @MidhunMP, hmm, but there is no warning for me. Then what should I use in this case? Any suggestion? – Chenglu Jun 18 '16 at 07:40
  • @Chenglu: What is your minimum targeted iOS version and the base sdk version ? – Midhun MP Jun 18 '16 at 07:41
  • @MidhunMP, the targeted is 8.0, and the base is 9.3 – Chenglu Jun 18 '16 at 07:54
  • @Chenglu: Can you check the answer linked in my first comment, whether it is working for you or not ? – Midhun MP Jun 18 '16 at 08:39