Rimozione frammento di URL da NSURL
Domanda
Sto scrivendo un'applicazione Cocoa, che utilizza NSURLs -. Ho bisogno di rimuovere la parte frammento del URL (la parte #BLAH)
esempio: http://example.com/#blah dovrebbe finire come http://example.com/
Ho trovato un certo codice in WebCore che sembra farlo utilizzando la funzionalità CFURL, ma non trova mai la parte frammento nell'URL. Ho incapsulato in una categoria di estensione:
-(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];
}
Questo è usato come tale:
NSURL *newUrl = [[NSURL URLWithString:@"http://example.com/#blah"] urlByRemovingFragment];
purtroppo, newUrl finisce per essere " http://example.com/#blah ", perché il prima linea in urlByRemovingComponent restituisce sempre kCFNotFound
Sono perplesso. C'è un modo migliore di andare su questo?
Codice di lavoro, grazie alla 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;
}
}
Soluzione
Che ne dite di questo:
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);
Altri suggerimenti
Questa è piuttosto una vecchia questione, ed è già stato risposto, ma per un'altra opzione semplice è così che ho fatto:
NSString* urlAsString = [myURL absoluteString];
NSArray* components = [urlAsString componentsSeparatedByString:@"#"];
NSURL* myURLminusFragment = [NSURL URLWithString: components[0]];
se non c'è frammento, urlMinusFragment sarà la stessa come myURL
Swift 3.0
questo verrà rimosso il frammento
if let fragment = url.fragment{
url = URL(string: url.absoluteString.replacingOccurrences(of: "#\(fragment)", with: "")!
}