问题
Say I have http://www.youtube.com
, http://youtube.com
, and https://www1.youtube.com/moretext
. How would I write a check to see if all of those URLs are from youtube.com?
I tried url.host
and it seems to keep the www.
and whatnot, which is not what I want.
I basically just want to be able to say:
if ([url isFromWebsite:@"youtube.com"]) {
// Do things.
}
Is this possible?
回答1:
Here’s an untested category on NSURL
that will provide the method you want.
@implementation NSURL (IsFromWebsite)
- (BOOL) isFromWebsite:(NSString *)domain
{
NSArray *selfHostComponents = [[self host] componentsSeparatedByString:@"."];
NSArray *targetHostComponents = [domain componentsSeparatedByString:@"."];
NSInteger selfComponentsCount = [selfHostComponents count];
NSInteger targetComponentsCount = [targetHostComponents count];
NSInteger offset = selfComponentsCount - targetComponentsCount;
if (offset < 0)
return NO;
for (NSUInteger i = offset; i < selfComponentsCount; i++) {
if (![selfHostComponents[i] isEqualToString:targetHostComponents[i - offset]])
return NO;
}
return YES;
}
@end
Edit: Another (also untested) way to do the same thing, as suggested by Jesse Rusak:
@implementation NSURL (IsFromWebsite)
- (BOOL) isFromWebsite:(NSString *)domain
{
NSArray *selfComponents = [[self host] componentsSeparatedByString:@"."];
NSArray *targetComponents = [domain componentsSeparatedByString:@"."];
NSInteger sizeDifference = [selfComponents count] - [targetComponents count];
if (sizeDifference < 0)
return NO;
return [[selfComponents subarrayWithRange:NSMakeRange(sizeDifference, [targetComponents count])]
isEqualToArray:targetComponents];
}
@end
回答2:
NSURL
— and indeed URLs in general — doesn't care too much about the actual content of the host portion of the URL; that's up to clients to interpret and handle. So it's up to you to perform your own handling of this info.
Here's a nice simple way:
- (BOOL)isYouTubeURL:(NSURL *)url;
{
NSString *host = url.host;
// Test the simple case of the domain being a direct match
// Case-insensitive, as domain names are defined to be
if ([host caseInsensitiveCompare:@"youtube.com"] == NSOrderedSame) {
return YES;
}
// And now the more complex case of a subdomain
// Look back from just the end of the string
NSStringCompareOptions options = NSBackwardsSearch|NSAnchoredSearch|NSCaseInsensitiveSearch;
NSRange range = [host rangeOfString:@".youtube.com" options:options];
return (range.location != NSNotFound);
}
You could optimise this if needed by searching only for the domain, and then seeing if the previous character doesn't exist (case #1 above), or has a .
before it (case #2 above). Going even further, CFURL
has API to tell you where in the raw string the host lies, and could search directly through that.
回答3:
In case anyone needs bdesham's answer in Swift 3.
extension URL {
func isFromWebsite(domain: String) -> Bool {
var selfHostComponents = self.host!.components(separatedBy: ".")
var targetHostComponents = domain.components(separatedBy: ".")
let selfComponentsCount = selfHostComponents.count
let targetComponentsCount = targetHostComponents.count
let offset = selfComponentsCount - targetComponentsCount
if offset < 0 {
return false
}
for i in offset..<selfComponentsCount {
if !(selfHostComponents[i] == targetHostComponents[i - offset]) {
return false
}
}
return true
}
}
回答4:
You can use the following code as NSString category
- (BOOL)isFromWebsite: (NSString *)website {
NSRange searchResult = [self rangeOfString:website];
if (searchResult.location != NSNotFound) {
return YES;
}
return NO;
}
You can use case insensitive compare also if that is a use case. Hope this helps.
来源:https://stackoverflow.com/questions/20430620/is-it-possible-to-just-get-the-domain-and-tld-from-an-nsurl-no-www-or-anything