项目默认为竖屏,在iOS6下UIWebView嵌Vedio标签播放视频屏幕不旋转,iOS5没有问题。
一、版本判断宏
// iOS版本=== #define SYSTEM_VERSION_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedSame) #define SYSTEM_VERSION_GREATER_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedDescending) #define SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedAscending) #define SYSTEM_VERSION_LESS_THAN(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] == NSOrderedAscending) #define SYSTEM_VERSION_LESS_THAN_OR_EQUAL_TO(v) ([[[UIDevice currentDevice] systemVersion] compare:v options:NSNumericSearch] != NSOrderedDescending) // ===iOS版本
#pragma mark - Rotation // IOS5支持的屏幕方向 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } // IOS6开启旋转 - (BOOL)shouldAutorotate { return YES; } // IOS6支持的屏幕方向 - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskAllButUpsideDown; } // IOS6默认支持竖屏 - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationPortrait; }三、接着,为了解决iOS6下点击UIWebView中vedio标签,播放器可以不旋转的问题,需要注册播放器进入全屏和退出全屏的通知
if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6")) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(vedioStarted:) name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];// 播放器即将播放通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(vedioFinished:) name:@"UIMoviePlayerControllerDidExitFullscreenNotification" object:nil];// 播放器即将退出通知 }
- (void)vedioStarted:(NSNotification *)notification {// 播放器即将播放处理 [[UIDevice currentDevice] performSelector:@selector(setOrientation:) withObject:(id)UIDeviceOrientationLandscapeLeft];// iOS6只能选择一个横屏方向,暂时实现不了检测屏幕旋转而旋转 } - (void)vedioFinished:(NSNotification *)notification {// 视频 [[UIDevice currentDevice] performSelector:@selector(setOrientation:) withObject:(id)UIDeviceOrientationPortrait];// 屏幕方向回复默认的竖屏 }
- (void)dealloc { if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6")) { [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIMoviePlayerControllerDidExitFullscreenNotification" object:nil]; } }
完整代码:里面用到UIWebView开源浏览器SVWebViewController(下载地址:https://github.com/samvermette/SVWebViewController)
BLWebVC.h
#import <UIKit/UIKit.h> @class SVWebViewController; @interface BLWebVC : UINavigationController - (id)initWithAddress:(NSString*)urlString; - (id)initWithURL:(NSURL *)URL; @property (nonatomic, retain) UIColor *barsTintColor; @end
BLWebVC.m
// // BLWebVC.m // XXX // // Created by yhw on 13-3-29. // // #import "BLWebVC.h" #import "SVWebViewController.h" @interface BLWebVC () @property (nonatomic, retain) SVWebViewController *webViewController; @end @implementation BLWebVC - (void)dealloc { if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6")) { [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil]; [[NSNotificationCenter defaultCenter] removeObserver:self name:@"UIMoviePlayerControllerDidExitFullscreenNotification" object:nil]; } [_barsTintColor release]; // [_webViewController release]; [super dealloc]; } - (void)loadView { [super loadView]; // // self.view.backgroundColor = [UIColor whiteColor]; } - (void)viewDidLoad { if (SYSTEM_VERSION_GREATER_THAN_OR_EQUAL_TO(@"6")) { [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(vedioStarted:) name:@"UIMoviePlayerControllerDidEnterFullscreenNotification" object:nil];// 播放器即将播放通知 [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(vedioFinished:) name:@"UIMoviePlayerControllerDidExitFullscreenNotification" object:nil];// 播放器即将退出通知 } } - (void)vedioStarted:(NSNotification *)notification {// 播放器即将播放处理 [[UIDevice currentDevice] performSelector:@selector(setOrientation:) withObject:(id)UIDeviceOrientationLandscapeLeft];// iOS6只能选择一个横屏方向,暂时实现不了检测屏幕旋转而旋转 } - (void)vedioFinished:(NSNotification *)notification {// 视频 [[UIDevice currentDevice] performSelector:@selector(setOrientation:) withObject:(id)UIDeviceOrientationPortrait];// 屏幕方向回复默认的竖屏 } #pragma mark - Initialization - (id)initWithAddress:(NSString*)urlString { return [self initWithURL:[NSURL URLWithString:urlString]]; } - (id)initWithURL:(NSURL *)URL { self.webViewController = [[[SVWebViewController alloc] initWithURL:URL] autorelease]; if (self = [super initWithRootViewController:self.webViewController]) { self.webViewController.navigationItem.leftBarButtonItem = [[[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemDone target:webViewController action:@selector(doneButtonClicked:)] autorelease]; } return self; } #pragma mark - Setters - (void)setBarsTintColor:(UIColor *)barsTintColor { if (_barsTintColor != barsTintColor) { [_barsTintColor release]; _barsTintColor = [barsTintColor retain]; } self.navigationBar.tintColor = _barsTintColor; } #pragma mark - Rotation // IOS5支持的屏幕方向 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation { return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown); } // IOS6开启旋转 - (BOOL)shouldAutorotate { return YES; } // IOS6支持的屏幕方向 - (NSUInteger)supportedInterfaceOrientations { return UIInterfaceOrientationMaskAllButUpsideDown; } // IOS6默认支持竖屏 - (UIInterfaceOrientation)preferredInterfaceOrientationForPresentation { return UIInterfaceOrientationPortrait; } - (void)willRotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation duration:(NSTimeInterval)duration { [super willRotateToInterfaceOrientation:toInterfaceOrientation duration:duration]; } - (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { [super didRotateFromInterfaceOrientation:fromInterfaceOrientation]; } - (void)handleOrientationChangeNotification:(NSNotification *)notification { } @end