| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199 |
- //
- // YRWebVC.m
- // UU_Ent
- //
- // Created by liujl on 2019/4/29.
- // Copyright © 2019 UAS. All rights reserved.
- //
- #import "YRWebVC.h"
- #import "AppDelegate.h"
- #import "UIDevice+extension.h"
- #import <WebKit/WebKit.h>
- #import <JavaScriptCore/JavaScriptCore.h>
- @interface YRWebVC ()<WKUIDelegate,WKNavigationDelegate,WKScriptMessageHandler,UINavigationControllerDelegate>
- @property(strong,nonatomic)UIProgressView *progress;
- @property(strong,nonatomic)WKWebView *webView;
- @end
- @implementation YRWebVC
- - (void)viewDidLoad {
- [super viewDidLoad];
-
- }
- -(void)createUI{
-
- //创建配置对象
- WKWebViewConfiguration *webConfig = [[WKWebViewConfiguration alloc]init];
-
- //为配置对象设置偏好设置
- WKPreferences *preferences = [[WKPreferences alloc]init];
- webConfig.preferences = preferences;
-
- //允许js和native交互
- preferences.javaScriptEnabled = YES;
-
- WKUserContentController *userContent = [[WKUserContentController alloc]init];
-
- [userContent addScriptMessageHandler:self name:@"event"];
-
- //监听相应的事件
- webConfig.userContentController = userContent;
-
- self.webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 64, self.view.frame.size.width, self.view.frame.size.height) configuration:webConfig];
-
- [self.view addSubview:self.webView];
-
- [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
-
- make.edges.equalTo(self.view);
-
- }];
-
- self.webView.navigationDelegate = self;
-
-
- NSURL *url = [NSURL URLWithString:self.url];
-
- NSURLRequest *request = [NSURLRequest requestWithURL:url];
-
- [self.webView loadRequest:request];
-
- self.progress = [UIProgressView new];
-
- self.progress.trackTintColor = self.view.backgroundColor;
-
- self.progress.progressTintColor = [UIColor orangeColor];
-
- [self.view addSubview:self.progress];
-
- [self.progress mas_makeConstraints:^(MASConstraintMaker *make) {
-
- make.top.left.right.equalTo(self.view);
- make.height.mas_equalTo(2);
-
- }];
-
-
-
- [self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
-
-
- }
- -(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation{
-
- [self.webView evaluateJavaScript:@"document.title" completionHandler:^(NSString * _Nullable title , NSError * _Nullable error) {
-
- self.title = title;
-
- }];
-
-
- }
- //接受js发送的消息
- -(void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{
-
- NSString *name = message.name;
-
- if ([name isEqualToString:@"event"]) {
-
- // 打开相册
- if ([message.body isEqualToString:@"ChartInterfaceOrientationMaskPortrait"]) {//横屏
-
- [self chartInterfaceOrientationMaskLandscapeRight];
-
- }else {//竖屏
-
- [self chartInterfaceOrientationMaskPortrait];
-
- }
-
-
- }
-
-
-
- }
- //横屏
- -(void)chartInterfaceOrientationMaskLandscapeRight{
-
- AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
- //允许转成横屏
- appDelegate.allowRotation = YES;
- //调用横屏代码
- [UIDevice switchNewOrientation:UIInterfaceOrientationLandscapeRight];
-
-
- [self.navigationController setNavigationBarHidden:YES animated:YES];
-
- }
- //竖屏
- -(void)chartInterfaceOrientationMaskPortrait{
-
- AppDelegate * appDelegate = (AppDelegate *)[UIApplication sharedApplication].delegate;
- //允许转成横屏
- appDelegate.allowRotation = NO;
- //调用横屏代码
- [UIDevice switchNewOrientation:UIInterfaceOrientationPortrait];
-
- [self.navigationController setNavigationBarHidden:NO animated:YES];
-
- }
- -(void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSKeyValueChangeKey,id> *)change context:(void *)context{
-
- if ([keyPath isEqualToString:@"estimatedProgress"]) {
-
- self.progress.progress = self.webView.estimatedProgress;
- if (self.webView.estimatedProgress >= 1.0f) {
- [UIView animateWithDuration:0.5f animations:^{
- self.progress.alpha = 0.0f;
- self.progress.progress = 0.0f;
- }];
- }else{
-
- self.progress.alpha = 1.0;
-
- }
-
- }
-
- }
- -(void)viewDidDisappear:(BOOL)animated{
-
- [super viewDidDisappear:animated];
-
- if (self.backBlock) {
-
- self.backBlock();
-
- }
-
-
-
- }
- -(void)removeAllMsgHandler{
-
- [self.webView.configuration.userContentController removeScriptMessageHandlerForName:@"event"];
-
-
- }
- -(void)dealloc{
-
- [self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
-
- }
- @end
|