YosiFZ
YosiFZ

Reputation: 7900

Notification about iphone oriention

I want to get notification when a user is rotate the screen to landscape or portrait, it is possible?

I find couple of article but i didn't found answer for this.

Upvotes: 0

Views: 138

Answers (2)

mttrb
mttrb

Reputation: 8345

If you want to be notified when the device has been rotated you can either implement the shouldAutorotateToInterfaceOrientation: method in your view controller or you can register to receive the UIDeviceOrientationDidChangeNotification.

Before we start, the device orientation and the interface orientation can be different. The device may be landscape but the interface may remain portrait depending on how the app has been written. Device notifications are sent shortly before the interface orientation is changed to match the device orientation. If you don't want the interface orientation to change you should implement the shouldAutorotateToInterfaceOrientation: method in your view controller to return NO. This will stop the interface orientation being updated.

From your question it sounds like you want to receive notifications so I think you want to use the second method. You can enable UIDeviceOrientationChangeNotifications using:

[[UIDevice currentDevice] beginGeneratingDeviceOrientationNotifications];

There is a corresponding:

[[UIDevice currentDevice] endGeneratingDeviceOrientationNotifications];

You can register to receive the notifications in the normal way, using the NSNotificationCenter and registering to receive the UIDeviceOrientationDidChangeNotification:

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

Finally, you would implement the method to be called when the notification is received as follows:

- (void)orientationChanged:(NSNotication *)notification {

    UIDeviceOrientation = [[UIDevice currentDevice] orientation];

    if (orientation == UIDeviceOrientationPortrait || 
        orientation == UIDeviceOrientationPortraitUpsideDown) {

        // Portrait

    } else {
        // Landscape
    }

}

As you can see, the orientation can be accessed using the orientation instance method of UIDevice.

Upvotes: 3

Charan
Charan

Reputation: 4946

You need to add local notification in the landscape orientation in shouldAutorotateToInterfaceOrientation method

try like this:

if(interfaceOrientation == UIInterfaceOrientationLandscapeRight || UIInterfaceOrientationLandscapeLeft)

    {
         here schedule your local notification
    }

Upvotes: 1

Related Questions