Check to see if a proposed file extension is valid/registered file extension on a system

StackOverflow https://stackoverflow.com/questions/19873838

  •  30-07-2022
  •  | 
  •  

سؤال

Is there an easy way (or any way) to see if a user typed file extension in a Save panel is a valid/registered extension/UTI on his system?

هل كانت مفيدة؟

المحلول

You can use the "Launch Services" function LSGetApplicationForInfo() to check if there is an application for opening items with the given extension. Example:

NSString *extension = @"txt";

BOOL extensionIsKnown;
CFURLRef app;
OSStatus status = LSGetApplicationForInfo(kLSUnknownType, kLSUnknownCreator,
                        (__bridge CFStringRef)(extension), kLSRolesAll,
                        NULL, &app);
if (status == noErr) {
    extensionIsKnown = YES;
    CFRelease(app);
} else {
    extensionIsKnown = NO;
}
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top