-3
我是Objective-C的新手,因爲我來自android背景。我在android中實現了一個功能,現在需要在IOS中實現。Objective-C - 在webview中打開webview
我有一個webview,它打開了一個網頁。用戶點擊那裏的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>
這是MyActivity:
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。我嘗試創建兩個獨立的網頁瀏覽,但是在登錄Facebook後,我無法返回到原始webview並閱讀cookie。
有人可以幫忙嗎?
什麼是我收到這麼多downvotes? – ashutosh