Skip to content

Instantly share code, notes, and snippets.

@QuintinAdam
Last active December 3, 2015 22:19
Show Gist options
  • Save QuintinAdam/e6983f35829db1610688 to your computer and use it in GitHub Desktop.
Save QuintinAdam/e6983f35829db1610688 to your computer and use it in GitHub Desktop.
class HomeScreen < PMWebScreen
uses_action_bar false
title "Web View"
stylesheet HomeScreenStylesheet
def on_load
@current_url = 'https://www.tacobell.com'
open_url(@current_url)
end
def add_web_view
# call open_url to do this
# SPECIAL NOTE: Must create from activity, not from context (breaks dialogs)
@webview = Android::Webkit::WebView.new(find.activity)
append(@webview)
settings = @webview.settings
settings.savePassword = true
settings.saveFormData = true
settings.javaScriptEnabled = true
settings.supportZoom = false
# Make the back button the device go back a screen on the webview
@back_for_webview = true
# By default some URLs try to launch a browser. Very unlikely that this is
# the behavior we'll want in an app. So we use this to stop
@webview.webViewClient = MyWebViewClient.new
@webview.webChromeClient = MyWebChromeClient.new
accept_cookies
@webview
end
end
class MyWebViewClient < Android::Webkit::WebViewClient
ACTION_VIEW = "android.intent.action.VIEW"
def shouldOverrideUrlLoading(view, url)
if url.startsWith("tel:")
app.launch(tel: url)
elsif url.startsWith("https://www.google.com/maps")
app.launch(map: url)
elsif url.startsWith("mailto:")
email = url.gsub("mailto:", '')
app.launch(email: email)
else
view.loadUrl(url) if view
end
true # when return true, stop loading URL from happening
end
end
class MyWebChromeClient < Android::Webkit::WebChromeClient
# does not work for api version 16. 17 should work.
__annotation__('@android.webkit.JavascriptInterface')
def onJsAlert(view, url, message, result)
mp message
result.confirm
true
end
__annotation__('@android.webkit.JavascriptInterface')
def onGeolocationPermissionsShowPrompt(origin, callback)
mp origin
mp callback
callback.invoke(origin, true, false)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment