This repository has been archived by the owner on Feb 7, 2020. It is now read-only.
/
ViewController.m
201 lines (170 loc) · 6.75 KB
/
ViewController.m
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
//
// ViewController.m
// FLWebView
//
// Created by Steve Richey on 11/21/14.
// Copyright (c) 2014 Float Mobile Learning. Shared under an MIT license. See license.md for details.
//
#import "ViewController.h"
// Required for calls to UIWebView and WKWebView to "see" our categories
#import "UIWebView+FLUIWebView.h"
#import "WKWebView+FLWKWebView.h"
@interface ViewController ()
@end
@implementation ViewController
/*
* Called when the view has completed loading. Time to set up our WebView!
*/
- (void) viewDidLoad {
[super viewDidLoad];
// Check if WKWebView is available
// If it is present, create a WKWebView. If not, create a UIWebView.
if (NSClassFromString(@"WKWebView")) {
_webView = [[WKWebView alloc] initWithFrame: [[self view] bounds]];
} else {
_webView = [[UIWebView alloc] initWithFrame: [[self view] bounds]];
}
// Add the webView to the current view.
[[self view] addSubview: [self webView]];
// Assign this view controller as the delegate view.
// The delegate methods are below, and include methods for UIWebViewDelegate, WKNavigationDelegate, and WKUIDelegate
[[self webView] setDelegateViews: self];
// Ensure that everything will resize on device rotate.
[[self webView] setAutoresizingMask: UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[[self view] setAutoresizingMask: UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
// Just to show *something* on load, we go to our favorite site.
[[self webView] loadRequestFromString:@"https://www.gowithfloat.com/"];
}
/*
* Enable rotating the view when the device rotates.
*/
- (BOOL) shouldAutorotateToInterfaceOrientation: (UIInterfaceOrientation) toInterfaceOrientation
{
return YES;
}
/*
* This more or less ensures that the status bar is hidden for this view.
* We also set UIStatusBarHidden to true in the Info.plist file.
* We hide the status bar so we can use the full screen height without worrying about an offset for the status bar.
*/
- (BOOL) prefersStatusBarHidden
{
return YES;
}
#pragma mark - UIWebView Delegate Methods
/*
* Called on iOS devices that do not have WKWebView when the UIWebView requests to start loading a URL request.
* Note that it just calls shouldStartDecidePolicy, which is a shared delegate method.
* Returning YES here would allow the request to complete, returning NO would stop it.
*/
- (BOOL) webView: (UIWebView *) webView shouldStartLoadWithRequest: (NSURLRequest *) request navigationType: (UIWebViewNavigationType) navigationType
{
return [self shouldStartDecidePolicy: request];
}
/*
* Called on iOS devices that do not have WKWebView when the UIWebView starts loading a URL request.
* Note that it just calls didStartNavigation, which is a shared delegate method.
*/
- (void) webViewDidStartLoad: (UIWebView *) webView
{
[self didStartNavigation];
}
/*
* Called on iOS devices that do not have WKWebView when a URL request load failed.
* Note that it just calls failLoadOrNavigation, which is a shared delegate method.
*/
- (void) webView: (UIWebView *) webView didFailLoadWithError: (NSError *) error
{
[self failLoadOrNavigation: [webView request] withError: error];
}
/*
* Called on iOS devices that do not have WKWebView when the UIWebView finishes loading a URL request.
* Note that it just calls finishLoadOrNavigation, which is a shared delegate method.
*/
- (void) webViewDidFinishLoad: (UIWebView *) webView
{
[self finishLoadOrNavigation: [webView request]];
}
#pragma mark - WKWebView Delegate Methods
/*
* Called on iOS devices that have WKWebView when the web view wants to start navigation.
* Note that it calls shouldStartDecidePolicy, which is a shared delegate method,
* but it's essentially passing the result of that method into decisionHandler, which is a block.
*/
- (void) webView: (WKWebView *) webView decidePolicyForNavigationAction: (WKNavigationAction *) navigationAction decisionHandler: (void (^)(WKNavigationActionPolicy)) decisionHandler
{
decisionHandler([self shouldStartDecidePolicy: [navigationAction request]]);
}
/*
* Called on iOS devices that have WKWebView when the web view starts loading a URL request.
* Note that it just calls didStartNavigation, which is a shared delegate method.
*/
- (void) webView: (WKWebView *) webView didStartProvisionalNavigation: (WKNavigation *) navigation
{
[self didStartNavigation];
}
/*
* Called on iOS devices that have WKWebView when the web view fails to load a URL request.
* Note that it just calls failLoadOrNavigation, which is a shared delegate method,
* but it has to retrieve the active request from the web view as WKNavigation doesn't contain a reference to it.
*/
- (void) webView:(WKWebView *) webView didFailProvisionalNavigation: (WKNavigation *) navigation withError: (NSError *) error
{
[self failLoadOrNavigation: [webView request] withError: error];
}
/*
* Called on iOS devices that have WKWebView when the web view begins loading a URL request.
* This could call some sort of shared delegate method, but is unused currently.
*/
- (void) webView: (WKWebView *) webView didCommitNavigation: (WKNavigation *) navigation
{
// do nothing
}
/*
* Called on iOS devices that have WKWebView when the web view fails to load a URL request.
* Note that it just calls failLoadOrNavigation, which is a shared delegate method.
*/
- (void) webView: (WKWebView *) webView didFailNavigation: (WKNavigation *) navigation withError: (NSError *) error
{
[self failLoadOrNavigation: [webView request] withError: error];
}
/*
* Called on iOS devices that have WKWebView when the web view finishes loading a URL request.
* Note that it just calls finishLoadOrNavigation, which is a shared delegate method.
*/
- (void) webView: (WKWebView *) webView didFinishNavigation: (WKNavigation *) navigation
{
[self finishLoadOrNavigation: [webView request]];
}
#pragma mark - Shared Delegate Methods
/*
* This is called whenever the web view wants to navigate.
*/
- (BOOL) shouldStartDecidePolicy: (NSURLRequest *) request
{
// Determine whether or not navigation should be allowed.
// Return YES if it should, NO if not.
return YES;
}
/*
* This is called whenever the web view has started navigating.
*/
- (void) didStartNavigation
{
// Update things like loading indicators here.
}
/*
* This is called when navigation failed.
*/
- (void) failLoadOrNavigation: (NSURLRequest *) request withError: (NSError *) error
{
// Notify the user that navigation failed, provide information on the error, and so on.
}
/*
* This is called when navigation succeeds and is complete.
*/
- (void) finishLoadOrNavigation: (NSURLRequest *) request
{
// Remove the loading indicator, maybe update the navigation bar's title if you have one.
}
@end