0

我将实施检查proximityState 属性,它告诉我用户是否在他的耳朵附近有手机。就像他在打电话一样。在 iOS 7 中工作,但后来由于其他原因我不得不删除这个 fauter。现在在 iOS 8 上,我在应用程序中添加了这个功能,并且在接近度后第一次将其状态更改为 YES,永远保持 YES。即使您将设备从耳朵中取出,它也不会切换到 NO。好像是ios的bug,有没有人遇到过同样的问题。

谢谢。

4

2 回答 2

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
于 2015-01-10T21:01:42.007 回答
0

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

于 2015-02-24T15:21:11.103 回答