Skip to content

Commit 71f199b

Browse files
committed
WKWebView优化
1 parent b02aec6 commit 71f199b

File tree

3 files changed

+53
-6
lines changed

3 files changed

+53
-6
lines changed

iOS_Tips/DarkMode/AppDelegate.m

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,9 @@ void HandleException(NSException *exception) {
3434
// NSLog(@"%@", exceptionInfo);
3535
}
3636
//设置异常捕获处理方法
37-
void SLSetUncaughtExceptionHandler(void)
38-
{
37+
void SLSetUncaughtExceptionHandler(void) {
3938
NSSetUncaughtExceptionHandler(&HandleException);
4039
}
41-
4240
//内存警告
4341
- (void)applicationDidReceiveMemoryWarning:(UIApplication *)application {
4442
// free_some_mem(1024*1024*10);

iOS_Tips/DarkMode/ViewController.m

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#import "SLOpenGLController.h"
1616
#import "SLMenuViewController.h"
1717
#import "SLCrashViewController.h"
18+
#import "SLWebViewController.h"
1819

1920
@interface ViewController ()<UITableViewDelegate, UITableViewDataSource>
2021
@property (nonatomic, strong) NSMutableArray *dataSource;
@@ -55,7 +56,8 @@ - (void)getData {
5556
@"VideoToolBox和AudioToolBox音视频编解码",
5657
@"OpenGL-ES学习", @"LeetCode算法练习集合",
5758
@"键盘和UIMenuController不能同时存在的问题",
58-
@"iOS Crash防护"]];
59+
@"iOS Crash防护",
60+
@"WKWebView优化"]];
5961
[self.tableView reloadData];
6062
}
6163

@@ -140,7 +142,11 @@ - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath
140142
case 10: {
141143
SLCrashViewController *crashViewController = [[SLCrashViewController alloc] init];
142144
[self.navigationController pushViewController:crashViewController animated:YES];
143-
// [self presentViewController:crashViewController animated:YES completion:nil];
145+
}
146+
break;
147+
case 11: {
148+
SLWebViewController *webViewController = [[SLWebViewController alloc] init];
149+
[self.navigationController pushViewController:webViewController animated:YES];
144150
}
145151
break;
146152
default:

iOS_Tips/DarkMode/WKWebView/SLWebViewController.m

Lines changed: 44 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,18 +7,61 @@
77
//
88

99
#import "SLWebViewController.h"
10+
#import <WebKit/WebKit.h>
1011

1112
@interface SLWebViewController ()
1213

14+
@property (nonatomic, strong) WKWebView * webView;
15+
1316
@end
1417

1518
@implementation SLWebViewController
1619

20+
#pragma mark - Override
1721
- (void)viewDidLoad {
1822
[super viewDidLoad];
19-
23+
[self setupUI];
24+
}
25+
26+
#pragma mark - UI
27+
- (void)setupUI {
28+
self.view.backgroundColor = UIColor.whiteColor;
29+
[self.view addSubview:self.webView];
30+
31+
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithTitle:@"上一步" style:UIBarButtonItemStyleDone target:self action:@selector(goBackAction:)];
32+
UIBarButtonItem *forwardItem = [[UIBarButtonItem alloc] initWithTitle:@"下一步" style:UIBarButtonItemStyleDone target:self action:@selector(goForwardAction:)];
33+
self.navigationItem.rightBarButtonItems = @[forwardItem,backItem];
34+
35+
}
36+
- (void)dealloc {
37+
NSLog(@"%@释放了",NSStringFromClass(self.class));
2038
}
2139

40+
#pragma mark - Getter
41+
- (WKWebView *)webView {
42+
if(_webView == nil){
43+
44+
//创建网页配置
45+
WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc] init];
46+
47+
_webView = [[WKWebView alloc] initWithFrame:CGRectMake(0, 0, SL_kScreenWidth, SL_kScreenHeight) configuration:config];
48+
49+
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://github.com/wsl2ls"]];
50+
[_webView loadRequest:request];
51+
}
52+
return _webView;
53+
}
54+
55+
#pragma mark - Help Methods
2256

57+
#pragma mark - Events Handle
58+
//返回上一步
59+
- (void)goBackAction:(id)sender{
60+
[_webView goBack];
61+
}
62+
//前往下一步
63+
- (void)goForwardAction:(id)sender{
64+
[_webView goForward];
65+
}
2366

2467
@end

0 commit comments

Comments
 (0)