Browse Source

Migrate to WKWebView

Iulian Onofrei 4 years ago
parent
commit
f98e0622b5
2 changed files with 21 additions and 17 deletions
  1. 20 16
      Classes/GlobalStateExplorers/FLEXWebViewController.m
  2. 1 1
      FLEX.podspec

+ 20 - 16
Classes/GlobalStateExplorers/FLEXWebViewController.m

@@ -8,10 +8,11 @@
 
 #import "FLEXWebViewController.h"
 #import "FLEXUtility.h"
+#import <WebKit/WebKit.h>
 
-@interface FLEXWebViewController () <UIWebViewDelegate>
+@interface FLEXWebViewController () <WKNavigationDelegate>
 
-@property (nonatomic, strong) UIWebView *webView;
+@property (nonatomic, strong) WKWebView *webView;
 @property (nonatomic, strong) NSString *originalText;
 
 @end
@@ -22,10 +23,12 @@
 {
     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
     if (self) {
-        self.webView = [[UIWebView alloc] init];
-        self.webView.delegate = self;
-        self.webView.dataDetectorTypes = UIDataDetectorTypeLink;
-        self.webView.scalesPageToFit = YES;
+        WKWebViewConfiguration *configuration = [WKWebViewConfiguration new];
+
+        configuration.dataDetectorTypes = UIDataDetectorTypeLink;
+
+        self.webView = [[WKWebView alloc] initWithFrame:CGRectZero configuration:configuration];
+        self.webView.navigationDelegate = self;
     }
     return self;
 }
@@ -53,9 +56,9 @@
 
 - (void)dealloc
 {
-    // UIWebView's delegate is assign so we need to clear it manually.
-    if (_webView.delegate == self) {
-        _webView.delegate = nil;
+    // WKWebView's delegate is assigned so we need to clear it manually.
+    if (_webView.navigationDelegate == self) {
+        _webView.navigationDelegate = nil;
     }
 }
 
@@ -78,22 +81,23 @@
 }
 
 
-#pragma mark - UIWebView Delegate
+#pragma mark - WKWebView Delegate
 
-- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
+- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler
 {
-    BOOL shouldStart = NO;
-    if (navigationType == UIWebViewNavigationTypeOther) {
+    WKNavigationActionPolicy policy = WKNavigationActionPolicyCancel;
+    if (navigationAction.navigationType == WKNavigationTypeOther) {
         // Allow the initial load
-        shouldStart = YES;
+        policy = WKNavigationActionPolicyAllow;
     } else {
         // For clicked links, push another web view controller onto the navigation stack so that hitting the back button works as expected.
-        // Don't allow the current web view do handle the navigation.
+        // Don't allow the current web view to handle the navigation.
+        NSURLRequest *request = navigationAction.request;
         FLEXWebViewController *webVC = [[[self class] alloc] initWithURL:[request URL]];
         webVC.title = [[request URL] absoluteString];
         [self.navigationController pushViewController:webVC animated:YES];
     }
-    return shouldStart;
+    decisionHandler(policy);
 }
 
 

+ 1 - 1
FLEX.podspec

@@ -32,7 +32,7 @@ Pod::Spec.new do |spec|
   spec.platform         = :ios, "8.0"
   spec.source           = { :git => "https://github.com/Flipboard/FLEX.git", :tag => "#{spec.version}" }
   spec.source_files     = "Classes/**/*.{h,m,mm}"
-  spec.frameworks       = [ "Foundation", "UIKit", "CoreGraphics", "ImageIO", "QuartzCore" ]
+  spec.frameworks       = [ "Foundation", "UIKit", "CoreGraphics", "ImageIO", "QuartzCore", "WebKit" ]
   spec.libraries        = [ "z", "sqlite3" ]
   spec.requires_arc     = true
   spec.public_header_files = [ "Classes/**/FLEXManager.h", "Classes/FLEX.h" ]