8
votes

Suppression de fragment d'URL de Nsurl

J'écris une application de cacao, qui utilise Nsurls - je dois retirer la partie de fragment de l'URL (la partie #blah).

Exemple: http://example.com/#blah doit finir par http: //example.com/ p>

J'ai trouvé du code dans un webcore qui semble le faire en utilisant la fonctionnalité CFURL, mais elle ne trouve jamais la partie de fragment dans l'URL. J'ai encapsulé dans une catégorie d'extension: p> xxx pré>

Ceci est utilisé comme tel: p> xxx pré>

Malheureusement, Newurl se termine Up étant " http://example.com/#blah " car la première ligne d'UrlbyRemovingComponent retourne toujours kcfnotfound

Je suis exclu. Y a-t-il une meilleure façon d'aller à ce sujet? P>

Code de travail, grâce à Nall STROR> P>

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


0 commentaires

3 Réponses :


8
votes

Que diriez-vous:

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


3 commentaires

proche. Apparemment, DatePathComponent renvoie le fragment et est une méthode Nstring. J'ai posté le code final à la question.


Nsurl a également un salepomponant qui ne renvoie pas le fragment, mais c'est 10.6+ développeur.apple.com/mac/library/documentation/cocoa/referenc E / ...


Que se passe-t-il si le dernier composant chemin est dans le fragment (par exemple, ces URL de fragment riche ont des applications Web)?



2
votes

C'est une question assez ancienne et il a déjà été répondu, mais pour une autre option simple, c'est comment je l'ai fait:

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


1 commentaires

Belle solution simple, mais vous voudrez peut-être passer à l'utilisation de composants.FirstObject si votre version iOS / OSX la prend en charge. Son plus sûr que d'accéder explicitement à des composants [0], même si les composantsÉquitésBystring: devraient toujours renvoyer un tableau non vide.



0
votes

Swift 3.0

Ceci éliminera le fragment xxx


0 commentaires