Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for WKWebView ... FINALLY #819

Merged
merged 3 commits into from
Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
78 changes: 78 additions & 0 deletions lib/ProMotion/web/ui_web_screen_module.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
module ProMotion
module UIWebScreenModule
def web_view_setup
self.webview = add UIWebView.new, {
frame: CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height),
delegate: self,
data_detector_types: data_detector_types
}

self.webview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight
self.webview.scalesPageToFit = self.scale_to_fit
self.webview.scrollView.decelerationRate = UIScrollViewDecelerationRateNormal
end

def evaluate(js)
self.webview.stringByEvaluatingJavaScriptFromString(js)
end

def evaluate_async(js, &block)
Dispatch::Queue.concurrent.async do
result = evaluate(js)
Dispatch::Queue.main.async do
block.call result
end
end
end

def go_to_item(item)
# self.webview.goToBackForwardListItem(item)
PM.logger.warn "`go_to_item` is not implemented with the older UIWebView, which doesn't support it."
false
end

def back_forward_list
# self.webview.backForwardList
PM.logger.warn "`back_forward_list` is not implemented with the older UIWebView, which doesn't support it."
false
end

def progress
# self.webview.estimatedProgress
PM.logger.warn "`progress` is not implemented with the older UIWebView, which doesn't support it."
false
end

# CocoaTouch methods

def webView(in_web, shouldStartLoadWithRequest:in_request, navigationType:in_type)
if %w(http https).include?(in_request.URL.scheme)
if self.external_links == true && in_type == UIWebViewNavigationTypeLinkClicked
if defined?(OpenInChromeController)
open_in_chrome in_request
else
open_in_safari in_request
end
return false # don't allow the web view to load the link.
end
end

load_request_enable = true #return true on default for local file loading.
load_request_enable = !!on_request(in_request, in_type) if self.respond_to?(:on_request)
load_request_enable
end

def webViewDidStartLoad(webView)
load_started if self.respond_to?(:load_started)
end

def webViewDidFinishLoad(webView)
load_finished if self.respond_to?(:load_finished)
end

def webView(webView, didFailLoadWithError:error)
load_failed(error) if self.respond_to?("load_failed:")
end

end
end
53 changes: 3 additions & 50 deletions lib/ProMotion/web/web_screen_module.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
module ProMotion
module WebScreenModule
include WKWebScreenModule if defined?(WKWebView)
include UIWebScreenModule unless defined?(WKWebView)

attr_accessor :webview, :external_links, :detector_types, :scale_to_fit

Expand All @@ -17,17 +19,6 @@ def on_init
# TODO: Remove in 3.0
end

def web_view_setup
self.webview ||= add UIWebView.new, {
frame: CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height),
delegate: self,
data_detector_types: data_detector_types
}
self.webview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight
self.webview.scalesPageToFit = self.scale_to_fit
self.webview.scrollView.decelerationRate = UIScrollViewDecelerationRateNormal
end

def web
self.webview
end
Expand Down Expand Up @@ -58,10 +49,7 @@ def set_content(content)
end

def open_url(url)
request = NSURLRequest.requestWithURL(
url.is_a?(NSURL) ? url : NSURL.URLWithString(url)
)
web.loadRequest request
web.loadRequest NSURLRequest.requestWithURL(url.to_url)
end

def convert_retina_images(content)
Expand Down Expand Up @@ -89,10 +77,6 @@ def html
evaluate("document.documentElement.outerHTML")
end

def evaluate(js)
self.webview.stringByEvaluatingJavaScriptFromString(js)
end

def current_url
evaluate('document.URL')
end
Expand All @@ -119,36 +103,6 @@ def open_in_safari(in_request)
UIApplication.sharedApplication.openURL(in_request.URL)
end

# UIWebViewDelegate Methods - Camelcase
def webView(in_web, shouldStartLoadWithRequest:in_request, navigationType:in_type)
if %w(http https).include?(in_request.URL.scheme)
if self.external_links == true && in_type == UIWebViewNavigationTypeLinkClicked
if defined?(OpenInChromeController)
open_in_chrome in_request
else
open_in_safari in_request
end
return false # don't allow the web view to load the link.
end
end

load_request_enable = true #return true on default for local file loading.
load_request_enable = !!on_request(in_request, in_type) if self.respond_to?(:on_request)
load_request_enable
end

def webViewDidStartLoad(webView)
load_started if self.respond_to?(:load_started)
end

def webViewDidFinishLoad(webView)
load_finished if self.respond_to?(:load_finished)
end

def webView(webView, didFailLoadWithError:error)
load_failed(error) if self.respond_to?("load_failed:")
end

protected

def data_detector_types
Expand All @@ -166,6 +120,5 @@ def map_detector_symbol(symbol)
all: UIDataDetectorTypeAll
}[symbol] || UIDataDetectorTypeNone
end

end
end
61 changes: 61 additions & 0 deletions lib/ProMotion/web/wk_web_screen_module.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
module ProMotion
module WKWebScreenModule
def web_view_setup
self.webview = add WKWebView.new, {
frame: CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height),
data_detector_types: data_detector_types
}

self.webview.UIDelegate = self
self.webview.navigationDelegate = self
self.webview.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight
self.webview.scrollView.decelerationRate = UIScrollViewDecelerationRateNormal
end

# `evaluate` will wait to return its payload
# `evaluate_async` requires a block
def evaluate(js)
res = nil
semaphore = Dispatch::Semaphore.new(0)
evaluate_async do |result, error|
res = result
semaphore.signal
end
semaphore.wait(Dispatch::TIME_FOREVER)
return res
end

def evaluate_async(js, &block)
self.webview.evaluateJavaScript(js, completionHandler: -> (result, error) {
block.call(result, error)
})
end

def go_to_item(item)
self.webview.goToBackForwardListItem(item)
end

def back_forward_list
self.webview.backForwardList
end

def progress
self.webview.estimatedProgress
end

# CocoaTouch methods

def webView(view, didCommitNavigation: navigation)
navigation_started(navigation) if self.respond_to?("navigation_started")
end

def webView(view, didFailNavigation: navigation, withError: error)
navigation_failed(navigation, error) if self.respond_to?("navigation_failed")
end

def webView(view, didFinishNavigation: navigation)
navigation_finished(navigation) if self.respond_to?("navigation_finished")
end

end
end