I have an application for both ipad iphones, ipad users always landscape mode is enabled and portrait for iphone users, now what I am trying to achieve is in the iphone application I play a video using AVPlayerViewController but since the application is locked in portrait inside the appdelegate, when I press the full screen button on the player it will just stay opn portrait mode I want to make it landscape I tried all the answers I found in stackoverflow but no luck any idea on how to make it work ?
Asked
Active
Viewed 579 times
0
-
is my answer is working for you ? – Darshan Kunjadiya Sep 24 '16 at 11:17
-
No it didnt. thanks anyways. – MOHi91 Sep 26 '16 at 06:14
2 Answers
0
Add below code in your AppDelegate.m
file.
Objective C
#if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_9_0
#define supportedInterfaceOrientationsReturnType NSUInteger
#else
#define supportedInterfaceOrientationsReturnType UIInterfaceOrientationMask
#endif
- (supportedInterfaceOrientationsReturnType)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)windowx
{
if ([[self.window.rootViewController presentedViewController] isKindOfClass:[AVPlayerViewController class]]
)
{
if ([self.window.rootViewController presentedViewController].isBeingDismissed)
{
return UIInterfaceOrientationMaskPortrait;
}else{
return UIInterfaceOrientationMaskAll;
}
}
}
Swift
#if __IPHONE_OS_VERSION_MAX_ALLOWED < __IPHONE_9_0
let supportedInterfaceOrientationsReturnType = NSUInteger
#else
let supportedInterfaceOrientationsReturnType = .mask
#endif
func application(_ application: UIApplication, supportedInterfaceOrientationsFor windowx: UIWindow) -> supportedInterfaceOrientationsReturnType {
if (self.window!.rootViewController!.presented! is AVPlayerViewController) {
if self.window!.rootViewController!.presented!.isBeingDismissed() {
return .portrait
}
else {
return .all
}
}
}

Darshan Kunjadiya
- 3,323
- 1
- 29
- 31
0
I solved this problem with an NSTimer I made a Method that will be called every second I know not good practice but this is what I did:
[NSTimer scheduledTimerWithTimeInterval:1.0f target:self selector:@selector(yourMethod) userInfo:nil repeats:YES];
-(void)yourMethod{
if(!_fullscreen){
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
}else{
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
} }
the _fullscreen is a flag that will change when the AVplayer goes to fullscreen view or normal view.
and to track the AVplayer states I used an Observer @"bounds" to check what size the AVPlayer screen is.
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *, id> *)change context:(void *)context {
if (object == self.playerViewController.contentOverlayView) {
if ([keyPath isEqualToString:@"bounds"]) {
CGRect oldBounds = [change[NSKeyValueChangeOldKey] CGRectValue], newBounds = [change[NSKeyValueChangeNewKey] CGRectValue];
BOOL wasFullscreen = CGRectEqualToRect(oldBounds, [UIScreen mainScreen].bounds), isFullscreen = CGRectEqualToRect(newBounds, [UIScreen mainScreen].bounds);
if (isFullscreen && !wasFullscreen) {
if (CGRectEqualToRect(oldBounds, CGRectMake(0, 0, newBounds.size.height, newBounds.size.width))) {
NSLog(@"rotated fullscreen");
}
else {
NSLog(@"entered fullscreen");
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
}else{
_fullscreen = YES;
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationLandscapeRight];
}
}
}
else if (!isFullscreen && wasFullscreen) {
NSLog(@"exited fullscreen");
if(UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
}else{
_fullscreen = NO;
[[UIDevice currentDevice] setOrientation:UIInterfaceOrientationPortrait];
}
}
}
}}

MOHi91
- 65
- 7