forked from sticksen/STKWebKitViewController
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSTKWebKitViewController.m
More file actions
244 lines (199 loc) · 8.83 KB
/
STKWebKitViewController.m
File metadata and controls
244 lines (199 loc) · 8.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
//
// STKWebKitViewController.m
// STKWebKitViewController
//
// Created by Marc on 03.09.14.
// Copyright (c) 2014 sticksen. All rights reserved.
//
#import "STKWebKitViewController.h"
@interface STKWebKitViewController ()
@property(nonatomic) NSMutableArray *viewConstraints;
@property(nonatomic) UIColor *savedNavigationbarTintColor;
@property(nonatomic) UIColor *savedToolbarTintColor;
@property(nonatomic) NSURLRequest *request;
@property (nonatomic) BOOL toolbarWasHidden;
@end
@implementation STKWebKitViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
return [self initWithURL:nil];
}
- (instancetype)initWithCoder:(NSCoder *)aDecoder
{
return [self initWithURL:nil];
}
- (instancetype)init
{
return [self initWithURL:nil];
}
- (instancetype)initWithAddress:(NSString *)urlString
{
return [self initWithURL:[NSURL URLWithString:urlString]];
}
- (instancetype)initWithURL:(NSURL *)url
{
return [self initWithURL:url userScript:nil];
}
- (instancetype)initWithURL:(NSURL *)url userScript:(WKUserScript *)script
{
return [self initWithRequest:[NSURLRequest requestWithURL:url] userScript:script];
}
- (instancetype)initWithAddress:(NSString *)string userScript:(WKUserScript *)script
{
return [self initWithURL:[NSURL URLWithString:string] userScript:script];
}
- (instancetype)initWithRequest:(NSURLRequest *)request
{
return [self initWithRequest:request userScript:nil];
}
- (instancetype)initWithRequest:(NSURLRequest *)request userScript:(WKUserScript *)script
{
if (self = [super initWithNibName:nil bundle:nil]) {
NSAssert([[UIDevice currentDevice].systemVersion floatValue] >= 8.0, @"WKWebView is available since iOS8. Use UIWebView, if you´re running an older version");
NSAssert([NSThread isMainThread], @"WebKit is not threadsafe and this function is not executed on the main thread");
self.newTabOpenMode = OpenNewTabExternal;
self.request = request;
if (script) {
WKUserContentController *userContentController = [[WKUserContentController alloc] init];
[userContentController addUserScript:script];
WKWebViewConfiguration *configuration = [[WKWebViewConfiguration alloc] init];
configuration.userContentController = userContentController;
_webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration];
} else {
_webView = [[WKWebView alloc] initWithFrame:CGRectZero];
}
_webView.navigationDelegate = self;
[self.view addSubview:_webView];
}
return self;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
NSAssert(self.navigationController, @"STKWebKitViewController needs to be contained in a UINavigationController. If you are presenting STKWebKitViewController modally, use STKModalWebKitViewController instead.");
[self.view setNeedsUpdateConstraints];
self.toolbarWasHidden = self.navigationController.isToolbarHidden;
[self.navigationController setToolbarHidden:NO animated:YES];
[self fillToolbar];
self.savedNavigationbarTintColor = self.navigationController.navigationBar.barTintColor;
self.savedToolbarTintColor = self.navigationController.toolbar.barTintColor;
if (self.toolbarTintColor) {
self.navigationController.toolbar.barTintColor = self.toolbarTintColor;
self.navigationController.toolbar.backgroundColor = self.toolbarTintColor;
self.navigationController.toolbar.tintColor = [UIColor whiteColor];
}
if (self.navigationBarTintColor) {
self.navigationController.navigationBar.barTintColor = self.navigationBarTintColor;
}
[self addObserver:self forKeyPath:@"webView.title" options:NSKeyValueObservingOptionNew context:NULL];
[self addObserver:self forKeyPath:@"webView.loading" options:NSKeyValueObservingOptionNew context:NULL];
[self addObserver:self forKeyPath:@"webView.estimatedProgress" options:NSKeyValueObservingOptionNew context:NULL];
if (self.request) {
[self.webView loadRequest:self.request];
}
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
self.navigationController.navigationBar.barTintColor = self.savedNavigationbarTintColor;
[self.navigationController setToolbarHidden:self.toolbarWasHidden];
self.navigationController.toolbar.barTintColor = self.savedToolbarTintColor;
[self removeObserver:self forKeyPath:@"webView.title"];
[self removeObserver:self forKeyPath:@"webView.loading"];
[self removeObserver:self forKeyPath:@"webView.estimatedProgress"];
}
- (BOOL)prefersStatusBarHidden
{
return self.navigationController.navigationBarHidden;
}
- (UIStatusBarAnimation)preferredStatusBarUpdateAnimation
{
return UIStatusBarAnimationSlide;
}
- (void)fillToolbar
{
UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"back"] style:UIBarButtonItemStylePlain target:self action:@selector(backTapped:)];
if (self.webView.canGoBack) {
backItem.tintColor = nil;
} else {
backItem.tintColor = [UIColor lightGrayColor];
}
UIBarButtonItem *forwardItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"forward"] style:UIBarButtonItemStylePlain target:self action:@selector(forwardTapped:)];
if (self.webView.canGoForward) {
forwardItem.tintColor = nil;
} else {
forwardItem.tintColor = [UIColor lightGrayColor];
}
UIBarButtonItem *reloadItem;
if (self.webView.isLoading) {
reloadItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"stop"] style:UIBarButtonItemStylePlain target:self action:@selector(stopTapped:)];
} else {
reloadItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"refresh"] style:UIBarButtonItemStylePlain target:self action:@selector(reloadTapped:)];
}
UIBarButtonItem *shareItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemAction target:self action:@selector(shareTapped:)];
UIBarButtonItem *flexibleSpaceItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
[self setToolbarItems:@[flexibleSpaceItem, backItem, flexibleSpaceItem, forwardItem, flexibleSpaceItem, reloadItem, flexibleSpaceItem, shareItem, flexibleSpaceItem] animated:NO];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context
{
if ([keyPath isEqualToString:@"webView.title"]) {
self.title = change[@"new"];
} else if ([keyPath isEqualToString:@"webView.loading"]) {
[self fillToolbar];
} else if ([keyPath isEqualToString:@"webView.estimatedProgress"]) {
}
}
- (void)viewDidLayoutSubviews
{
self.webView.frame = self.view.bounds;
}
- (void)backTapped:(UIBarButtonItem *)button
{
[self.webView goBack];
}
- (void)forwardTapped:(UIBarButtonItem *)button
{
[self.webView goForward];
}
- (void)reloadTapped:(UIBarButtonItem *)button
{
[self.webView reload];
}
- (void)stopTapped:(UIBarButtonItem *)button
{
[self.webView stopLoading];
}
- (void)shareTapped:(UIBarButtonItem *)button
{
NSMutableArray *items = [[NSMutableArray alloc] init];
if (self.title) {
[items addObject:self.title];
}
if (self.request.URL) {
[items addObject:self.request.URL];
}
UIActivityViewController *controller = [[UIActivityViewController alloc] initWithActivityItems:items applicationActivities:self.applicationActivities];
controller.popoverPresentationController.barButtonItem = button;
[self presentViewController:controller animated:YES completion:nil];
}
#pragma mark -
- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
{
if (!navigationAction.targetFrame) { //this is a 'new window action' (aka target="_blank") > open this URL as configured in 'self.newTabOpenMode'. If we´re doing nothing here, WKWebView will also just do nothing. Maybe this will change in a later stage of iOS 8
if (self.newTabOpenMode == OpenNewTabExternal) {
NSURL *url = navigationAction.request.URL;
UIApplication *app = [UIApplication sharedApplication];
if ([app canOpenURL:url]) {
[app openURL:url];
}
} else if (self.newTabOpenMode == OpenNewTabInternal) {
[webView loadRequest:navigationAction.request];
}
}
decisionHandler(WKNavigationActionPolicyAllow);
}
-(void)webView:(WKWebView *)webView didFinishNavigation:(WKNavigation *)navigation
{
[self.webView.scrollView scrollRectToVisible:CGRectMake(0, 0, 1, 1) animated:NO]; //otherwise top of website is sometimes hidden under Navigation Bar
}
@end