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