How to get device current orientation in an App Extension, I have tried below two methods but no success.
It always return UIDeviceOrientationUnknown
[[UIDevice currentDevice] orientation]
It shows red message that ‘sharedApplication’ is not available on iOS (App Extension)
[[UIApplication sharedApplication] statusBarOrientation];
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.
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:
So to detect current orientation, I used following code: In Objc :
And in swift4, you can use this:
This code won't give you the exact UIDeviceOrientation but you'll be able to know if it's in portrait or landscape mode
I found a way where we can calculate our device orientation like this way in (App Extension)
It gives me right orientation but still not get as device is LandscapeLeft or LandscapeRight as well as Portrait or PortraitUpsideDown.
Still need help.
In BroadcastExtension you can use sampleBuffer to understand orientation:
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.
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