|
@@ -9,57 +9,77 @@
|
|
|
#import "RDSUserProtocolViewController.h"
|
|
|
#import <WebKit/WebKit.h>
|
|
|
|
|
|
-@interface RDSUserProtocolViewController ()<WKUIDelegate,WKNavigationDelegate>
|
|
|
+@interface RDSUserProtocolViewController () <WKUIDelegate, WKNavigationDelegate, WKScriptMessageHandler>
|
|
|
@property (nonatomic, strong) WKWebView *webView;
|
|
|
|
|
|
@end
|
|
|
|
|
|
@implementation RDSUserProtocolViewController
|
|
|
|
|
|
+- (void)dealloc {
|
|
|
+ WKUserContentController *controller = self.webView.configuration.userContentController;
|
|
|
+ [controller removeScriptMessageHandlerForName:@"closePage"];
|
|
|
+}
|
|
|
+
|
|
|
- (void)viewDidLoad {
|
|
|
[super viewDidLoad];
|
|
|
-
|
|
|
+
|
|
|
self.title = _vcTitle;
|
|
|
[self p_setupUI];
|
|
|
-
|
|
|
+
|
|
|
[self p_loadData];
|
|
|
-
|
|
|
}
|
|
|
|
|
|
--(void) p_loadData{
|
|
|
+- (void)viewWillAppear:(BOOL)animated {
|
|
|
+ [super viewWillAppear:animated];
|
|
|
+ [self.navigationController setNavigationBarHidden:YES];
|
|
|
+}
|
|
|
+
|
|
|
+- (void)viewWillDisappear:(BOOL)animated {
|
|
|
+ [super viewWillDisappear:animated];
|
|
|
+ [self.navigationController setNavigationBarHidden:NO];
|
|
|
+}
|
|
|
+
|
|
|
+- (void)p_loadData {
|
|
|
/* 加载服务器url的方法*/
|
|
|
NSString *url = self.url;
|
|
|
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:url]];
|
|
|
[_webView loadRequest:request];
|
|
|
}
|
|
|
|
|
|
--(void) p_setupUI{
|
|
|
-
|
|
|
- self.webView = [[WKWebView alloc] init];
|
|
|
+- (void)p_setupUI {
|
|
|
+ WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
|
|
|
+ [configuration.userContentController addScriptMessageHandler:self name:@"closePage"];
|
|
|
|
|
|
+ self.webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration];
|
|
|
+
|
|
|
// 放在AppDelegate中全局修改
|
|
|
-// [_webView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
|
|
|
-// NSString *newUserAgent = [result stringByAppendingFormat:@"/%@", @"yongxulvjian"];
|
|
|
-// [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent":newUserAgent}];
|
|
|
-// }];
|
|
|
-
|
|
|
+ // [_webView evaluateJavaScript:@"navigator.userAgent" completionHandler:^(id _Nullable result, NSError * _Nullable error) {
|
|
|
+ // NSString *newUserAgent = [result stringByAppendingFormat:@"/%@", @"yongxulvjian"];
|
|
|
+ // [[NSUserDefaults standardUserDefaults] registerDefaults:@{@"UserAgent":newUserAgent}];
|
|
|
+ // }];
|
|
|
+
|
|
|
[self.view addSubview:_webView];
|
|
|
[_webView mas_makeConstraints:^(MASConstraintMaker *make) {
|
|
|
- make.left.equalTo(self.view);
|
|
|
- make.right.equalTo(self.view);
|
|
|
- make.top.equalTo(self.view);
|
|
|
- if (@available(iOS 11.0, *)) {
|
|
|
- make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
|
|
|
- } else {
|
|
|
- make.bottom.equalTo(self.view.mas_bottom);
|
|
|
- }
|
|
|
+// make.left.equalTo(self.view);
|
|
|
+// make.right.equalTo(self.view);
|
|
|
+// make.top.equalTo(self.view);
|
|
|
+// if (@available(iOS 11.0, *)) {
|
|
|
+// make.bottom.equalTo(self.view.mas_safeAreaLayoutGuideBottom);
|
|
|
+// } else {
|
|
|
+// make.bottom.equalTo(self.view.mas_bottom);
|
|
|
+// }
|
|
|
+ make.edges.mas_equalTo(UIEdgeInsetsZero);
|
|
|
}];
|
|
|
_webView.UIDelegate = self;
|
|
|
_webView.navigationDelegate = self;
|
|
|
_webView.allowsBackForwardNavigationGestures = YES;
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
+}
|
|
|
+
|
|
|
+- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message {
|
|
|
+ if ([message.name isEqualToString:@"closePage"]) {
|
|
|
+ [self.navigationController popViewControllerAnimated:YES];
|
|
|
+ }
|
|
|
}
|
|
|
|
|
|
@end
|