Android WebView for Facebook Like Button - android

I'm trying to make facebook like functionality in Android WebView (project specification does not allow browser opening, or any out of application activity).
So, restrictions are that it has to be done in WebView. I've managed to make it a dialog, and apon user's click like button, it (the WebView) redirects successfully (in the same view) to facebooks login page. After successful authentication, the WebView (in a dialog) is redirected to blank page with facebook header.
Interestingly enough, when user leaves the blank dialog and click again on the like button it works like perfectly (like and unlike) - it somehow keeps authentication active. To resolve the blank page, I've tried/used following:
using WebViewClient and shouldOverloadUrlForwarding to keep whole process in same WebView dialog.
using WebChromeClient to properly execute JavaScript - without it after login is not possible to like/unlike.
tried using setUserAgentString() to simulate other browsers like Chrome or Firefox
tried the SSL Error certificate handling (in API level 8) (at WebViewClient)
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
using (and all possible combination of these)
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
Tried also persisting cookies with CookieSyncManager, CookieManager and manually handling.
All of this was with no result. I really appreciate any help!

To get past the blank page you do this:
webview.setWebViewClient(new LikeWebviewClient(this));
private class LikeWebviewClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
Log.d(TAG, "onPageFinished url: " +url);
// Facebook redirects to this url once a user has logged in, this is a blank page so we override this
// http://www.facebook.com/connect/connect_to_external_page_widget_loggedin.php?............
if(url.startsWith("http://www.facebook.com/connect/connect_to_external_page_widget_loggedin.php")){
String redirectUrl = getFacebookLikeUrl();
view.loadUrl(redirectUrl);
return;
}
super.onPageFinished(view, url);
}
}

I had the same issue on my android application. The cause of the issue is FB login javascript opens a new page on a new window. Then it tries to close it after login success. Please follow flowing example from my working codes.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0099cc"
tools:context=".MyActivity"
android:id="#+id/webview_frame">
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
The Webview of id "webview" is the main view for my content. Below is my activity codes.
public class MyActivity extends Activity {
/* URL saved to be loaded after fb login */
private static final String target_url="http://www.example.com";
private static final String target_url_prefix="www.example.com";
private Context mContext;
private WebView mWebview;
private WebView mWebviewPop;
private FrameLayout mContainer;
private long mLastBackPressTime = 0;
private Toast mToast;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_urimalo);
// final View controlsView =
// findViewById(R.id.fullscreen_content_controls);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
mWebview = (WebView) findViewById(R.id.webview);
//mWebviewPop = (WebView) findViewById(R.id.webviewPop);
mContainer = (FrameLayout) findViewById(R.id.webview_frame);
WebSettings webSettings = mWebview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAppCacheEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setSupportMultipleWindows(true);
mWebview.setWebViewClient(new UriWebViewClient());
mWebview.setWebChromeClient(new UriChromeClient());
mWebview.loadUrl(target_url);
mContext=this.getApplicationContext();
}
private class UriWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String host = Uri.parse(url).getHost();
//Log.d("shouldOverrideUrlLoading", url);
if (host.equals(target_url_prefix))
{
// This is my web site, so do not override; let my WebView load
// the page
if(mWebviewPop!=null)
{
mWebviewPop.setVisibility(View.GONE);
mContainer.removeView(mWebviewPop);
mWebviewPop=null;
}
return false;
}
if(host.equals("m.facebook.com"))
{
return false;
}
// Otherwise, the link is not for a page on my site, so launch
// another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler,
SslError error) {
Log.d("onReceivedSslError", "onReceivedSslError");
//super.onReceivedSslError(view, handler, error);
}
}
class UriChromeClient extends WebChromeClient {
#Override
public boolean onCreateWindow(WebView view, boolean isDialog,
boolean isUserGesture, Message resultMsg) {
mWebviewPop = new WebView(mContext);
mWebviewPop.setVerticalScrollBarEnabled(false);
mWebviewPop.setHorizontalScrollBarEnabled(false);
mWebviewPop.setWebViewClient(new UriWebViewClient());
mWebviewPop.getSettings().setJavaScriptEnabled(true);
mWebviewPop.getSettings().setSavePassword(false);
mWebviewPop.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
mContainer.addView(mWebviewPop);
WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(mWebviewPop);
resultMsg.sendToTarget();
return true;
}
#Override
public void onCloseWindow(WebView window) {
Log.d("onCloseWindow", "called");
}
}
}
The key for this issue is onCreateWindow. A new window is created and inserted to the frame layout and removed upon success. I added the removal at shouldOverrideUrlLoading.

I had to work through this almost exact same problem on iPhone. What I had to do was to intercept the request that the webview makes to the 'blank page' you described above, and instead tell the webview to load the like URL.

Didn't worked for me:(, but form observing i perceive that wrong redirected link started with
url.startsWith("http://m.facebook.com/a/profile.php?fan&id"))

Related

Android WebView Facebook Login (popup/redirection issues)

how is everyone?
I am working on a simple mobile application utilizing WebView. My website utilizes a layout giving it the appearance of a native app. I also allow users to login with their facebook account into my website. Here are my scenarios
** WORKS ** From PC/Chrome: Can access the mobile site URL, login with facebook which opens the pop-up dialog to either A.) Login, or B.) if user is FB logged in, prompt user to accept
** WORKS ** From Phone/Chrome: Can access the mobile site URL, login with facebook which opens a new tab with A.) Login, or B.) if user is FB logged in, prompt user to accept
** PROBLEM! ** From Phone/APK: Loads website in WebView, click login with facebook, opens facebook login page in the webview, but once I enter details and attempt to login, nothing happens. If I go to my home screen and reopen the app, it logs me in using the details I previously submitted
So basically what I am saying is that in the APP, once I leave my website URL and go to facebook's website to enter facebook details to login with it and press login -- it doesn't redirect back to my website for some reason. I've been plucking my hairs trying to find a solution to this, I've also searched extensively on here and other places but have not been able to find a workable solution, I'm sure many other people may be experiencing similar issues with facebook or other services
Here is my code (was originally much more simple, however I am now utilizing what I found in another topic covering this issue, however it still does not work -- I'd also like to mention that with this portion of code that I found, when TARGET_URL is loaded from a phone, the phone will redirect it to the mobile version of the website, which, since it's how the code functions, causes it to open up in a browser instead of my WebView)
I'm totally confused =/
public class MainActivity extends Activity {
/* URL saved to be loaded after fb login */
private static final String target_url="http://www.moneygirlsmusic.tv/app/index.php?do=/mobile/";
private static final String target_url_prefix="www.moneygirlsmusic.tv/app/index.php?do=/mobile/";
private Context mContext;
private WebView mWebview;
private WebView mWebviewPop;
private FrameLayout mContainer;
private long mLastBackPressTime = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// final View controlsView =
// findViewById(R.id.fullscreen_content_controls);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
mWebview = (WebView) findViewById(R.id.webview);
//mWebviewPop = (WebView) findViewById(R.id.webviewPop);
mContainer = (FrameLayout) findViewById(R.id.webview_frame);
WebSettings webSettings = mWebview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAppCacheEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setSupportMultipleWindows(true);
mWebview.setWebViewClient(new UriWebViewClient());
mWebview.setWebChromeClient(new UriChromeClient());
mWebview.loadUrl(target_url);
mContext=this.getApplicationContext();
}
private class UriWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String host = Uri.parse(url).getHost();
Log.d("shouldOverrideUrlLoading", url);
if (host.equals(target_url_prefix))
{
// This is my web site, so do not override; let my WebView load
// the page
if(mWebviewPop!=null)
{
mWebviewPop.setVisibility(View.GONE);
mContainer.removeView(mWebviewPop);
mWebviewPop=null;
}
return false;
}
if(host.equals("m.facebook.com") || host.equals("www.facebook.com"))
{
return false;
}
// Otherwise, the link is not for a page on my site, so launch
// another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler,
SslError error) {
Log.d("onReceivedSslError", "onReceivedSslError");
//super.onReceivedSslError(view, handler, error);
}
}
class UriChromeClient extends WebChromeClient {
#Override
public boolean onCreateWindow(WebView view, boolean isDialog,
boolean isUserGesture, Message resultMsg) {
mWebviewPop = new WebView(mContext);
mWebviewPop.setVerticalScrollBarEnabled(false);
mWebviewPop.setHorizontalScrollBarEnabled(false);
mWebviewPop.setWebViewClient(new UriWebViewClient());
mWebviewPop.getSettings().setJavaScriptEnabled(true);
mWebviewPop.getSettings().setSavePassword(false);
mWebviewPop.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
mContainer.addView(mWebviewPop);
WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(mWebviewPop);
resultMsg.sendToTarget();
return true;
}
#Override
public void onCloseWindow(WebView window) {
Log.d("onCloseWindow", "called");
}
}
I forgot to include my previous resources used to try to figure this out
Making facebook login work with an Android Webview
Making facebook login work with an Android Webview
Android WebView for Facebook Like Button
Android WebView for Facebook Like Button
In android 6 running in some mobiles you should add this to your java code to redirect . otherwise it doesn't redirect and nothing happens .
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});

Flash is not loading in Web view in Android

I am trying to Load a webview in Android. Where web page have a swf file but it is not loading. I know there are many question and answer on same topic but no one helped me.
I have already targeted it version 11. And I have put android:hardwareAccelerated="true" too in manifest.xml file.
My code is here-
public class MainActivity extends Activity {
private WebView mWebView;
String url="http://mypages.com/Ch_001/ScoPage.html";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.webView);
mWebView.setKeepScreenOn(true);
WebSettings settings = mWebView.getSettings();
settings.setPluginState(PluginState.ON);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.setInitialScale(100);
// wbView.getSettings().setUseWideViewPort(true);
mWebView.setScrollBarStyle(View.SCROLLBARS_OUTSIDE_OVERLAY);
mWebView.setWebViewClient(new MyWebViewClient());
}
private class MyWebViewClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler,
SslError error) {
Log.e("Error VAGARO", error.toString());
handler.proceed();
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return false;
}
}
}
After one week struggle I got a Flash player from Macromedia, which works for me.
You can search in Google for-"Flash Player 11.1.for Android 4.0 - Macromedia - Adobe" or install APK from this link.
Install this apk in your Android device.
And when you will open it, it will ask for choose default browser.
Choose any browser which support Flash (UC-Browser, Chrome etc).
Now run your any application it will load your Flash video.
Add this code:
webview.getSettings().setPluginsEnabled(true);
add mWebView.loadUrl(url);in onCreate

WebView shows Choose Action Dialog, does not navigate

Given the following code, the WebView will not navigate to and display google.com but instead a Choose Action Dialog will pop up with all installed browser apps to choose from.
Is this intended and is there a fix for this imo weird behaviour?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
WebView webview = new WebView(this);
setContentView(webview);
WebSettings settings = webview.getSettings();
settings.setJavaScriptEnabled(true);
webview.loadUrl("http://www.google.com/");
}
See Clicking URLs opens default browser
Essentially, you'll have to provide your own WebClient and override shouldOverrideUrlLoading
private class CustomWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}

Blank empty page in android webview after facebook login using javascript

i am developing a new android application,and using a webview to view my website in one the website pages there is a link for login with facebook
after logging directly, it returns a blank and embty webview
i have seen couple of topics here regarding the same issue but it is not exactly like mine
please note that if i am using normal desktop browser the issue is not there
I assume that there is a property of the webview should be adjusted , below my webview code
webView1=(WebView)findViewById(R.id.webView1);
webView1.setWebViewClient(new MyWebViewClient());
WebView webView1 = (WebView) findViewById(R.id.webView1);
webView1.setWebChromeClient(new WebChromeClient());
webView1.getSettings().setAppCacheEnabled(true);
webView1.getSettings().setDatabaseEnabled(true);
webView1.getSettings().setDomStorageEnabled(true);
webView1.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView1.getSettings().setGeolocationEnabled(true);
webView1.loadUrl("http://www.myactivepoint.com/mobile/maplp.asp");
i really approciate your support
I had a similar experience with you. I solved the problem by the following methods.
public class CustomWebViewClient extends WebViewClient{
#Override
public void onPageFinished(WebView view, String url) {
if(url.startsWith("https://m.facebook.com/dialog/oauth")){
String redirectUrl = "http://www.mydomain.com/myReturnUrl";
view.loadUrl(redirectUrl);
return;
}
super.onPageFinished(view, url);
}}
and just add it to your WebView
webview.setWebViewClient(new CustomWebViewClient());
This is my solution. After successfull login you have to redirect the page. You can get the address from refsrc parameter in the Facebook URL, but sometimes (e.g. next try to log in) refsrc is missing so I use domain parameter. You can override the URL address if you need. See the code:
webView.setWebViewClient(new WebViewClient()
{
#Override
public void onPageFinished(final WebView view, final String url)
{
if(getActivity()!=null)
{
if(url.contains("facebook.com") && url.contains("dialog/oauth") &&
(url.contains("&refsrc") || url.contains("&domain")))
{
Uri uri = Uri.parse(url);
String callbackUrl = uri.getQueryParameter("refsrc");
if(callbackUrl==null)
callbackUrl = "http://" + uri.getQueryParameter("domain");
view.loadUrl(callbackUrl); // callback URL
}
}
}
}

Android fb like button in WebView [duplicate]

I'm trying to make facebook like functionality in Android WebView (project specification does not allow browser opening, or any out of application activity).
So, restrictions are that it has to be done in WebView. I've managed to make it a dialog, and apon user's click like button, it (the WebView) redirects successfully (in the same view) to facebooks login page. After successful authentication, the WebView (in a dialog) is redirected to blank page with facebook header.
Interestingly enough, when user leaves the blank dialog and click again on the like button it works like perfectly (like and unlike) - it somehow keeps authentication active. To resolve the blank page, I've tried/used following:
using WebViewClient and shouldOverloadUrlForwarding to keep whole process in same WebView dialog.
using WebChromeClient to properly execute JavaScript - without it after login is not possible to like/unlike.
tried using setUserAgentString() to simulate other browsers like Chrome or Firefox
tried the SSL Error certificate handling (in API level 8) (at WebViewClient)
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed();
}
using (and all possible combination of these)
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
Tried also persisting cookies with CookieSyncManager, CookieManager and manually handling.
All of this was with no result. I really appreciate any help!
To get past the blank page you do this:
webview.setWebViewClient(new LikeWebviewClient(this));
private class LikeWebviewClient extends WebViewClient {
#Override
public void onPageFinished(WebView view, String url) {
Log.d(TAG, "onPageFinished url: " +url);
// Facebook redirects to this url once a user has logged in, this is a blank page so we override this
// http://www.facebook.com/connect/connect_to_external_page_widget_loggedin.php?............
if(url.startsWith("http://www.facebook.com/connect/connect_to_external_page_widget_loggedin.php")){
String redirectUrl = getFacebookLikeUrl();
view.loadUrl(redirectUrl);
return;
}
super.onPageFinished(view, url);
}
}
I had the same issue on my android application. The cause of the issue is FB login javascript opens a new page on a new window. Then it tries to close it after login success. Please follow flowing example from my working codes.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0099cc"
tools:context=".MyActivity"
android:id="#+id/webview_frame">
<WebView
android:id="#+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
The Webview of id "webview" is the main view for my content. Below is my activity codes.
public class MyActivity extends Activity {
/* URL saved to be loaded after fb login */
private static final String target_url="http://www.example.com";
private static final String target_url_prefix="www.example.com";
private Context mContext;
private WebView mWebview;
private WebView mWebviewPop;
private FrameLayout mContainer;
private long mLastBackPressTime = 0;
private Toast mToast;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_urimalo);
// final View controlsView =
// findViewById(R.id.fullscreen_content_controls);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
mWebview = (WebView) findViewById(R.id.webview);
//mWebviewPop = (WebView) findViewById(R.id.webviewPop);
mContainer = (FrameLayout) findViewById(R.id.webview_frame);
WebSettings webSettings = mWebview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAppCacheEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setSupportMultipleWindows(true);
mWebview.setWebViewClient(new UriWebViewClient());
mWebview.setWebChromeClient(new UriChromeClient());
mWebview.loadUrl(target_url);
mContext=this.getApplicationContext();
}
private class UriWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String host = Uri.parse(url).getHost();
//Log.d("shouldOverrideUrlLoading", url);
if (host.equals(target_url_prefix))
{
// This is my web site, so do not override; let my WebView load
// the page
if(mWebviewPop!=null)
{
mWebviewPop.setVisibility(View.GONE);
mContainer.removeView(mWebviewPop);
mWebviewPop=null;
}
return false;
}
if(host.equals("m.facebook.com"))
{
return false;
}
// Otherwise, the link is not for a page on my site, so launch
// another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler,
SslError error) {
Log.d("onReceivedSslError", "onReceivedSslError");
//super.onReceivedSslError(view, handler, error);
}
}
class UriChromeClient extends WebChromeClient {
#Override
public boolean onCreateWindow(WebView view, boolean isDialog,
boolean isUserGesture, Message resultMsg) {
mWebviewPop = new WebView(mContext);
mWebviewPop.setVerticalScrollBarEnabled(false);
mWebviewPop.setHorizontalScrollBarEnabled(false);
mWebviewPop.setWebViewClient(new UriWebViewClient());
mWebviewPop.getSettings().setJavaScriptEnabled(true);
mWebviewPop.getSettings().setSavePassword(false);
mWebviewPop.setLayoutParams(new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
mContainer.addView(mWebviewPop);
WebView.WebViewTransport transport = (WebView.WebViewTransport) resultMsg.obj;
transport.setWebView(mWebviewPop);
resultMsg.sendToTarget();
return true;
}
#Override
public void onCloseWindow(WebView window) {
Log.d("onCloseWindow", "called");
}
}
}
The key for this issue is onCreateWindow. A new window is created and inserted to the frame layout and removed upon success. I added the removal at shouldOverrideUrlLoading.
I had to work through this almost exact same problem on iPhone. What I had to do was to intercept the request that the webview makes to the 'blank page' you described above, and instead tell the webview to load the like URL.
Didn't worked for me:(, but form observing i perceive that wrong redirected link started with
url.startsWith("http://m.facebook.com/a/profile.php?fan&id"))

Categories

Resources