【问题标题】:How to get the url domain name with objective-c如何用objective-c获取url域名
【发布时间】:2015-05-06 01:37:50
【问题描述】:

当我得到一个类似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

【问题讨论】:

  • 你也可以试试NSURL *url = [NSURL urlWithString:{YOUR_URL_STRING_HERE}]; NSString *hostname = [url host];
  • 等等,你想让h5.m.abc.com.cn给你abc.com吗?您需要明确说明您正在寻找什么行为。这不是简单的二级域名解析。
  • @BenZotto 你是对的,我的错。我犯了一个愚蠢的错误。应该是abc.com.cn 谢谢

标签: objective-c url domain-name


【解决方案1】:

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

  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);

【讨论】:

  • NSString *justDomain = [NSString stringWithFormat:@"%@.%@", parts[parts.count - 2], parts[parts.count - 1]];
猜你喜欢
  • 2011-04-04
  • 2010-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-10-30
  • 1970-01-01
相关资源
最近更新 更多