thieumao

Proximity Sensor iOS

Mar 26th, 2017
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import "ViewController.h"
  2.  
  3. @interface ViewController ()
  4.  
  5. @end
  6.  
  7. @implementation ViewController
  8.  
  9. - (void)viewDidLoad {
  10.     [super viewDidLoad];
  11.     UIDevice *device = [UIDevice currentDevice];
  12.     device.proximityMonitoringEnabled = YES;
  13.    
  14.     // Proximity Sensor Notification
  15.     [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(proximityChanged:) name:@"UIDeviceProximityStateDidChangeNotification" object:device];
  16. }
  17.  
  18. - (void)proximityChanged:(NSNotification *)notification {
  19.     UIDevice *device = [notification object];
  20.     if (device.proximityState == 1) {
  21. //        [audioSession setCategory:AVAudioSessionCategoryPlayAndRecord error:nil];
  22.         NSLog(@"Vao roi");
  23.     } else {
  24.         NSLog(@"Cung Vao roi");
  25. //        [audioSession setCategory:AVAudioSessionCategoryPlayback error:nil];
  26.     }
  27. }
  28.  
  29. @end
Advertisement
Add Comment
Please, Sign In to add comment