2010-04-06 133 views
2

与接近感应有关的文档指出,如果在没有接近感应传感器(例如iPod touch,iPad)的设备上使用接近感应API,它们就会像接近感应传感器已经启动一样返回。如何检测iPhone OS设备是否有接近传感器?

除了检查[[UIDevice currentDevice] .model]字符串并解析“iPhone”,“iPod touch”或“iPad”是否有一种确定接近传感器是否位于给定设备上的方式?

回答

2

苹果的文件指出,“并不是所有的iPhone OS设备有接近传感器。”要确定您的应用程序正在运行的设备支持近距离监控,设置proximityMonitoringEnabled属性为YES,然后检查其值:

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

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

+0

的医生说:如果要判断接近监测可用,尝试启用它。如果{'proximityState'属性的值保持为NO,则接近度监控不可用。但是,真的,如果您在传感器前面没有任何东西启用proximityState,我不明白这一点...... – Oliver 2012-01-30 14:45:13

+0

与直接相关,但绝不会与“是”进行比较。只需在布尔上下文中评估它,就像'if(device.proximityMonitoringEnabled)' – ipmcc 2016-02-09 14:23:49

3

从文档的UIDevice摘自:

proximityMonitoringEnable d

一个布尔值,指示是否启用 接近监测(是) (NO)。

...

讨论

....

并非所有iPhone OS设备有 接近传感器。要确定 接近监测是否可用, 会尝试启用它。如果值 proximityState属性保持为 否,则近似监视不是 可用。

克劳斯

0

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

-(BOOL) hasProximitySensor { 

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

    [dev setProximityMonitoringEnabled:oldValue]; 

    return (oldValue != newValue); 
} 
相关问题