|
18 | 18 | #import "TWTRWebViewController.h"
|
19 | 19 | #import <TwitterCore/TWTRAuthenticationConstants.h>
|
20 | 20 |
|
21 |
| -@interface TWTRWebViewController () <UIWebViewDelegate> |
| 21 | +@interface TWTRWebViewController () <WKNavigationDelegate> |
22 | 22 |
|
23 |
| -@property (nonatomic, strong) UIWebView *webView; |
| 23 | +@property (nonatomic, strong) WKWebView *webView; |
24 | 24 | @property (nonatomic, assign) BOOL showCancelButton;
|
25 | 25 | @property (nonatomic, copy) TWTRWebViewControllerCancelCompletion cancelCompletion;
|
26 | 26 |
|
27 | 27 | @end
|
28 | 28 |
|
29 | 29 | @implementation TWTRWebViewController
|
30 | 30 |
|
| 31 | +// Conversion from UIWebViewNavigationType to WKNavigationType |
| 32 | ++ (UIWebViewNavigationType)_enumHelperForNavigationType:(WKNavigationType)wkNavigationType { |
| 33 | + switch (wkNavigationType) { |
| 34 | + case WKNavigationTypeLinkActivated: |
| 35 | + return UIWebViewNavigationTypeLinkClicked; |
| 36 | + break; |
| 37 | + case WKNavigationTypeFormSubmitted: |
| 38 | + return UIWebViewNavigationTypeFormSubmitted; |
| 39 | + break; |
| 40 | + case WKNavigationTypeBackForward: |
| 41 | + return UIWebViewNavigationTypeBackForward; |
| 42 | + break; |
| 43 | + case WKNavigationTypeReload: |
| 44 | + return UIWebViewNavigationTypeReload; |
| 45 | + break; |
| 46 | + case WKNavigationTypeFormResubmitted: |
| 47 | + return UIWebViewNavigationTypeFormResubmitted; |
| 48 | + break; |
| 49 | + case WKNavigationTypeOther: |
| 50 | + default: |
| 51 | + return UIWebViewNavigationTypeOther; |
| 52 | + break; |
| 53 | + } |
| 54 | +} |
| 55 | + |
31 | 56 | - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
|
32 | 57 | {
|
33 | 58 | self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
|
@@ -65,30 +90,32 @@ - (void)load
|
65 | 90 |
|
66 | 91 | - (void)loadView
|
67 | 92 | {
|
68 |
| - [self setWebView:[[UIWebView alloc] init]]; |
69 |
| - [[self webView] setScalesPageToFit:YES]; |
70 |
| - [[self webView] setDelegate:self]; |
| 93 | + [self initWebView]; |
71 | 94 | [self setView:[self webView]];
|
72 | 95 | }
|
73 | 96 |
|
74 |
| -#pragma mark - UIWebview delegate |
| 97 | +#pragma mark - WKWebview delegate |
75 | 98 |
|
76 |
| -- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType |
77 |
| -{ |
| 99 | +- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler { |
| 100 | + NSURLRequest* request = navigationAction.request; |
78 | 101 | if (![self whitelistedDomain:request]) {
|
79 | 102 | // Open in Safari if request is not whitelisted
|
80 | 103 | NSLog(@"Opening link in Safari browser, as the host is not whitelisted: %@", request.URL);
|
81 | 104 | [[UIApplication sharedApplication] openURL:request.URL];
|
82 |
| - return NO; |
| 105 | + decisionHandler(WKNavigationActionPolicyCancel); |
| 106 | + return; |
83 | 107 | }
|
| 108 | + WKNavigationActionPolicy decision = WKNavigationActionPolicyAllow; |
84 | 109 | if ([self shouldStartLoadWithRequest]) {
|
85 |
| - return [self shouldStartLoadWithRequest](self, request, navigationType); |
| 110 | + if () { |
| 111 | + decision = WKNavigationActionPolicyCancel; |
| 112 | + }; |
86 | 113 | }
|
87 |
| - return YES; |
| 114 | + |
| 115 | + decisionHandler(decision); |
88 | 116 | }
|
89 | 117 |
|
90 |
| -- (void)webView:(UIWebView *)webView didFailLoadWithError:(NSError *)error |
91 |
| -{ |
| 118 | +- (void)webView:(WKWebView *)webView didFailNavigation:(WKNavigation *)navigation withError:(NSError *)error { |
92 | 119 | if (self.errorHandler) {
|
93 | 120 | self.errorHandler(error);
|
94 | 121 | self.errorHandler = nil;
|
@@ -120,4 +147,21 @@ - (void)enableCancelButtonWithCancelCompletion:(TWTRWebViewControllerCancelCompl
|
120 | 147 | [self setCancelCompletion:cancelCompletion];
|
121 | 148 | }
|
122 | 149 |
|
| 150 | +- (void)initWebView { |
| 151 | + // From: https://stackoverflow.com/questions/26295277/wkwebview-equivalent-for-uiwebviews-scalespagetofit |
| 152 | + NSString *jScript = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);"; |
| 153 | + |
| 154 | + WKUserScript *wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES]; |
| 155 | + WKUserContentController *wkUController = [[WKUserContentController alloc] init]; |
| 156 | + [wkUController addUserScript:wkUScript]; |
| 157 | + |
| 158 | + WKWebViewConfiguration *wkWebConfig = [[WKWebViewConfiguration alloc] init]; |
| 159 | + wkWebConfig.userContentController = wkUController; |
| 160 | + |
| 161 | + WKWebView* wkWebV = [[WKWebView alloc] initWithFrame:self.view.frame configuration:wkWebConfig]; |
| 162 | + |
| 163 | + [self setWebView:wkWebV]; |
| 164 | + [[self webView] setNavigationDelegate:self]; |
| 165 | + |
| 166 | +} |
123 | 167 | @end
|
0 commit comments