12

What is the best way to detect a device's orientation in an application extension? I have had mixed results with solutions I've found on here:

How to detect Orientation Change in Custom Keyboard Extension in iOS 8?

Get device current orientation (App Extension)

I have looked at size classes and UITraitCollection and found that the device inaccurately reports that it is in portrait when it is in fact in landscape (not sure if this is OS bug, or I am not querying the right APIs the right way).

What is the best method for accomplishing:

  • The device's current orientation when the extension is first loaded
  • The orientation the device will rotate to
  • The orientation the device did rotate to

Thank you,

Community
  • 1
  • 1
barfoon
  • 27,481
  • 26
  • 92
  • 138

3 Answers3

20

I faced with that problem and looked through your examples too, but there wasn't a good solution of it. How I resolved it: I created a class that does some calculations of the UIScreen values and returns the self defined device orientation.

Class Header:

typedef NS_ENUM(NSInteger, InterfaceOrientationType) {
    InterfaceOrientationTypePortrait,
    InterfaceOrientationTypeLandscape
};

@interface InterfaceOrientation : NSObject

+ (InterfaceOrientationType)orientation;

@end

Implementation:

@implementation InterfaceOrientation

+ (InterfaceOrientationType)orientation{

    CGFloat scale = [UIScreen mainScreen].scale;
    CGSize nativeSize = [UIScreen mainScreen].currentMode.size;
    CGSize sizeInPoints = [UIScreen mainScreen].bounds.size;

    InterfaceOrientationType result;

    if(scale * sizeInPoints.width == nativeSize.width){
        result = InterfaceOrientationTypePortrait;
    }else{
        result = InterfaceOrientationTypeLandscape;
    }

    return result;
}

@end

I put it to viewWillLayoutSubviews or viewDidLayoutSubviews methods to catch the orientation changes event.

if([InterfaceOrientation orientation] == InterfaceOrientationTypePortrait){
     // portrait   
}else{
     // landscape   
}

In case you want to get an exact side of the device orientation (left, right, upside down) this approach won't resolve your problem. It just returns either the portrait or landscape orientations.

Hope it will help you.

  • I ran that code and it and it returned landscape when the device was actually portrait ? I'm still confused about that ios 8 orientation change . As far as i know the frame wont change but bounds did with orientation . Now is it just reveresed or ? – user519274 Nov 08 '14 at 01:18
  • user519274 - Thank you for the comment. It still works. Did you put the code into the right place? It should be in viewWillLayoutSubviews or viewDidLayoutSubviews methods of UIViewController. – Alexander Polovinka Nov 11 '14 at 10:20
  • But how to get device orientation is landScapeLeft or ladnScapeRight like this one http://stackoverflow.com/questions/25462091/get-device-current-orientation-app-extension – Anand Suthar Nov 14 '14 at 11:51
  • Anand K, I described it on my answer. The provided code can't calculate an exact side for the landscape or portrait mode. – Alexander Polovinka Nov 14 '14 at 16:17
1

You can get the device orientation in extension by adding a observer on UIApplicationWillChangeStatusBarOrientationNotification and then extracting the orientation as follows.

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(orientationWillChange:) name:UIApplicationWillChangeStatusBarOrientationNotification object:nil];
}

- (void)orientationWillChange:(NSNotification*)n
{
    UIInterfaceOrientation orientation = (UIInterfaceOrientation)[[n.userInfo objectForKey:UIApplicationStatusBarOrientationUserInfoKey] intValue];

    if (orientation == UIInterfaceOrientationLandscapeLeft)
        //handle accordingly
    else if (orientation == UIInterfaceOrientationLandscapeRight)
        //handle accordingly
    else if (orientation == UIInterfaceOrientationPortraitUpsideDown)
        //handle accordingly
    else if (orientation == UIInterfaceOrientationPortrait)
        //handle accordingly
}

Thanks

Omkar Guhilot
  • 1,712
  • 11
  • 17
0

My share extension targets iOS 14.0 or newer, and an answer provided by a different user suggests subscribing to the UIApplicationWillChangeStatusBarOrientationNotification notification, which is deprecated as of iOS 13.

The deprecation note in UIApplication.h suggests using viewWillTransitionToSize:withTransitionCoordinator: instead, which is working well for me. I've defined this message on my share extension's root view controller. I do have to deduce the orientation from the size that's provided, like so:

- (void)viewWillTransitionToSize:(CGSize)size withTransitionCoordinator:(id<UIViewControllerTransitionCoordinator>)coordinator
{
    [super viewWillTransitionToSize:size withTransitionCoordinator:coordinator];

    if (size.height > size.width) {
        // setup your UI for portrait orientation
    }
    else {
        // setup your UI for landscape orientation
    }
}