Unsupported URL in NSURLRequest

这一生的挚爱 提交于 2019-12-02 20:07:53

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

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

In my case I fixed it with this :

strURL = [strURL stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

strURL contains the string with the URL.

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

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.

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.

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.

same answer as Alf G but with IOS 9+

strUrl = [strUrl stringByAddingPercentEncodingWithAllowedCharacters:NSCharacterSet.URLHostAllowedCharacterSet]

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.

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!