热更新demo

hotController.m 15KB

    // // hotController.m // ThePaperBase // // Created by 李元元 on 16/5/11. // Copyright © 2016年 scar1900. All rights reserved. // #import "hotController.h" #import "linkController.h" #define NAVBAR_HEIGHT 63 @interface hotController ()<UIWebViewDelegate,imageScanerDelegate,TPMovieContentDelegate> { CGFloat webHeight; CGFloat videoBtnHeight; BOOL isWebViewLoadFinish; } @property(nonatomic, strong)UIButton *videoBtn; @property(nonatomic, strong)UIButton *pushBtn; @property(nonatomic, strong)linkController *linkVC; @end @implementation hotController - (void)viewDidLoad { [super viewDidLoad]; self.view.backgroundColor = [UIColor colorWithHexString:ContentDetailBack]; isWebViewLoadFinish = NO; if (IS_IPHONE4) { videoBtnHeight = 200; }else if(IS_IPHONE5){ videoBtnHeight = 200; }else if(IS_IPHONE_6){ videoBtnHeight = 200 * 750/640; }else if (IS_IPHONE_6P){ videoBtnHeight = 200 * 1242/640; videoBtnHeight = videoBtnHeight *2/3; }else{ videoBtnHeight = 200; } self.videoBtn.frame = CGRectMake(0, 0, rect_screen.size.width, videoBtnHeight); self.webView.frame = CGRectMake(0, NAVBAR_HEIGHT, rect_screen.size.width, rect_screen.size.height); [self.view addSubview:self.webView]; [self initJSbirdge]; [self.webView.scrollView addSubview:self.videoBtn]; [self.webView.scrollView addSubview:self.pushBtn]; //http://101.231.211.82:8083/demo // NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://101.231.211.82:8083/demo"]]; NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://101.231.211.82:8084/ftp/dev/demo/index.html"]]; // NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://101.231.211.82:8084/ftp/dev/demo/index.html"] cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:20]; [self.webView loadRequest:request]; [_bridge send:[NSString stringWithFormat:@"deviceResolution,%@",[TPUserDefault instance].deviceResolution]]; // Do any additional setup after loading the view. } #pragma mark -- view init -(UIButton *)videoBtn{ if (!_videoBtn) { _videoBtn = [UIButton buttonWithType:UIButtonTypeCustom]; _videoBtn.backgroundColor = [UIColor blackColor]; [_videoBtn addTarget:self action:@selector(gotoMovie:) forControlEvents:UIControlEventTouchUpInside]; } return _videoBtn; } //-(TPMovieContent *)moviePlayerController{ // if (!_moviePlayerController) { // NSMutableDictionary *urlDic = [NSMutableDictionary dictionary]; // [urlDic setObject:@"http://video.thepaper.cn/video/0/9/139.mp4" forKey:@"standard"]; // [urlDic setObject:@"http://video.thepaper.cn/video/0/9/140.mp4" forKey:@"high"]; // CGFloat videoHeight = 338/2*(rect_screen.size.width-2*10)/300; // CGRect videoRect = CGRectMake(0, NAVBAR_HEIGHT, CGRectGetWidth(self.view.bounds), videoHeight); // _moviePlayerController = [[TPMovieContent alloc] initWithFrame:videoRect contentUrlDic:urlDic]; // // [KEY_WINDOW addSubview:_moviePlayerController.view]; // } // return _moviePlayerController; //} - (UIWebView*)webView { if (!_webView) { _webView = [[UIWebView alloc] init]; _webView.backgroundColor = [UIColor colorWithHexString:ContentDetailBack]; _webView.scalesPageToFit = NO; _webView.delegate = self; _webView.scrollView.decelerationRate = 1.0f; } return _webView; } -(UIButton *)pushBtn{ if (!_pushBtn) { _pushBtn = [UIButton buttonWithType:UIButtonTypeCustom]; [_pushBtn setTitle:@"跳转页面" forState:UIControlStateNormal]; _pushBtn.titleLabel.font = appFont(20,NO); [_pushBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal]; [_pushBtn addTarget:self action:@selector(gotoHot:) forControlEvents:UIControlEventTouchUpInside]; } return _pushBtn; } - (void)initJSbirdge { _bridge = [WebViewJavascriptBridge bridgeForWebView:_webView webViewDelegate:self handler:^(id data, WVJBResponseCallback responseCallback) { responseCallback(@"Response for message from ObjC"); }]; [_bridge registerHandler:@"imageClick" handler:^(id data, WVJBResponseCallback responseCallback) { NSLog(@"testObjcCallback called: %@", data); if ([data isMatchedByRegex:@"_imageClick"]) { NSString *imageId = [data stringByReplacingOccurrencesOfString:@"_imageClick" withString:@""]; [self imageClick:imageId]; } }]; [_bridge registerHandler:@"linkClick" handler:^(id data, WVJBResponseCallback responseCallback) { if ([data isMatchedByRegex:@"_linkClick"]) { NSString *linkId = [data stringByReplacingOccurrencesOfString:@"_linkClick" withString:@""]; [self linkClick:linkId]; } }]; } #pragma mark -- btn handler -(void)gotoMovie:(UIButton*)btn{ NSMutableDictionary *urlDic = [NSMutableDictionary dictionary]; [urlDic setObject:@"http://video.thepaper.cn/video/0/9/139.mp4" forKey:@"standard"]; [urlDic setObject:@"http://video.thepaper.cn/video/0/9/140.mp4" forKey:@"high"]; // CGFloat videoHeight = 338/2*(rect_screen.size.width-2*10)/300; CGRect videoRect = CGRectMake(0, NAVBAR_HEIGHT, CGRectGetWidth(self.view.bounds), videoBtnHeight); self.moviePlayerController = [[TPMovieContent alloc] initWithFrame:videoRect contentUrlDic:urlDic]; self.moviePlayerController.delegate = self; [KEY_WINDOW addSubview:self.moviePlayerController.view]; [self.moviePlayerController relayoutSubViewForScreen:videoRect fullScreen:NO]; } -(void) gotoHot:(UIButton*)btn{ if (self.moviePlayerController) { [self stopAndRemoveVideo]; } [(MLNavigationController *)self.navigationController pushViewController:[hotController new] animated:YES]; } #pragma mark - image delegate - (void)imageClick:(NSString*)imageId { if ([imageId isMatchedByRegex:@"http:__"]) { imageId = [imageId stringByReplacingOccurrencesOfString:@"_" withString:@"/"]; } // NSString *webUrl = @"http://101.231.211.82:8083/demo/"; // NSString *imageUrl = [webUrl stringByAppendingString:imageId]; NSMutableArray *imageBOArray = [NSMutableArray array]; imageObjectBO *bo = [imageObjectBO new]; bo.url = imageId; bo.tags = @"www_big"; bo.desc = @"图片"; bo.width = @"500"; bo.height = @"300"; [imageBOArray addObject:bo]; TPImageScannerController *imageScanner = [[TPImageScannerController alloc]init]; [self.navigationController pushViewController:imageScanner animated:NO]; [UIView transitionWithView:self.navigationController.view duration:0.35f options:UIViewAnimationOptionTransitionFlipFromLeft animations:nil completion:^(BOOL finished) { imageScanner.currentIndex = 0; imageScanner.imageBOList = imageBOArray; imageScanner.type = contentImageScannerType; imageScanner.delegate = self; }]; // NSString *str = [NSString stringWithFormat:@"点击IOS:%@",imageId]; // ShowMessage(str,YES); } -(void) linkClick:(NSString*) linkId{ self.linkVC = [linkController new]; self.linkVC.webUrl = linkId; [self.navigationController pushViewController:self.linkVC animated:YES]; // ShowMessage([NSString stringWithFormat:@"点击IOS%@",linkId], YES); } - (void)imageContentClick:(imageObjectBO*)imageBO { [MobClick event:@"10"]; ((MLNavigationController *)self.navigationController).canDragBack = YES; [self.navigationController popViewControllerAnimated:NO]; [UIView transitionWithView:self.navigationController.view duration:0.35f options:UIViewAnimationOptionTransitionFlipFromRight animations:nil completion:nil]; } #pragma mark - video method - (void)playFinish { UIInterfaceOrientation currentOrientation = [[UIApplication sharedApplication] statusBarOrientation]; if (currentOrientation != UIInterfaceOrientationPortrait) { [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO]; [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"]; } [self stopAndRemoveVideo]; if ([self.delegate respondsToSelector:@selector(didEndPlayVedio)]) { [self.delegate didEndPlayVedio]; } [(TPWindow*)KEY_WINDOW enabelEmbeddedVideoGesture:NO withVideoController:nil embeddedRect:CGRectZero]; } - (void)turnMovieViewToPortrait { [self.moviePlayerController relayoutSubViewForScreen:self.moviePlayerController.originRect fullScreen:NO]; } - (void)stopAndRemoveVideo { [self.moviePlayerController.view removeFromSuperview]; [self.moviePlayerController selfDealloc]; self.moviePlayerController = nil; self.moviePlayerController.view = nil; ((MLNavigationController *)self.navigationController).canDragBack = YES; if ([self.delegate respondsToSelector:@selector(didEndPlayVedio)]) { [self.delegate didEndPlayVedio]; } } - (void)turnToright { [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationLandscapeRight animated:NO]; [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationLandscapeRight] forKey:@"orientation"]; } - (void)turnToPortrait { [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO]; [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"]; } - (void)enterFullScreen:(BOOL)isEnter { if (isEnter) { [self turnToPortrait]; }else { [self turnToright]; } } #pragma mark - tpmovie delegate - (void)closeHandler { UIInterfaceOrientation currentOrientation = [[UIApplication sharedApplication] statusBarOrientation]; if (currentOrientation != UIInterfaceOrientationPortrait) { [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:NO]; [[UIDevice currentDevice] setValue:[NSNumber numberWithInteger:UIInterfaceOrientationPortrait] forKey:@"orientation"]; } if (self.moviePlayerController.isEmbedded) { if ([self.delegate respondsToSelector:@selector(videoClosedWithEmbeddedFullScrrenn)]) { [self.delegate videoClosedWithEmbeddedFullScrrenn]; } } [self stopAndRemoveVideo]; [(TPWindow*)KEY_WINDOW enabelEmbeddedVideoGesture:NO withVideoController:nil embeddedRect:CGRectZero]; } - (void)movieTimedOut { [self stopAndRemoveVideo]; } - (void)moviePlayerWillMoveFromWindow { // if (![self.view.subviews containsObject:self.moviePlayerController.view]) { // UIInterfaceOrientation currentOrientation = [[UIApplication sharedApplication] statusBarOrientation]; // if (currentOrientation != UIInterfaceOrientationPortrait) { // [[UIApplication sharedApplication] // setStatusBarOrientation:UIInterfaceOrientationPortrait // animated:NO]; // // [[UIDevice currentDevice] // setValue:[NSNumber // numberWithInteger:UIInterfaceOrientationPortrait] // forKey:@"orientation"]; // } // [self.view addSubview:self.moviePlayerController.view]; // [self.moviePlayerController setFrame:videoRect]; // self.moviePlayerController.controls.cutPhotoBtn.hidden = YES; // self.moviePlayerController.controls.fullscreenButton.selected = NO; // } } #pragma mark - fontSize - (void)reloadTableWithFontChange { dispatch_async(dispatch_get_main_queue(), ^{ [_bridge send:[NSString stringWithFormat:@"changeFontSize,%fpx",[TPUserDefault instance].contFontSize]]; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ CGFloat height = [[self.webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue]; CGSize contentSize = self.webView.scrollView.contentSize; webHeight = height+100; self.webView.scrollView.contentSize = CGSizeMake(contentSize.width, webHeight); }); }); } - (void)webViewDidStartLoad:(UIWebView *)webView { } - (void)webViewDidFinishLoad:(UIWebView *)webView { [[NSUserDefaults standardUserDefaults] setInteger:0 forKey:@"WebKitCacheModelPreferenceKey"]; [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitDiskImageCacheEnabled"];//自己添加的,原文没有提到。 [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"WebKitOfflineWebApplicationCacheEnabled"];//自己添加的,原文没有提到。 [[NSUserDefaults standardUserDefaults] synchronize]; isWebViewLoadFinish = YES; dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{ CGFloat height = [[webView stringByEvaluatingJavaScriptFromString:@"document.body.offsetHeight"] floatValue]; CGSize contentSize = webView.scrollView.contentSize; webHeight = height+100; webView.scrollView.contentSize = CGSizeMake(contentSize.width, webHeight); self.pushBtn.frame = CGRectMake(0,height, rect_screen.size.width, 100); [self addObserver:self forKeyPath:@"self.webView.scrollView.contentSize" options:NSKeyValueObservingOptionNew context:nil]; }); } #pragma mark - KVO call back - (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context { if (self.webView.scrollView.contentSize.height != webHeight) { CGSize contentSize = self.webView.scrollView.contentSize; self.webView.scrollView.contentSize = CGSizeMake(contentSize.width, webHeight); [self.webView.scrollView setContentOffset:CGPointMake(0, -200)]; } } - (void)dealloc { NSLog(@"dealloc:%@",self.description); if (isWebViewLoadFinish) { [self removeObserver:self forKeyPath:@"self.webView.scrollView.contentSize" context:nil]; } [self.webView stopLoading]; [self.webView loadHTMLString:@" " baseURL:nil]; [self.webView removeFromSuperview]; self.webView.delegate = nil; self.webView = nil; [[NSURLCache sharedURLCache] removeAllCachedResponses]; [[NSURLCache sharedURLCache] setDiskCapacity:0]; [[NSURLCache sharedURLCache] setMemoryCapacity:0]; [[NSNotificationCenter defaultCenter] removeObserver:self]; } - (void)didReceiveMemoryWarning { [super didReceiveMemoryWarning]; // Dispose of any resources that can be recreated. } @end