How to get device current orientation in an App Extension, I have tried below two methods but no success.
It always return UIDeviceOrientationUnknown
<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)
}
I know it's late, but the mistake in this question was in this line:
[[NSNotificationCenter defaultCenter] addObserver:self.view selector:@selector(notification_OrientationWillChange:) name:UIApplicationWillChangeStatusBarOrientationNotification object:nil];
addObserver:self.view
is wrong, the notification should be attached on self
for being called. Work also on iOS8.
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
}
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