2009-11-05 14 views
6

Piszę aplikację Cocoa, która używa NSURL - muszę usunąć część fragmentu adresu URL (część #BLAH).Usuwanie fragmentu adresu URL z NSURL

przykład: http://example.com/#blah powinien skończyć jako http://example.com/

znalazłem jakiś kod w WebCore że wydaje się to zrobić za pomocą funkcji CFURL, ale to nigdy nie znajdzie się część fragmentu w adresie URL. Mam zamknięte go w kategorii rozszerzeń:

-(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]; 
} 

ten jest używany jako takie:

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

niestety NEWURL kończy się „http://example.com/#blah”, ponieważ pierwsza linia w urlByRemovingComponent zawsze zwraca kCFNotFound

Jestem zaskoczony. Czy jest lepszy sposób na robienie tego?

Kodeks Pracy, dzięki nWszystkie

-(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; 
    } 
} 

Odpowiedz

7

Jak o tym:

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

siebie. pozornie lastPathComponent zwraca fragment i jest metodą NSString. Wysłałem ostatni kod do pytania. – pixel

+0

NSURL ma również lastPathComponent, który nie zwraca fragmentu, ale jest to 10.6 lub więcej http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSURL_Class/Reference/Reference.html# // apple_ref/doc/uid/20000301-SW22 – nall

+0

co jest nie tak z url.fragment? – Sam

1

Jest to dość stary pytanie, i to już odpowiedział, ale na kolejną prostą opcją tym jak to zrobiłem:

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

jeśli istnieje ma fragment urlMinusFragment będzie taka sama jak myURL

+0

Ładne proste rozwiązanie, ale możesz przełączyć się na using components.firstObject, jeśli obsługuje go twoja wersja systemu iOS/OSX. Jest bezpieczniejszy niż jawny dostęp do komponentów [0], nawet jeśli componentsSeparatedByString: * powinien * zawsze zwracać niepustą tablicę. –

0

Swift 3,0

będzie wyjąć fragment

if let fragment = url.fragment{ 
    url = URL(string: url.absoluteString.replacingOccurrences(of: "#\(fragment)", with: "")! 
} 
Powiązane problemy