Search code examples
iphonepathnsurl

Remove ".." from string representation of URL


When you have a filesystem path, you can have a ".." removed (and the previous path component removed as well) by using the stringByResolvingSymlinksInPath selector. How can I achieve the same thing for a URL? For example I start out with, say:

www.example.com/themes/themeA/../common/assetA.png

Which I need converted to:

www.example.com/themes/common/assetA.png

Solution

  • For a URL use the NSURL method:

    - (NSURL *)standardizedURL
    

    Returns a new URL that points to the same resource as the original URL and is an absolute path.

    Example:

    NSString *s  = @"www.example.com/themes/themeA/../common/assetA.png";
    NSURL    *u  = [NSURL URLWithString:s];
    NSURL    *su = [u standardizedURL];
    NSLog(@"su: %@", su);
    

    NSLog output:

    su: www.example.com/themes/common/assetA.png