Android: Get the clicked url that opened the application - android

I have App Links setup which routes the specific urls into the application.
How can I determine what the url of the last clicked url that opened the app was?
The issue I'm running into is if multiple listeners for onResume are registered (IE I open the app from different sources a url click from Safari and messages), I get all of the previously clicked urls called.
public void onActivityResumed(Activity activity) {
String currentLink = activity.getIntent().getDataString();
}
Essentially I'm looking for the Android equivalent of the following iOS code to get the url which opened up the app.
- (BOOL)application:(UIApplication *)application continueUserActivity:(NSUserActivity *)userActivity restorationHandler:(void (^)(NSArray * _Nullable))restorationHandler {
NSURL *clickedURL = userActivity.webpageURL;
}

Related

How to verify user is authenticated entire app Xamarin Form

I'm new with Xamarin form, my question is how can I check user is logged in for entire app, example every time when users go to a new page, it checks for authent. I tried successfully for every page check for auth but is there any another ways to do it? I did some research on internet and some said that i have to auth on the App.cs on OnStart() but the event is not raise when i go to the next page, it starts only when user open the app.
Here is my code, I'm using Google auth.
On the logged in page (HomePage):
public HomePage(NetworkAuthData networkAuthData)
{
if (string.IsNullOrEmpty(networkAuthData.Id))
{
//Always require user authentication
//Application.Current.MainPage.Navigation.PopAsync();
Application.Current.MainPage = new NavigationPage(new SocialLoginPage(oAuth2Service));
}
else
{
BindingContext = networkAuthData;
InitializeComponent();
}
}
It works but when i move these code to app.cs on OnStart() it just run once when opened the app.

Detect when URL changes in WebView - Xamarin

It is my first time working with Xamarin, and need to show up a back button on the screen only when the URL of my WebView is different from the first one, so that the users can go back to the "home" page of the web site. If there is any function that gets called once the page is completely loaded so I can get the URL and compare with the previos one would be great. Thanks you!
You could get the url from the event webView.Navigated
webView.Navigated += WebView_Navigated;
private void WebView_Navigated(object sender, WebNavigatedEventArgs e)
{
var url = e.Url;
//...
}

How can I return an app to the foreground, considering the new security changes in API 29?

I have an app that makes an http request via the localhost to a separate, third-party app which I do not control, and waits for a response from that call before continuing. The workflow goes like this:
User is inside my app
User presses a button, which launches and calls out to the third-party application
User interacts with the third-party application
When the third-party application finishes its work, my app picks up the completed http response, and pulls itself back to the forefront via MoveTaskToFront for the user to continue working.
This functions properly in Android 9 and below, but the last step does not work in Android 10, I believe due to the new restrictions on launching activities from the background.
I have no control over the third-party app, so I cannot modify it to close itself when finished working, or request that the calling app be returned to the foreground when appropriate. Does anyone know of a workaround for this?
Edit: as requested, I've added the code snippet with the call out. This is a Xamarin project, so it's written in C#, but this particular code section is Android-platform-specific, so I am able to make Android system calls.
First I have to bring up the third-party app:
Intent intent = CrossCurrentActivity.Current.AppContext.PackageManager.GetLaunchIntentForPackage("com.bbpos.android.tsys");
if (intent != null)
{
// We found the activity now start the activity
intent.AddFlags(ActivityFlags.ClearTask);
CrossCurrentActivity.Current.AppContext.StartActivity(intent);
}
Then I call into it via the localhost, process the response, and want to switch back to my app.
using (var client = new HttpClient())
{
// by calling .Result we're forcing synchronicity
var response = client.GetAsync("http://127.0.0.1:8080/v2/pos?TransportKey=" + pTransportKey + "&Format=JSON").Result;
if (response.IsSuccessStatusCode)
{
var responseContent = response.Content;
// as above, forcing synchronicity
string responseString = responseContent.ReadAsStringAsync().Result;
var result = JsonConvert.DeserializeObject<GeniusTransactionResponse>(responseString);
var manager = (ActivityManager)Application.Context.GetSystemService(Context.ActivityService);
var test = manager.AppTasks.First().TaskInfo.Id;
manager.AppTasks.First().MoveToFront();
//manager.MoveTaskToFront(CrossCurrentActivity.Current.Activity.TaskId, 0);
return result;
}
else
{
return null;
}
}
Quick update in case anyone else has this same issue: I was able to work around this by adding an Accessibility Service to the project. Simply having an Accessibility Service registered and enabled by the user allows MoveTaskToFront to function as it did in APIs <29; the actual service doesn't need to do anything.

Android WebView with AngularJS application route changes not being detected in shouldOverrideUrlLoading

I have been working on a hybrid android application. Currently a WebView in our application is pointing to an AngularJS 1.5.7 application. When the user hits a button inside of the application that changes the route I was expecting the shouldOverrideUrlLoading function to be called inside of my WebViewClient. However, this is not the case. It looks like shouldOverrideUrlLoading does not get hit on Angualar route changes.
This being the case I have gone down the following rabbit holes:
onPageFinished - Overriding this function in the WebViewClient works, however, it is not being called until after the new route is getting hit. Which is adding to the application loading time and creating a choppy experience. ` #Override
public void onPageFinished(WebView view, String url) {
if (url.endsWith("/#/")) {
signOut();
} else if (url.endsWith("/login")) {
// TODO: show some sort of failure message?
Log.i("Login Route", "The webview just attempted to go to the login route.");
signOut();
} else if (url.endsWith("/security")) {
Intent intent = new Intent(getApplicationContext(), SecurityActivity.class);
startActivity(intent);
}
}`
shouldInterceptRequest - Overriding this function allows you to watch for requests. However, by the time the requests go out from the AngularJS application the web view is showing a new route once again providing a choppy user experience.
onLoadResource - same
JavaScriptInterface - Currently I have set up a JavaScript interface to watch for window.location changes. This seems to catch the route changes quicker than any of the above options, however, there is still a glimpse quick flicker of the web page I do not want to do go to. You can find how to do Javascript bridging on this post
Any suggestions would be greatly appreciated! Thanks.

Cookie not saving in Web view in Xamarin(Android)

I am creating an android application in xamarin. I am using a web view to display a website. After user login website create a cookie and should store in web view. There are two web view in app, One web view is displaying the pages and if there is any text box in the page that page is opened in second web view.
So now when user tries to login, second web view is opened(as login page contains text box), after user enter details and click next button, the second view is closed and next page is opened in first web view. After login a cookie is created and stored in web view and when user open the app next time its doesn't asks for login. This is what should happen.
The problem is, if the user enter details and after clicks the next button(next page is loading in first web view) and immediately quits the application then start app again then cookies does not exists and app asks for login again.
After login I am reading the cookie value on page finish event of webview and displaying in toast. If i quits the app after login, i gets the cookie value in toast but when i starts the app again the cookie doesn't exists anymore and it asks me for login again
public override void OnPageFinished (WebView view, string url)
{
try
{
if (view.Url == Urls.URL_INDEX)
{
var cookieManager = CookieManager.Instance;
if (cookieManager != null)
{
//getcookie string from the url
string cookie = cookieManager.GetCookie (view.Url);
if (!string.IsNullOrEmpty (cookie))
{
string[] cookies = cookie.Split (';');
foreach (var newcookie in cookies)
{
if (newcookie.Trim().StartsWith (Constants.COOKIE_NAME))
{
string cookieValue = newcookie.Substring (newcookie.IndexOf ('='));
Toast.MakeText(activity,cookieValue,ToastLength.Short).Show();
}
}
}
}
}
}
catch (Exception ex)
{
Console.WriteLine ("Exception in storing cookie in Home Activity : "+ex.Message);
Toast.MakeText (activity, "Exception : " + ex.Message,ToastLength.Long).Show();
}
}
I don't know why this is happening, please help.
The cookies are stored in RAM to get the best performance and synced every five minutes to the permanent storage. You'll need to manually force CookieSyncManager to sync the cookies in your OnPageFinished method so that they're still available when you start the application again. Refer to the CookieSyncManager documentation for more details.

Categories

Resources