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 (i.e. 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 give开发者_运维百科n 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/
Maybe this snippet could be helpful:
-(BOOL) hasProximitySensor {
UIDevice *dev = [UIDevice currentDevice];
BOOL oldValue = [dev isProximityMonitoringEnabled];
[dev setProximityMonitoringEnabled:!oldValue];
BOOL newValue = [dev isProximityMonitoringEnabled];
[dev setProximityMonitoringEnabled:oldValue];
return (oldValue != newValue);
}
精彩评论