From 52e15f461561248f05a655d6e3a9741d593f7f57 Mon Sep 17 00:00:00 2001 From: Xiaohan Wang Date: Thu, 5 Dec 2019 23:10:13 -0800 Subject: [PATCH 1/3] [NAR-47277] replace UIWebView with WKWebView --- .../FLEXWebViewController.m | 30 ++++++++++--------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/Classes/Global State Explorers/FLEXWebViewController.m b/Classes/Global State Explorers/FLEXWebViewController.m index 7162880654..f150e62c07 100644 --- a/Classes/Global State Explorers/FLEXWebViewController.m +++ b/Classes/Global State Explorers/FLEXWebViewController.m @@ -6,12 +6,13 @@ // Copyright (c) 2014 Flipboard. All rights reserved. // +#import #import "FLEXWebViewController.h" #import "FLEXUtility.h" -@interface FLEXWebViewController () +@interface FLEXWebViewController () -@property (nonatomic, strong) UIWebView *webView; +@property (nonatomic, strong) WKWebView *webView; @property (nonatomic, strong) NSString *originalText; @end @@ -22,10 +23,13 @@ - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { 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; + self.webView = [[WKWebView alloc] init]; + self.webView.navigationDelegate = self; + self.webView.configuration.dataDetectorTypes = UIDataDetectorTypeLink; + self.webView.contentMode = UIViewContentModeScaleToFill; + #if !TARGET_OS_IPHONE + self.webView.allowsMagnification = YES; + #endif } return self; } @@ -70,21 +74,19 @@ - (void)copyButtonTapped:(id)sender } -#pragma mark - UIWebView Delegate +#pragma mark - WKNavigationDelegate Delegate -- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType -{ - BOOL shouldStart = NO; - if (navigationType == UIWebViewNavigationTypeOther) { +- (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler { + if (navigationAction.navigationType == WKNavigationTypeOther) { // Allow the initial load - shouldStart = YES; + decisionHandler(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. - FLEXWebViewController *webVC = [[[self class] alloc] initWithURL:[request URL]]; + FLEXWebViewController *webVC = [[[self class] alloc] initWithURL:[navigationAction.request URL]]; [self.navigationController pushViewController:webVC animated:YES]; + decisionHandler(WKNavigationActionPolicyCancel); } - return shouldStart; } From 787705c480e08bb8926ab6793521ac5831a88f4d Mon Sep 17 00:00:00 2001 From: Xiaohan Wang Date: Mon, 9 Dec 2019 14:58:21 -0800 Subject: [PATCH 2/3] [NAR-47277] keep UIWebView code for apportable --- .../FLEXWebViewController.m | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/Classes/Global State Explorers/FLEXWebViewController.m b/Classes/Global State Explorers/FLEXWebViewController.m index f150e62c07..90f7ee341a 100644 --- a/Classes/Global State Explorers/FLEXWebViewController.m +++ b/Classes/Global State Explorers/FLEXWebViewController.m @@ -6,13 +6,22 @@ // Copyright (c) 2014 Flipboard. All rights reserved. // +#ifdef PGDROID +#else #import +#endif #import "FLEXWebViewController.h" #import "FLEXUtility.h" +#ifdef PGDROID +@interface FLEXWebViewController () + +@property (nonatomic, strong) UIWebView *webView; +#else @interface FLEXWebViewController () @property (nonatomic, strong) WKWebView *webView; +#endif @property (nonatomic, strong) NSString *originalText; @end @@ -23,6 +32,12 @@ - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil { self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]; if (self) { +#ifdef PGDROID + self.webView = [[UIWebView alloc] init]; + self.webView.delegate = self; + self.webView.dataDetectorTypes = UIDataDetectorTypeLink; + self.webView.scalesPageToFit = YES; +#else self.webView = [[WKWebView alloc] init]; self.webView.navigationDelegate = self; self.webView.configuration.dataDetectorTypes = UIDataDetectorTypeLink; @@ -30,6 +45,7 @@ - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil #if !TARGET_OS_IPHONE self.webView.allowsMagnification = YES; #endif +#endif } return self; } @@ -73,7 +89,24 @@ - (void)copyButtonTapped:(id)sender [[UIPasteboard generalPasteboard] setString:self.originalText]; } +#ifdef PGDROID +#pragma mark - UIWebView Delegate +- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType +{ + BOOL shouldStart = NO; + if (navigationType == UIWebViewNavigationTypeOther) { + // Allow the initial load + shouldStart = YES; + } 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. + FLEXWebViewController *webVC = [[[self class] alloc] initWithURL:[request URL]]; + [self.navigationController pushViewController:webVC animated:YES]; + } + return shouldStart; +} +#else #pragma mark - WKNavigationDelegate Delegate - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigationAction *)navigationAction decisionHandler:(void (^)(WKNavigationActionPolicy))decisionHandler { @@ -88,6 +121,7 @@ - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigati decisionHandler(WKNavigationActionPolicyCancel); } } +#endif #pragma mark - Class Helpers From 418506bbcdc46f241aadf7a0bb62432b9d24f401 Mon Sep 17 00:00:00 2001 From: Xiaohan Wang Date: Thu, 12 Dec 2019 16:04:45 -0800 Subject: [PATCH 3/3] [NAR-47277] add WKUIDelegate and implement createWebViewWithConfiguration method when target=_blank in js code, which needs to create a new webview --- .../Global State Explorers/FLEXWebViewController.m | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/Classes/Global State Explorers/FLEXWebViewController.m b/Classes/Global State Explorers/FLEXWebViewController.m index 90f7ee341a..efae340647 100644 --- a/Classes/Global State Explorers/FLEXWebViewController.m +++ b/Classes/Global State Explorers/FLEXWebViewController.m @@ -18,7 +18,7 @@ @interface FLEXWebViewController () @property (nonatomic, strong) UIWebView *webView; #else -@interface FLEXWebViewController () +@interface FLEXWebViewController () @property (nonatomic, strong) WKWebView *webView; #endif @@ -39,6 +39,7 @@ - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil self.webView.scalesPageToFit = YES; #else self.webView = [[WKWebView alloc] init]; + self.webView.UIDelegate = self; self.webView.navigationDelegate = self; self.webView.configuration.dataDetectorTypes = UIDataDetectorTypeLink; self.webView.contentMode = UIViewContentModeScaleToFill; @@ -121,6 +122,14 @@ - (void)webView:(WKWebView *)webView decidePolicyForNavigationAction:(WKNavigati decisionHandler(WKNavigationActionPolicyCancel); } } + +#pragma mark - WKUIDelegate Delegate +- (WKWebView *)webView:(WKWebView *)webView createWebViewWithConfiguration:(WKWebViewConfiguration *)configuration forNavigationAction:(WKNavigationAction *)navigationAction windowFeatures:(WKWindowFeatures *)windowFeatures { + if (!navigationAction.targetFrame.isMainFrame) { + [webView loadRequest:navigationAction.request]; + } + return nil; +} #endif