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.
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);
                        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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With