0

I have a HybridWebView embedded in one of the Pages of my app. If I visit a website and sign in, the HybridWebView stores the relevant information and whenever I reopen the same website I am still signed in, even after closing and reopening the app.

But when I open the same website from the same Page using await Browser.OpenAsync(Url, BrowserLaunchMode.SystemPreferred); this time I need to log in again.

Is it possible to share the session information between these 2 WebViews or better globally within the app?

Costas
  • 459
  • 3
  • 15

1 Answers1

1

At first, Browser.OpenAsync(Url, BrowserLaunchMode.SystemPreferred) will open the Browser not a WebView. You can check the official document about using browser.

And the browser is another app on the device, you can't share the cookie in your app's webview with the browser app. For more information, you can check this answer about the Android Webview private browsing.

In addition,according to this case about how to share cache including cookies between android.webkit.WebView(s), the webviews in the same android app will default share the cookies with each other.

Liyun Zhang - MSFT
  • 8,271
  • 1
  • 2
  • 14
  • It was suggested here: https://stackoverflow.com/a/75141524/7360423 that `Browser.OpenAsync(Url, BrowserLaunchMode.SystemPreferred)` opens the `Url` in **a Chrome based in-app WebView**. Isn't that right? Or is it right for the following code: `Browser.OpenAsync(Url)`? **edit**: https://learn.microsoft.com/en-us/dotnet/api/xamarin.essentials.browserlaunchmode?view=xamarin-essentials Well, I guess you are wrong. With `BrowserLaunchMode.SystemPreferred` you stay in app. – Costas Jan 18 '23 at 09:40
  • I have tried your code. Yes it will open the url in the app and based on the Chrome, but if you bookmark a website in the Chrome app, it will also be bookmarked in the webview opened by `Browser.OpenAsync(Url)`. In other words, the webview opened by `Browser.OpenAsync(Url)` uses the same cookie of the Chrome app. And about the webview's cookie and the browser's cookie, I have explained above. @Costas – Liyun Zhang - MSFT Jan 18 '23 at 09:54
  • You said that the `Browser.OpenAsync(Url)` uses the same cookie as the **Chrome app**. Is this still true for `Browser.OpenAsync(Url, BrowserLaunchMode.SystemPreferred)` too? In other words is it expected to stay logged out in the `Browser.OpenAsync(Url, BrowserLaunchMode.SystemPreferred)` WebView, while I'm logged in, in the embedded WebView? – Costas Jan 18 '23 at 10:05
  • We can't make sure how does the Browser.OpenAsync(Url) work clearly. But in the native android, the webviews in the same app will share the cookies with each other. But the `Browser.OpenAsync(Url)` didn't. @Costas – Liyun Zhang - MSFT Jan 18 '23 at 10:09
  • And the official document also said, it will launch the optimized system browser. And the optimized system browser should be another app, it is in your app doesn't mean you can access the browser's data and cookie. @Costas – Liyun Zhang - MSFT Jan 18 '23 at 10:11
  • In the android, most of the app's data is private. @Costas – Liyun Zhang - MSFT Jan 18 '23 at 10:12
  • Is it possible to do the sharing manually, by adding the appropriate code, after declaring the appropriate permissions in the Android manifest of course? – Costas Jan 18 '23 at 10:15
  • No, you can't. The share depends on two apps, you can make your app share the data with other app. But you can't make the other app receive your share data. In other words, if you want share the webview cookie in your app to the browser. You need to add the data to the browser's cookie. It impossible. @Costas – Liyun Zhang - MSFT Jan 18 '23 at 10:20
  • In addition, if you used the HybridWebView, you don't have use the `Browser.OpenAsync(Url)` to open the browser either. One of them is enough. @Costas – Liyun Zhang - MSFT Jan 18 '23 at 10:37
  • I have an embedded `HybridWebview` in a **Page**. I use `Browser.OpenAsync` in another **Page**. – Costas Jan 18 '23 at 10:44
  • You can only use the HybridWebview with a singleton instance. Or only use the Browser.OpenAsync, I can't find any way to share the webview's session with the browser. @Costas – Liyun Zhang - MSFT Jan 18 '23 at 10:48
  • I guess you are right. I should use my `HybridWebView` everywhere. Is it possible to launch it as a separate Page, as the Broswer.OpenAsync does or should I create a specific Page dedicated for this reason? – Costas Jan 18 '23 at 10:55
  • Yes, you can create a separate Page with the constructor method which has a url parameter or has a binding property to the webview's source. You can make the page singleton and change the binding webview's source every time. @Costas – Liyun Zhang - MSFT Jan 18 '23 at 11:00