iOS 11 实现App在禁止转屏的状态下网页播放器全屏

本文介绍如何在iOS应用中仅允许系统内置全屏视频播放器转屏,同时保持其他界面为竖屏显示。通过重写UIViewController的相关方法,并在AppDelegate中进行配置,实现精确控制播放器界面的横屏展示。

禁止转屏是这个意思,在General中设置Device Orientation只有竖屏。

要点就是重写UIViewController的以下3个属性方法

系统的全屏视频播放器是AVFullScreenViewController,但并未暴露出任何的API,所以要在UIViewController的扩展中去修改

以下是UIViewController扩展中的实现方法,判断只有视频播放器才转屏

- (BOOL)shouldAutorotate {
    if ([self isKindOfClass:NSClassFromString(@"AVFullScreenViewController")]) {
        return YES;
    }
    
    return NO;
}

- (UIInterfaceOrientationMask)supportedInterfaceOrientations {
    if ([self isKindOfClass:NSClassFromString(@"AVFullScreenViewController")]) {
        return UIInterfaceOrientationMaskLandscapeRight;
    }

    return UIInterfaceOrientationMaskPortrait;
}

- (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation {
    if ([self isKindOfClass:NSClassFromString(@"AVFullScreenViewController")]) {
        return UIInterfaceOrientationLandscapeRight;
    }

    return UIInterfaceOrientationPortrait;
}

 

还没有结束,需要在AppDelegate中重写

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(nullable UIWindow *)window  NS_AVAILABLE_IOS(6_0) __TVOS_PROHIBITED;

这个系统方法。其实只需要支持竖屏和播放器横屏两种状态就够了,但是那样需要加判断,在转屏的时候返回不同值。所以这里直接用UIInterfaceOrientationMaskAll更为简洁。

- (UIInterfaceOrientationMask)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {
    return UIInterfaceOrientationMaskAll;
}

接下来就要在需要打开全屏播放的控制器中实现逻辑了。

添加BOOL变量判断是否加载完

@property (nonatomic,assign)BOOL didWebViewLoadOK;

初始化时加入下面两个通知

      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(begainFullScreen) name:UIWindowDidBecomeVisibleNotification object:nil];//进入全屏
      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(endFullScreen) name:UIWindowDidBecomeHiddenNotification object:nil];//退出全屏

别忘记移除

- (void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIWindowDidBecomeVisibleNotification object:nil];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:UIWindowDidBecomeHiddenNotification object:nil];
}

WebView的代理

- (void)webViewDidFinishLoad:(UIWebView *)webView {
    self.didWebViewLoadOK = YES;
}

- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error {
    self.didWebViewLoadOK = NO;
}

实现通知

#pragma mark - Notification
-(void)begainFullScreen
{
    if(!self.didWebViewLoadOK) {
        return;
    }
    
    [[UIDevice currentDevice] setValue:@"UIInterfaceOrientationLandscapeLeft" forKey:@"orientation"];

    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
        SEL selector = NSSelectorFromString(@"setOrientation:");
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
        [invocation setSelector:selector];
        [invocation setTarget:[UIDevice currentDevice]];
        int val = UIInterfaceOrientationLandscapeLeft;
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }

}

- (void)endFullScreen
{
    if ([[UIDevice currentDevice] respondsToSelector:@selector(setOrientation:)]) {
        SEL selector = NSSelectorFromString(@"setOrientation:");
        NSInvocation *invocation = [NSInvocation invocationWithMethodSignature:[UIDevice instanceMethodSignatureForSelector:selector]];
        [invocation setSelector:selector];
        [invocation setTarget:[UIDevice currentDevice]];
        int val =UIInterfaceOrientationPortrait;
        [invocation setArgument:&val atIndex:2];
        [invocation invoke];
    }
}

这样就完成了

 

转载于:https://www.cnblogs.com/xiaobaizhu/p/8081717.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值