-(void)viewWillAppear:(BOOL)animated{
UIDevice *device = [UIDevice currentDevice]; //Get the device object
[device beginGeneratingDeviceOrientationNotifications]; //Tell it to start monitoring the accelerometer for orientation
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter]; //Get the notification centre for the app
[nc addObserver:self selector:@selector(orientationChanged:) name:UIDeviceOrientationDidChangeNotification object:device];
}
-(void)viewWillDisappear:(BOOL)animated {
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
UIDevice *device = [UIDevice currentDevice]; //Get the device object
[nc removeObserver:self name:UIDeviceOrientationDidChangeNotification object:device];
}
- (void)orientationChanged:(NSNotification *)note {
UIDeviceOrientation status= [[UIDevice currentDevice] orientation];
switch (status) {
case UIDeviceOrientationPortrait: // Device oriented vertically, home button on the bottom
[self dismissViewControllerAnimated:YES completion:nil];
break;
case UIDeviceOrientationPortraitUpsideDown: // Device oriented vertically, home button on the top
break;
case UIDeviceOrientationLandscapeLeft: // Device oriented horizontally, home button on the right
;
break;
case UIDeviceOrientationLandscapeRight: // Device oriented horizontally, home button on the left
;
break;
default:
break;
}
}