38

If I run this request from my terminal I can see the JSON requests as normally:

curl -XGET 192.168.0.6:8888/scripts/data/backend2/index.php/name/_all

My code for the NSURlRequest is this:

 NSURLRequest *request = [NSURLRequest requestWithURL:
                             [NSURL URLWithString:@"192.168.0.6:8888/scripts/data/backend2/index.php/name/_all"]];

    [[NSURLConnection alloc] initWithRequest:request delegate:self];

And I am getting this error:

didFailWithError
2013-11-29 22:31:08.164 Ski Greece[607:a0b] Connection failed: Error Domain=NSURLErrorDomain Code=-1002 "unsupported URL" UserInfo=0xcd042d0 {NSErrorFailingURLStringKey=192.168.0.6:8888/scripts/data/backend2/index.php/name/_all, NSErrorFailingURLKey=192.168.0.6:8888/scripts/data/backend2/index.php/name/_all, NSLocalizedDescription=unsupported URL, NSUnderlyingError=0xdbdcc70 "unsupported URL"}

How can I make the call to that URL? I cannot access the server code - I know it is just setup to return me what I need, if I call that URL?

ghostrider
  • 5,131
  • 14
  • 72
  • 120

11 Answers11

61

Try to include appropriate url scheme to your url, e.g.

[NSURL URLWithString:@"http://www...

Vizllx
  • 9,135
  • 1
  • 41
  • 79
  • 13
    I have a URL starting with http://... and I still get the error. Anybody knows what else could cause this? – 最白目 Feb 24 '15 at 09:31
  • @最白目 in my case the problem was with the end of the url. I doubt you still need answer for this but it may help someone in the future :) – i.bel Jan 20 '20 at 13:31
27

In my case I fixed it with this :

strURL = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

strURL contains the string with the URL.

Alf G
  • 271
  • 3
  • 2
11

In my case, I visit a service running on my own mac, so my url is 127.0.0.1:8080/list

After I add a http:// scheme. It works!

Now it is http://127.0.0.1:8080/list instead of 127.0.0.1:8080/list

halftrue
  • 149
  • 2
  • 7
9

I too struggled for the same error, even though the url path was correct but there was a space in it, before http, like below:

NSString *path = @" http://www.mylink/";
NSURL *url = [NSURL URLWithString:path];

so I was getting url as nil and so it was giving "unsupported URL". then by removing the space worked for me.

Keshav
  • 2,965
  • 3
  • 25
  • 30
3

same answer as Alf G but with IOS 9+

strUrl = [strUrl stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLHostAllowedCharacterSet]
2

In my case spaces are added to my URL. I have removed spaces and run. Please make sure you have not added any spaces to your URL even when you are passing parameters. Hope it helps someone.

Narasimha Nallamsetty
  • 1,215
  • 14
  • 16
2

In my case, I was passing an unwrapped optional in Swift. Once I unwrapped the optional to string, the URL was accepted correctly.

Justin Domnitz
  • 3,217
  • 27
  • 34
1

It seems its a malformed URL or it's not a valid url at all, try to hit this url in browser, I think you will not get any result. error code=-1002 occurs when the url is unsupported.

Sanchit Kumar Singh
  • 513
  • 1
  • 5
  • 17
0

Like stated before, a space in the URL can cause this but it's also possible that your string contains an unsupported character. For example, if you copy and paste a URL from a PDF, Word or other document it might contain unsupported characters. To the eye it looks fine but not the compiler.

To fix this, in your [NSURL URLWithString:@"http://blabhblabh"] method, delete the entire line of code, not just the url, and retype the link and method by hand.

ClintChil
  • 35
  • 6
0

This error can happen if your URL contains a trailing newline.

I ran into this when I was appending an API key read from a text file (to keep it out of the version control system) to the end of an URL, and the file included a training whitespace.

See https://stackoverflow.com/a/4645700/12484 for code that will trim newline characters out of a string.

Jon Schneider
  • 25,758
  • 23
  • 142
  • 170
0

I solved VIA below code

FinalUrl = [FinalUrl stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLQueryAllowedCharacterSet];

Thanks.

Aaban Tariq Murtaza
  • 1,155
  • 14
  • 16