Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

URL host name returns null

I need to get the hostname of a string url

I am trying to create the NSURL object from the string and then using host property of NSURL to get the host name. But Host name is always null.

NSURL *url = [NSURL URLWithString: @"www.google.com"];
NSLog(@"Host name %@", url.host);

but the host name is always null. can anybody explain why is host name null?

Edit: I am sorry there was a typo i missed a quote and a comma while asking the question. But the problem is still the same.

like image 958
codemaster Avatar asked Oct 29 '12 22:10

codemaster


2 Answers

As per Apple documentation,

- (NSString *)host;

Return Value:

The host of the URL. If the receiver does not conform to RFC 1808, returns nil

So if it is not conforming to RFC 1808, it will return nil. As per definition of RFC 1808,

correct syntax is as follows :

<scheme>://<net_loc>/<path>;<params>?<query>#<fragment>

So you need to add http:// also while forming NSURL.

NSURL *url = [NSURL URLWithString:@"http://www.google.com"];
NSLog(@"Host name %@", url.host);
like image 108
iDev Avatar answered Sep 21 '22 01:09

iDev


Looks like your missing a " after www.google.com? Try adding it in.

NSURL *url = [NSURL URLWithString: @"www.google.com"];
NSLog(@"Host name %@", url.host);                  ^
                     ^

(Notice on the second line I also added a comma, I tried to point at it with a ^ best I could)

EDIT: Ah, okay then it may be the comma on the second line then.

like image 35
Dustin Avatar answered Sep 19 '22 01:09

Dustin