【发布时间】:2016-02-17 11:31:25
【问题描述】:
我是 Objective-C 的新手,因为我来自 android 背景。我在android中实现了一个功能,现在需要在IOS中实现。
我有一个打开网页的网页视图。用户单击那里的 facebook 登录按钮,该按钮会在 web 视图中作为新的 webchromeclient 弹出窗口。登录后,我将 webview 替换为原始 webview 并捕获 cookie。页面自动判断用户的登录状态并打开用户的个人资料页面。
这是我的活动布局:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<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 xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/webview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</FrameLayout>
</LinearLayout>
这是我的活动:
public class MyActivity extends Activity {
private static final String target_url="https://www.mywebpage.com/login";
private static final String target_url_prefix="www.mywebpage.com";
private static final String target_url_profile= "http://www.mywebpage.com/profile" ;
private WebView mWebview; //To Hold original page
private WebView mWebviewPop; //To hold facebook login popup
private FrameLayout mContainer; //Framecontainer to replace webviews
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myactivity);
mWebview = (WebView) findViewById(R.id.webview);
mContainer = (FrameLayout) findViewById(R.id.webview_frame);
//To check login status and other stuffs
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
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);
}
//
private class UriWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
String host = Uri.parse(url).getHost();
//If host is actually my webpage, remove popup and dont override
if (host.equals(target_url_prefix))
{
if(mWebviewPop!=null)
{
mWebviewPop.setVisibility(View.GONE);
mContainer.removeView(mWebviewPop);
mWebviewPop=null;
}
return false;
}
//If this is a facebook login popup, dont override
if(host.equals("m.facebook.com"))
{
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
return true;
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
String cookies = CookieManager.getInstance().getCookie(url); //Just reading cookies here
if(cookies != null && !cookies.isEmpty()) {
//Further logics with cookies
}
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
String cookies = CookieManager.getInstance().getCookie(url);
if(cookies != null && !cookies.isEmpty()) {
if (isUserloggedinFacebook(cookies)) { //Check user login status from cookies
if (mWebviewPop != null) {
mWebviewPop.setVisibility(View.GONE);
mContainer.removeView(mWebviewPop);
mWebviewPop = null;
}
mWebview.loadUrl(target_url_profile); //User is loggedin, load profile page
}
}
}
}
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");
}
}
}
此代码需要用 Objective-C 编写,但我不确定如何覆盖 url 并替换 webview。我尝试创建两个单独的 webview,但是在使用 facebook 登录后,我无法返回原始 webview 并读取 cookie。
有人可以帮忙吗?
【问题讨论】:
-
是什么让我收到这么多反对票?
标签: android ios objective-c facebook