What characters are allowed in a iOS file name?

后端 未结 9 2134
长情又很酷
长情又很酷 2020-12-29 05:20

I\'m looking for a way to make sure a string can be used as a file name under iOS. I\'m currently in the section of the code that deletes incompatible characters. I\'m wonde

9条回答
  •  孤独总比滥情好
    2020-12-29 05:51

    I've had to save remote files locally with filenames containing other characters than basic alpha-numeric characters. I use the method below to strip out potential invalid characters, ensuring it's a valid filename for the filesystem when generating a NSURL using URLWithString:

        filename = [[filename componentsSeparatedByCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]] componentsJoinedByString:@"" ];
        filename = [[filename componentsSeparatedByCharactersInSet:[NSCharacterSet illegalCharacterSet]] componentsJoinedByString:@"" ];
        filename = [[filename componentsSeparatedByCharactersInSet:[NSCharacterSet symbolCharacterSet]] componentsJoinedByString:@"" ];
        fileURLString = [NSTemporaryDirectory() stringByAppendingPathComponent:filename];
        fileURL = [NSURL URLWithString:fileURLString];
    

    You may also want to test for collision errors first using:

        [[NSFileManager defaultManager] fileExistsAtPath:[fileURL absoluteString]]
    

提交回复
热议问题