I am new to Objective-C, as I'm from android background. I implemented a functionality in android, which is now needed to be implemented in IOS.
I have a webview which opens up a webpage. User clicks the facebook login button on there which fires up popup as a new webchromeclient within the webview. Once login, I replace the webview with original one and capture the cookies. Page automatically determines user's login status and opens up user's profile page.
Here is my layout for activity:
<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>
And this is 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");
}
}
}
This code is needs to be written in Objective-C but I am not sure how to override url and replace webview. I tried creating two separate webviews, but after login with facebook, I can't return back to original webview and read cookies.
Can someone help?