iPhone接近传感器提供错误的数据

时间:2015-01-10 19:04:29

标签: ios objective-c iphone proximity proximitysensor

我会实现检查proximityState属性,该属性告诉我用户是否在他耳边有电话。就像他在打个电话一样。在iOS 7中工作,但是由于其他原因我不得不删除这个feauter。现在在iOS 8上我在应用程序中添加此功能,并且在接近将状态改为YES时,首次永久保留YES。即使从耳朵中取出设备,它也不会切换为NO。看起来它是ios中的一个错误,其他任何人都有同样的问题。

感谢。

2 个答案:

答案 0 :(得分:2)

我没有遇到此问题,以下代码适用于iOS 8.请记住,如果应用程序不支持纵向模式,则不会监控接近传感器。 我的申请代表:

#import "AppDelegate.h"

@interface AppDelegate ()
@end

@implementation AppDelegate
{
    UIDevice *currentDevice;
}

- (BOOL) application: (UIApplication*) application didFinishLaunchingWithOptions: (NSDictionary*) launchOptions
{
    NSLog(@"Application finished launching!");
    currentDevice = [UIDevice currentDevice];
    [currentDevice setProximityMonitoringEnabled:YES];
    if (currentDevice.proximityMonitoringEnabled)
    {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(onProximitySensorStateChanged:) name:UIDeviceProximityStateDidChangeNotification object:nil];
        NSLog(@"Monitoring proximity sensor!");
    }
    else
    {
        NSLog(@"Device does not have a proximity sensor!");
    }
    return YES;
}

- (void) onProximitySensorStateChanged: (NSNotification*) notification
{
    if (currentDevice.proximityState)
    {
        NSLog(@"User is now close to the proximity sensor!");
    }
    else
    {
        NSLog(@"User is now away from the proximity sensor!");
    }
}
@end

答案 1 :(得分:0)

所以我发现了问题。应该在主线程上读取self.device.proximityState。所有工作都是在后台威胁中完成的,如果我在主线程上检查了proximityState,它就能正常工作。

相关问题