mWebView load certain links external - android-webview

Hi there Need help i need Certain links to load external and not thru Webview
for example my link www.sample.com and sub links internal and www.google.com external.
mWebView.setWebViewClient(new MyWebViewClient() {
#Override
public void onReceivedSslError(final WebView view, final SslErrorHandler handler, final SslError error) {
handler.proceed();
}
});
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
swipeRefreshLayout.setRefreshing(false);
}
});
Uri uri = getIntent().getData();
if(uri!=null)
{
String path = uri.toString();
Toast.makeText(MainActivity.this, "Path = "+path,Toast.LENGTH_LONG).show();
}
// REMOTE RESOURCE
mWebView.loadUrl(getString(R.string.web_url));
mWebView.setWebViewClient(new WebViewClient());

Related

What is friendUserId in Snapkit?

I'm connecting my app with bitmoji here, now i wanted to add Friendmoji also but In official documentation "friendUserId: the external ID of the friend user provided by the app" is mentioned but from where we get this external id is not properly specified! so what do i set for the friendUserId?
You need to load external id when userIsLoggedIn. Like this way:
if (SnapLogin.isUserLoggedIn(this)) {
loadExternalId();
}
private void loadExternalId() {
SnapLogin.fetchUserData(this, EXTERNAL_ID_QUERY, null, new FetchUserDataCallback() {
#Override
public void onSuccess(#Nullable UserDataResponse userDataResponse) {
if (userDataResponse == null || userDataResponse.hasError()) {
return;
}
mMyExternalId = userDataResponse.getData().getMe().getExternalId();
mFriendmojiToggle.setVisibility(View.VISIBLE);
}
#Override
public void onFailure(boolean isNetworkError, int statusCode) {
// handle error
}
});
}

Xamarin.Auth: Using Facebook oauth, how to redirect to my app?

I've just started using Xamarin.Auth and I want to enable Facebook login via oauth.
Here is my config:
public static string ClientId = "client id";
public static string ClientSecret = "client secret";
public static string Scope = "email";
public static string AuthorizeUrl = "https://m.facebook.com/dialog/oauth";
public static string RedirectUrl = "https://www.facebook.com/connect/login_success.html";
public static string AccessTokenUrl = "https://m.facebook.com/dialog/oauth/token";
Code for initiating the authentication:
public class AuthenticationPageRenderer : PageRenderer
{
public override void ViewDidAppear(bool animated)
{
base.ViewDidAppear (animated);
var auth = new OAuth2Authenticator (
Constants.ClientId,
Constants.ClientSecret,
Constants.Scope,
new Uri (Constants.AuthorizeUrl),
new Uri (Constants.RedirectUrl),
new Uri (Constants.AccessTokenUrl)
);
auth.Completed += OnAuthenticationCompleted;
PresentViewController (auth.GetUI (), true, null);
}
async void OnAuthenticationCompleted (object sender, AuthenticatorCompletedEventArgs e)
{
Debug.WriteLine ("AUTH Completed!");
if (e.IsAuthenticated) {
}
}
}
Seems to work fine, but instead of redirecting the user to https://www.facebook.com/connect/login_success.html, I want to redirect him back to my app again. Any help much appreciated!
Best,
Sascha
You can "redirect back" to your app again by simply invoking your own method to display the app's page you want to show to your user like this.
async void OnAuthenticationCompleted (object sender, AuthenticatorCompletedEventArgs e)
{
Debug.WriteLine ("AUTH Completed!");
if (e.IsAuthenticated) {
//invoke the method that display the app's page
//that you want to present to user
App.SuccessfulLoginAction.Invoke();
}
}
In your App.cs
public static Action SuccessfulLoginAction
{
get
{
return new Action(() =>
{
//show your app page
var masterDetailPage = Application.Current.MainPage as MasterDetailPage;
masterDetailPage.Detail = new NavigationPage((Page)Activator.CreateInstance(typeof(MainPage)));
masterDetailPage.IsPresented = false;
});
}
}
Assuming that MainPage is the page you wanted to show after successful login. I am using Xamarin.Forms with MasterDetailPage to display pages in my example which maybe different from your app but the concept is the same.
Just call DismissViewController (true, null) in your OnAuthenticationCompleted method. Or use the async equivalent:
async void OnAuthenticationCompleted(object sender, AuthenticatorCompletedEventArgs e)
{
Debug.WriteLine("AUTH Completed!");
await DismissViewControllerAsync(true);
if (e.IsAuthenticated)
{
}
}

Android WebView in a ViewSwitcher loadUrl loads once

I have in my ViewSwitcher a ListView and a WebView. In my ListView's adapter, I have an onclick listener that writes the clicked url in the list to sharedpreferences. I'm trying to load that url into the WebView using an onSharedPreferencesChangedListener.
This is the code in my adapter:
convertView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
viewSwitcher.showNext();
Settings.writeSettings(context, "webviewUrl",
urls.get(position));
}
});
return convertView;
And in the preference listener:
#Override
public void onSharedPreferenceChanged(SharedPreferences pref, String key) {
if (key.equals("webviewUrl")) {
Log.d("TAG", pref.getString(key, null));
WebView wv = (WebView) findViewById(R.id.rss_webview);
wv.getSettings().setJavaScriptEnabled(true);
wv.setWebViewClient(new MyWebViewClient());
wv.loadUrl("about:blank");
wv.loadUrl(pref.getString(key, null));
}
}
This works great except it only works once. The preference listener code logs the correct urls, and the code executes each time I want it to, but wv.loadUrl() method seems to do nothing after the first successful call. Can anyone explain to me why this is happening and perhaps offer a solution? Thanks.
I solved the problem by implementing a static ViewHolder on the WebView whose reference I needed to keep longer than its views' lifecycle.
private static final class WebViewHolder {
WebView wv;
}
#Override
public void onSharedPreferenceChanged(SharedPreferences pref, String key) {
WebViewHolder holder = new WebViewHolder();
if (key.equals("webviewUrl")) {
if (wv == null) {
wv = new WebView(this);
holder.wv = (WebView) findViewById(R.id.rss_webview);
holder.wv.getSettings().setJavaScriptEnabled(true);
holder.wv.setWebViewClient(new MyWebViewClient());
wv.setTag(holder);
} else {
holder = (WebViewHolder) wv.getTag();
}
holder.wv.loadUrl("about:blank");
holder.wv.loadUrl(pref.getString(key, null));
}
}

Android load url in internal WebView issue

I have a problem with a specific site when loading in my apps internal webview.
In the LogCat i get these 2 lines (the tag is "chromium").
[INFO:CONSOLE(25)] "Uncaught TypeError: Cannot call method 'getItem' of null", source: http://m.ynet.co.il/Default_Ynet.aspx?type=3&id=4519238 (25)
[INFO:CONSOLE(73)] "Uncaught TypeError: Cannot call method 'push' of undefined", source: http://m.ynet.co.il/Default_Ynet.aspx?type=3&id=4519238 (73)
If i redirect to the external browser all is fine. Also it happens only with the above site (see in logcat)
public class WebActivity extends Activity {
WebView wv;
final Activity activity = this;
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setProgressBarIndeterminateVisibility(true);
setContentView(R.layout.activity_web);
wv = (WebView)findViewById(R.id.webView1);
Intent intent = getIntent();
final String url = intent.getStringExtra("url");
Log.i("webView", url);
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setBuiltInZoomControls(true);
wv.getSettings().setLoadWithOverviewMode(true);
wv.getSettings().setUseWideViewPort(true);
wv.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress)
{
activity.setTitle("Loading...");
activity.setProgress(progress * 100);
if(progress == 100)
activity.setTitle(R.string.app_name);
}
});
wv.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
});
wv.loadUrl(url);
}
Any ideas?
It turns out that there was a problem using the shouldOverrideUrlLoading method (below) with this particular site.
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
Once this was deleted all was good...

How to create a simple android browser?

I simply want to create an apk that will take a url, and open a window and simply run like a browser
so far I have:
public class Browser extends Activity {
WebView mWebView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.loadUrl("http://www.google.com");
}
}
This works fine, except when i open a link it'll take me to the actual browser, I'm having trouble where to place this code to override links opening in a new browser:
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
WebView has a setWebViewClient method.
so you'd do something like
mWebView.setWebViewClient(new HelloWebViewClient());
Check out my project called FriarFramework, which is an ebook app publisher.
It basically takes a collection of HTML files locally and packages them up into a WebView.
https://github.com/hanchang/Friar-Framework
Try this
webView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
progressBar.setVisibility(View.GONE);
}
});
mWebView.setWebViewClient(new HelloWebViewClient());
This should be enough.
for more see the official doc about Building web apps in WebView
You have to implement WebViewClient.
You can detect URL inside shouldOverrideUrlLoading() method:
browser.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// You can have URL here
}
});