IODisplayCreateInfoDictionary() is deprecated, and needs some replacement

Originator:google
Number:rdar://15801217 Date Originated:2014-01-12
Status:Open Resolved:
Product:OS X Product Version:10.9
Classification: Reproducible:Always
 
Summary:
I can't find any supported way to get the localized name of a NSScreen.  Many apps need to display a list (e.g. popup menu) of available screens, to display different content on different screens.  We need a supported way to do this.

Prior to 10.9, there was an approach using IODisplayCreateInfoDictionary(), but that has been deprecated.  See below for a NSScreen category I've written to use it.  We need an approach that is supported on 10.9.

I would suggest adding a name or localizedName property to NSScreen as the ideal solution.


@implementation NSScreen (Dejal)

/**
 Read-only property to return the unique ID of the screen, aka the screen number.  Not the same as the screen list index.
 
 @returns The reciever's unique display ID.
 
 @author DJS 2014-01.
 */

- (NSUInteger)displayID;
{
    NSDictionary *screenDictionary = [self deviceDescription];
    NSNumber *screenID = [screenDictionary objectForKey:@"NSScreenNumber"];
    
    return [screenID unsignedIntegerValue];
}

/**
 Read-only property to return the localized name of the receiver.
 
 @returns The localized name of the screen, or nil if none is available.
 
 @author DJS 2014-01.
 */

- (NSString *)screenName;
{
    return [self screenNameForDisplayID:self.displayID];
}

/**
 Given a display ID, returns the localized screen name, or nil if none is available.
 
 @param displayID The unique ID of the screen, as returned by the displayID property.
 @returns The localized name of the screen, or nil if none is available.
 
 @author DJS 2014-01.
 */

// IODisplayCreateInfoDictionary() is deprecated as of 10.9; suppress the warning for this method, since there's no alternative:
#pragma clang diagnostic ignored "-Wdeprecated-declarations"

- (NSString *)screenNameForDisplayID:(NSUInteger)displayID;
{
    NSDictionary *deviceInfo = (NSDictionary *)CFBridgingRelease(IODisplayCreateInfoDictionary(CGDisplayIOServicePort((CGDirectDisplayID)displayID), kIODisplayOnlyPreferredName));
    NSDictionary *localizedNames = [deviceInfo objectForKey:[NSString stringWithUTF8String:kDisplayProductName]];
    
    return [[localizedNames allValues] firstObject];
}

// Restore deprecation warnings:
#pragma clang diagnostic warning "-Wdeprecated-declarations"

@end

Steps to Reproduce:
1. Try calling IODisplayCreateInfoDictionary().
2. Get deprecation warning.
3. Search for alternative.
4. Fail.
5. File bug report.


Expected Results:
It works without a warning, or there's some alternative.

Actual Results:
I can't find any alternative to using a deprecated function.  I am sad.

Version:
OS X 10.9.

Comments


Please note: Reports posted here will not necessarily be seen by Apple. All problems should be submitted at bugreport.apple.com before they are posted here. Please only post information for Radars that you have filed yourself, and please do not include Apple confidential information in your posts. Thank you!