如何用objective-c获取url域名

How to get the url domain name with objective-c

当我得到一个 url 喜欢的 http://h5.m.abc.com/awp/base/cart.htm?#!/awp/base/cart.htm 时,我只需要 abc.com,我该怎么办?

编辑:无论 url 如何变化,我只需要 abc.com,例如:http://h5.m.abc.com.cn/awp/base/cart.htm?#!/awp/base/cart.htm 我只需要 abc.com.cn http://a.b.h.m.abc.cn/awp/base/cart.htm?#!/awp/base/cart.htm我只需要abc.cn.

我认为这不是最好的方法,但会奏效。

  NSString *host = [[NSURL URLWithString:@"http://h5.m.abc.com/awp/base/cart.htm?#!/awp/base/cart.htm"] host];

  NSArray  *parts = [host componentsSeparatedByString:@"."];

  NSString *justDomain = [NSString stringWithFormat:@"%@.%@",
                      parts[parts.count - 2], parts[parts.count - 1]];

  NSLog(@"just domain %@", justDomain);