0
if let url = URL(string: "https://omsoftware.org/sorora/public/profile_images/kapil borkar_199.jpeg"){} 

is always getting fail when the file extension is .jpeg . i have tried with .png it works fine only . URL(string:) is not giving url object when extension is .jpeg. please help.

PGDev
  • 23,751
  • 6
  • 34
  • 88
  • What do you mean by "it fails"? Maybe try `https://omsoftware.org/sorora/public/profile_images/kapil%20borkar_199.jpeg`? Note that I replaced the space with a `%20`. – Sweeper Oct 16 '19 at 07:34

2 Answers2

3

You need to encode the urlString to handle the whitespaces. Use addingPercentEncoding(withAllowedCharacters:) method on the urlString, i.e.

let str = "https://omsoftware.org/sorora/public/profile_images/kapil borkar_199.jpeg"

if let urlString = str.addingPercentEncoding(withAllowedCharacters: .urlQueryAllowed), let url = URL(string: urlString) {
    //add your code here...
}

addingPercentEncoding(withAllowedCharacters:)

Returns a new string made from the receiver by replacing all characters not in the specified set with percent-encoded characters.

Refer this to know more about addingPercentEncoding(withAllowedCharacters:) method.

PGDev
  • 23,751
  • 6
  • 34
  • 88
0

As you can see there is whitespace in your url so you can use like

let urlStr = "your Url Strting".replacingOccurrences(of: " ", with: "%20")
if let url = URL(string: urlStr){}
golu_kumar
  • 231
  • 2
  • 9
  • This is a very specific solution for this problem which works, but will haunt you back later. The best method is just to encode the url. Please read https://stackoverflow.com/a/58408248/3999808 – Zun Oct 16 '19 at 08:14