How can I detect if an iPhone OS device has a proximity sensor?

The docs related to proximity sensing state that if the proximity sensing APIs are used on a device without a proximity sensor (ie iPod touch, iPad) they will just return as if the proximity sensor has fired.

Aside from checking the [[UIDevice currentDevice].model] string and parsing for "iPhone", "iPod touch", or "iPad" is there a slicker way to determine if a proximity sensor is on a given device?


Taken from UIDevice documentation:

proximityMonitoringEnabled

A Boolean value indicating whether proximity monitoring is enabled (YES) or not (NO).

...

Discussion

....

Not all iPhone OS devices have proximity sensors. To determine if proximity monitoring is available, attempt to enable it. If the value of the proximityState property remains NO, proximity monitoring is not available.

Claus


Apple's documentation notes that “Not all iPhone OS devices have proximity sensors.” To determine if the device your app is running supports proximity monitoring, set the proximityMonitoringEnabled property to YES, then check its value:

UIDevice *device = [UIDevice currentDevice];
device.proximityMonitoringEnabled = YES;
if (device.proximityMonitoringEnabled == YES)
    // do something

Source: http://www.mobileorchard.com/new-in-iphone-30-tutorial-series-part-4-proximity-detection/


也许这个片段可能会有所帮助:

-(BOOL) hasProximitySensor {

    UIDevice *dev = [UIDevice currentDevice];
    BOOL oldValue = [dev isProximityMonitoringEnabled];
    [dev setProximityMonitoringEnabled:!oldValue];
    BOOL newValue = [dev isProximityMonitoringEnabled];

    [dev setProximityMonitoringEnabled:oldValue];

    return (oldValue != newValue);
}
链接地址: http://www.djcxy.com/p/50362.html

上一篇: 如何检测iphone4s

下一篇: 如何检测iPhone OS设备是否有接近传感器?