Rox Rox - 5 months ago 11
Objective-C Question

UIDeviceOrientationFaceDown in iphone

I want to detect UIDeviceOrientationFaceDown. How can i do this???
Actually i want to perform some action when my iphone face is down on flat surface.
how is it possible??? plz help me to do this.

i have this code, but don't know where and how apply this code

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

BOOL getOrientationUpdates = [[UIDevice currentDevice] isGeneratingDeviceOrientationNotifications];
NSLog(@"will receive orientation notifications: %@", getOrientationUpdates?@"YES":@"NO");

[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(orientationChanged:)
name:UIDeviceOrientationDidChangeNotification
object:nil];


if there is any tutorial then please suggest me that

Thanks in Advance and most welcome to your precious help and suggestions.

Answer

dont you think you can accomplish this by detecting the ProximityState of iPhone? use the [UIDevice currentDevice] singleton, proximityMonitoringEnabled to YES. you can then access the proximity information through the proximityState property.

[[UIDevice currentDevice]proximityState];

iPhone has a sensor turns off the screen when you put it on your ear during a call, AFAIK that is an infrared sensor. and you can access it.

EDIT: You can also accomplish it using the below code. UIDeviceOrientationFaceDown The device is held parallel to the ground with the screen facing downwards. (regardless if it touched any object) if you want to know if the iPhone touched an object, detect the proximity state of device.

 [[NSNotificationCenter defaultCenter] removeObserver:self];
 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(detectOrientation) name:@"UIDeviceOrientationDidChangeNotification" object:nil];


    -(void)detectOrientation;{

            switch ([[UIDevice currentDevice] orientation]) {
                case UIDeviceOrientationPortrait:
                {
                    NSLog(@"portrait");
                }
                    break;
                case UIDeviceOrientationPortraitUpsideDown:
                {
                    NSLog(@"portraitUpSideDown");
                }
                    break;
                case UIDeviceOrientationLandscapeLeft:
                {
                    NSLog(@"landscapeLeft");
                }
                    break;
                case UIDeviceOrientationLandscapeRight:
                {
                    NSLog(@"landscapeRight");
                }
                break;
               case UIDeviceOrientationFaceDown:
               {
                    NSLog(@"facedown!!");
                }
                 break;

            default:
                break;
        }
    } 

}

EDIT: to answer the question in comment. add this line in your viewDidLoad

[UIDevice currentDevice].proximityMonitoringEnabled = YES;
[[NSNotificationCenter defaultCenter] removeObserver:self];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleProximityChangeNotification:) name:UIDeviceProximityStateDidChangeNotification object:nil];

then write a method

-(void)handleProximityChangeNotification{
     if([[UIDevice currentDevice]proximityState]){
        NSLog(@"...");
    }
}