Search code examples
iphoneobjective-cnsurl

Trim any illegal characters from NSURL before writing to disk


I would like to write a string to an NSURL which may contain characters such as / or :, which are not supported by the file system. Is there any convenience method to trim these characters? Or maybe a reference containing all illegal characters, so that I can write such a method myself?


Solution

  • May be it's better just to escape them with something like this:

    NSString *unescaped = @"http://www";
    NSString *escapedString = (NSString *)CFURLCreateStringByAddingPercentEscapes(
        NULL,
       (CFStringRef)unescaped,
        NULL,
       (CFStringRef)@"!*'();:@&=+$,/?%#[]",
        kCFStringEncodingUTF8);
    
    NSLog(@"escapedString: %@",escapedString);
    

    as it stated here?

    Or you can just strip ! * ' ( ) ; : @ & = + $ , / ? % # [ ] characters, if you wish so. Look here for a list of reserved characters.