Get device current orientation (App Extension)

2020-02-04 11:42发布

How to get device current orientation in an App Extension, I have tried below two methods but no success.

  1. It always return UIDeviceOrientationUnknown

    [[UIDevice currentDevice] orientation]
    
  2. It shows red message that ‘sharedApplication’ is not available on iOS (App Extension)

    [[UIApplication sharedApplication] statusBarOrientation];
    
  3. I also add an observer but it not getting called.

    [[NSNotificationCenter defaultCenter] addObserver:self.view selector:@selector(notification_OrientationWillChange:) name:UIApplicationWillChangeStatusBarOrientationNotification object:nil];
    
    
    
    - (void)notification_OrientationWillChange:(NSNotification*)n
    {
       UIInterfaceOrientation orientation = (UIInterfaceOrientation)[[n.userInfo objectForKey:UIApplicationStatusBarOrientationUserInfoKey] intValue];
    
        if (orientation == UIInterfaceOrientationLandscapeLeft)
           [self.textDocumentProxy insertText:@"Left"];
        if (orientation == UIInterfaceOrientationLandscapeRight)
           [self.textDocumentProxy insertText:@"Right"];
    }
    

So now how can anyone get current device orientation.

10条回答
手持菜刀,她持情操
2楼-- · 2020-02-04 12:20

I have done in my custom keyboard extension,Hope It will help you..

In order to update your custom keyboard when the orientation changes, override viewDidLayoutSubviews in the UIInputViewController. In another word, we can say that viewDidLayoutSubviews always called when rotation done.

In keyboard extension we're unable to use traditional flow as we usually used:

[UIApplication sharedApplication] statusBarOrientation]

So to detect current orientation, I used following code: In Objc :

if([UIScreen mainScreen].bounds.size.width < [UIScreen mainScreen].bounds.size.height){
// Portrait Orientation
}
else{
//Landscape Orientation
}

And in swift4, you can use this:

if UIScreen.main.bounds.size.width > UIScreen.main.bounds.size.height {
        //portrait Orientation
    }
    else
    {
        //landscape Orientation
    }
查看更多
Viruses.
3楼-- · 2020-02-04 12:23

This code won't give you the exact UIDeviceOrientation but you'll be able to know if it's in portrait or landscape mode

BOOL isLandScape = !(self.view.frame.size.width == ([[UIScreen mainScreen] bounds].size.width*([[UIScreen mainScreen] bounds].size.width<[[UIScreen mainScreen] bounds].size.height))+([[UIScreen mainScreen] bounds].size.height*([[UIScreen mainScreen] bounds].size.width>[[UIScreen mainScreen] bounds].size.height)));
查看更多
叛逆
4楼-- · 2020-02-04 12:27

I found a way where we can calculate our device orientation like this way in (App Extension)

- (void)viewDidLayoutSubviews
{
   if(self.view.frame.size.width > self.view.frame.size.height)
      NSLog(@"Landscape");
   else
      NSLog(@"Portrait");
}

It gives me right orientation but still not get as device is LandscapeLeft or LandscapeRight as well as Portrait or PortraitUpsideDown.

Still need help.

查看更多
女痞
5楼-- · 2020-02-04 12:29

In BroadcastExtension you can use sampleBuffer to understand orientation:

if let orientationAttachment =  CMGetAttachment(sampleBuffer, RPVideoSampleOrientationKey as CFString, nil) as? NSNumber 
{  
          let orientation = CGImagePropertyOrientation(rawValue: orientationAttachment.uint32Value)   
}
查看更多
啃猪蹄的小仙女
6楼-- · 2020-02-04 12:31

You could take a look at this library and its forks. It uses CoreMotion to detect the device orientation so it may work on App Extension's restricted environment.

查看更多
戒情不戒烟
7楼-- · 2020-02-04 12:33

I haven't been been able to get it to work in an iMessage App Extension. Apple seems to have silently disabled it as far as I can tell. https://forums.developer.apple.com/thread/53981

查看更多
登录 后发表回答