我正在编写使用NSURLS的可可应用程序 - 我需要删除URL的片段部分(#blah部分)。

例子: http://example.com/#blah 应该最终成为 http://example.com/

我在WebCore中发现了一些代码,这些代码似乎是通过使用CFURL功能来完成的,但是它从未找到URL中的片段部分。我已经将其封装在扩展类别中:

-(NSURL *)urlByRemovingComponent:(CFURLComponentType)component {
    CFRange fragRg = CFURLGetByteRangeForComponent((CFURLRef)self, component, NULL);
    // Check to see if a fragment exists before decomposing the URL.
    if (fragRg.location == kCFNotFound)
        return self;

    UInt8 *urlBytes, buffer[2048];
    CFIndex numBytes = CFURLGetBytes((CFURLRef)self, buffer, 2048);
    if (numBytes == -1) {
        numBytes = CFURLGetBytes((CFURLRef)self, NULL, 0);
        urlBytes = (UInt8 *)(malloc(numBytes));
        CFURLGetBytes((CFURLRef)self, urlBytes, numBytes);
    } else
        urlBytes = buffer;

    NSURL *result = (NSURL *)CFMakeCollectable(CFURLCreateWithBytes(NULL, urlBytes, fragRg.location - 1, kCFStringEncodingUTF8, NULL));
    if (!result)
        result = (NSURL *)CFMakeCollectable(CFURLCreateWithBytes(NULL, urlBytes, fragRg.location - 1, kCFStringEncodingISOLatin1, NULL));

    if (urlBytes != buffer) free(urlBytes);
    return result ? [result autorelease] : self;
}
-(NSURL *)urlByRemovingFragment {
    return [self urlByRemovingComponent:kCFURLComponentFragment];
}

这样可以使用:

NSURL *newUrl = [[NSURL URLWithString:@"http://example.com/#blah"] urlByRemovingFragment];

不幸的是,Newurl最终成为”http://example.com/#blah“因为urlbyremovingComponent中的第一行总是返回kcfnotfound

我很难过。有更好的方法吗?

工作代码,感谢NALL

-(NSURL *)urlByRemovingFragment {
    NSString *urlString = [self absoluteString];
    // Find that last component in the string from the end to make sure to get the last one
    NSRange fragmentRange = [urlString rangeOfString:@"#" options:NSBackwardsSearch];
    if (fragmentRange.location != NSNotFound) {
        // Chop the fragment.
        NSString* newURLString = [urlString substringToIndex:fragmentRange.location];
        return [NSURL URLWithString:newURLString];
    } else {
        return self;
    }
}
有帮助吗?

解决方案

这个怎么样:

NSString* s = @"http://www.somewhere.org/foo/bar.html/#label";
NSURL* u = [NSURL URLWithString:s];

// Get the last path component from the URL. This doesn't include
// any fragment.
NSString* lastComponent = [u lastPathComponent];

// Find that last component in the string from the end to make sure
// to get the last one
NSRange fragmentRange = [s rangeOfString:lastComponent
                                 options:NSBackwardsSearch];

// Chop the fragment.
NSString* newURLString = [s substringToIndex:fragmentRange.location + fragmentRange.length];

NSLog(@"%@", s);
NSLog(@"%@", newURLString);

其他提示

这是一个非常古老的问题,并且已经回答了,但是对于另一个简单的选择,这是我的做法:

 NSString* urlAsString = [myURL absoluteString];
 NSArray* components = [urlAsString componentsSeparatedByString:@"#"];
 NSURL* myURLminusFragment = [NSURL URLWithString: components[0]];

如果没有碎片,urlminusfragment将与myurl相同

Swift 3.0

这将消除片段

if let fragment = url.fragment{
  url = URL(string: url.absoluteString.replacingOccurrences(of: "#\(fragment)", with: "")!
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top