IOS webView视频播放支持横屏

Safari会自动进入全局播放的情况

//禁止Safari自动全屏播放  webView.allowsInlineMediaPlayback = YES;

iOS 全局禁止横屏,但UIWebView 全屏播放视频,横屏,解决办法

UIWebview在播放网页视频的时候我们需要进行是否全屏状态的监听。

一般的需求是在播放视频时候需要横屏,退出全屏的时候不能横屏,但是UIWebview没有给出响应的方法

1,在APPDelegate.h文件中增加属性:是否支持横屏

/***  是否允许横屏的标记 */

@property (nonatomic,assign)BOOL allowRotation;

在APPDelegate.m文件中增加方法,控制全部不支持横屏

-(NSUInteger)application:(UIApplication *)application supportedInterfaceOrientationsForWindow:(UIWindow *)window {

if (self.allowRotation) {

return UIInterfaceOrientationMaskAll;

}

return UIInterfaceOrientationMaskPortrait;

}

2,在需要实现webView横屏的界面上添加通知

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

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

在退出全屏时,增加逻辑让其强制编程竖屏,这样当全屏播放的时候,点击down("完成")时,就会自动变成竖屏了。

// 进入全屏

-(void)begainFullScreen

{

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

appDelegate.allowRotation = YES;

}

// 退出全屏

-(void)endFullScreen

{

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

appDelegate.allowRotation = NO;

//强制归正:

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];

}

}

你可能感兴趣的:(IOS webView视频播放支持横屏)