EMCDDeviceManager+ProximitySensor.m 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /************************************************************
  2. * * Hyphenate CONFIDENTIAL
  3. * __________________
  4. * Copyright (C) 2016 Hyphenate Inc. All rights reserved.
  5. *
  6. * NOTICE: All information contained herein is, and remains
  7. * the property of Hyphenate Inc.
  8. * Dissemination of this information or reproduction of this material
  9. * is strictly forbidden unless prior written permission is obtained
  10. * from Hyphenate Inc.
  11. */
  12. #import <UIKit/UIKit.h>
  13. #import "EMCDDeviceManager+ProximitySensor.h"
  14. @implementation EMCDDeviceManager (ProximitySensor)
  15. @dynamic isSupportProximitySensor;
  16. @dynamic isCloseToUser;
  17. #pragma mark - proximity sensor
  18. - (BOOL)isProximitySensorEnabled {
  19. BOOL ret = NO;
  20. ret = self.isSupportProximitySensor && [UIDevice currentDevice].proximityMonitoringEnabled;
  21. return ret;
  22. }
  23. - (BOOL)enableProximitySensor {
  24. BOOL ret = NO;
  25. if (_isSupportProximitySensor) {
  26. [[UIDevice currentDevice] setProximityMonitoringEnabled:YES];
  27. ret = YES;
  28. }
  29. return ret;
  30. }
  31. - (BOOL)disableProximitySensor {
  32. BOOL ret = NO;
  33. if (_isSupportProximitySensor) {
  34. [[UIDevice currentDevice] setProximityMonitoringEnabled:NO];
  35. _isCloseToUser = NO;
  36. ret = YES;
  37. }
  38. return ret;
  39. }
  40. - (void)sensorStateChanged:(NSNotification *)notification {
  41. BOOL ret = NO;
  42. if ([[UIDevice currentDevice] proximityState] == YES) {
  43. ret = YES;
  44. }
  45. _isCloseToUser = ret;
  46. if([self.delegate respondsToSelector:@selector(proximitySensorChanged:)]){
  47. [self.delegate proximitySensorChanged:_isCloseToUser];
  48. }
  49. }
  50. #pragma mark - getter
  51. - (BOOL)isCloseToUser {
  52. return _isCloseToUser;
  53. }
  54. - (BOOL)isSupportProximitySensor {
  55. return _isSupportProximitySensor;
  56. }
  57. @end