ios - Check if a file extension is valid -
i'm trying file extension using [string pathextension]
not return file extension per se. eg: have file named 'example.png', when use method 'png' correct. let's have file no extension, 'example', nil
still correct. happens when have file 'example.109'? '109' extension incorrect. happens '.109' part of filename itself. there way validate this?
i take meaning of valid filename extension filename extension there application present on system claims/declares extension.
you can determine using following code:
nsstring *pathextension = [@"example.109" pathextension]; nslog(@"pathextension == %@", pathextension); cfstringref utitype = uttypecreatepreferredidentifierfortag( kuttagclassfilenameextension, (__bridge cfstringref)pathextension, null); nslog(@"utitype == %@", utitype); cfdictionaryref declaration = uttypecopydeclaration(utitype); nslog(@"declaration == %@", declaration); // print (null) cfstringref jpgutitype = uttypecreatepreferredidentifierfortag( kuttagclassfilenameextension, cfstr("jpg"), null); nslog(@"jpg's uti type == %@", jpgutitype); cfdictionaryref knowndeclaration = uttypecopydeclaration(jpgutitype); nslog(@"knowndeclaration == %@", knowndeclaration); if (utitype) cfrelease(utitype); if (knownfilenameextensionsutitype) cfrelease(knownfilenameextensionsutitype); if (declaration) cfrelease(declaration); if (knowndeclaration) cfrelease(knowndeclaration);
that print following console:
pathextension == 109 utitype == dyn.age8xcqb3 declaration == (null) jpg's uti type == public.jpeg knowndeclaration == { uttypeconformsto = "public.image"; uttypedescription = "jpeg image"; uttypeidentifier = "public.jpeg"; uttypetagspecification = { "com.apple.ostype" = jpeg; "public.filename-extension" = ( jpeg, jpg, jpe ); "public.mime-type" = ( "image/jpeg", "image/jpg" ); }; }
as can see printed results, call uttypecopydeclaration()
uti type associated .109
filename extension returned null
, means there no application declares filename extension. compare results returned uttypecopydeclaration()
call uti type associated .jpg
filename extension. there, call returns valid dictionary contains information, since there application (or system itself) declares filename extension.
Comments
Post a Comment