Facebook Like 按钮的 Android WebView
我正在尝试在 Android WebView 中创建类似 facebook 的功能(项目规范不允许打开浏览器,或任何应用程序外活动)。
因此,限制是它必须在 WebView 中完成。我已经设法将其设为一个对话框,并且当用户单击“类似”按钮时,它(WebView)成功重定向(在同一视图中)到facebook的登录页面。身份验证成功后,WebView
(在对话框中)将重定向到带有 facebook 标头的空白页面。
有趣的是,当用户离开空白对话框并再次单击“喜欢”按钮时,它的工作方式非常完美(喜欢和不喜欢) - 它以某种方式保持身份验证处于活动状态。为了解决空白页面,我尝试/使用了以下内容:
- 使用
WebViewClient
和shouldOverloadUrlForwarding
将整个过程保持在同一个WebView
对话框中。 - 使用
WebChromeClient
正确执行 JavaScript - 登录后没有它就不可能喜欢/不喜欢。 - 尝试使用
setUserAgentString()
模拟其他浏览器,例如 Chrome 或 Firefox 尝试了 SSL 错误证书处理(在 API 级别 8 中)(在
WebViewClient
处)<代码>@Override 公共无效onReceivedSslError(WebView视图,SslErrorHandler处理程序,SslError错误){ 处理程序.继续(); }
使用(以及这些的所有可能的组合)
webView.getSettings().setAppCacheEnabled(true); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
还尝试使用
CookieSyncManager
、CookieManager
保存 cookie 并手动处理。
这一切都没有结果。我真的很感谢任何帮助!
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
andshouldOverloadUrlForwarding
to keep whole process in sameWebView
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
要跳过空白页,请执行以下操作:
To get past the blank page you do this:
我的 Android 应用程序也遇到了同样的问题。问题的原因是FB登录javascript在新窗口中打开新页面。然后它会在登录成功后尝试关闭它。请遵循我的工作代码中的流动示例。
id“webview”的
Webview
是我的内容的主视图。以下是我的活动代码。这个问题的关键是
onCreateWindow
。将创建一个新窗口并将其插入到框架布局中,并在成功后将其删除。我在shouldOverrideUrlLoading
添加了删除操作。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.
The
Webview
of id "webview" is the main view for my content. Below is my activity codes.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 atshouldOverrideUrlLoading
.我必须在 iPhone 上解决这个几乎完全相同的问题。我要做的就是拦截 webview 对您上面描述的“空白页面”发出的请求,并告诉 webview 加载类似的 URL。
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