Xamarin Forms WebView

DaveSilveiraDaveSilveira USMember ✭✭

We have an application in Production that has native versions for iOS and Android.

We are converting the code of these native applications into a single Xamarin Forms application.

This application is based on WebView, responsible for:

1 - Execute JavaScripts commands of the presented pages;
2 - Execution of C # code, from JavaScripts pages;
3 - Save the content, as image, to storage;
4 - Validate the Certificate through the Certificate Pinning process.

Solution for JavaScript questions:
docs.microsoft.com/pt-br/xamarin/xamarin-forms/app-fundamentals/custom-renderer/hybridwebview
github.com/SKLn-Rad/Xam.Plugin.Webview

Solution for Certificate Pinning: basdecort.com/2018/07/18/protecting-your-users-with-certificate-pinning/

After trying to merge the solutions, I´ve discovered that the forms data are not been submited between pages anymore.

Does anyone knows if Xamarin Forms supports this kind of WebView solution?

Answers

  • LandLuLandLu Member, Xamarin Team Xamurai
    edited May 7

    What do you mean forms data are not been submited between pages anymore?
    Hybrid WebView could interact with JavaScripts. So I think your 1 and 2 requirements could be fit through using that control.
    But I can't open your url about Certificate Pinning process, did you fix that either?
    You'd better share your sample here to help us get more information.

  • DaveSilveiraDaveSilveira USMember ✭✭

    What a coincidence!! The Certificate Pinning article is out one day after I posted my questions. :neutral:

    Their article can be found in Google´s cache and their code is hosted in github.com/basdecort/Xamarin. I saved the contents article in the following PDF file.

    LandLu, Thanks for answering.

    I can find lots of sample codes working well for each requirements, but I am trying to make them to work together using one single WebView.

    The solution that Javascripts requirements works, uses "View" and a PCL project. The solution for Certificate Pinning, uses "WebView" and requires .Net Standard 2.0.

    For the saving content from WebView requirement, I tested several codes, but the only thing I got was a black or a white image.

    I am going to to prepare the merged code and post it here.

  • DaveSilveiraDaveSilveira USMember ✭✭

    I attached the solution that I've been work with. It has a merged solutions for requirements 1, 2 and 4.

    I excluded any important information. May be needed to point it to some site form to simulate the loss of data.

  • LandLuLandLu Member, Xamarin Team Xamurai

    I can run your sample on my side, but I can only enter the first main page. What parts do you want me to help you fix? Each item in your list view seems to require some kind of permission as it is not written by English I can't understand its meaning.

    I can't find how do you store your images and data either.

    I've seen your renderer for web view and you said Javascripts requirements was made by PCL. I think this documentation is much more appropriate for you: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/hybridwebview

  • DaveSilveiraDaveSilveira USMember ✭✭

    Sorry, LandLu. The file I post before was a version that I tried the solutions to work in parallel. It was not the merged solutions. As I said before, the save image solution does not make part of this file.

    I am sending a new file with the merged solutions of XAM Hybrid WebView(evoluated version of Microsofts article) and the Certificate Pinning. The solutions works separated, but not together.

    You can change the address on AccountAccess.cs file.

    Each solution has your own Renderer. They were merged into FormsWebViewRenderer.cs. The Certificate Pinning, uses SafeWebViewClient class. If we comment ShouldInterceptRequest method, the data filled on the form can be requested when submited.

    Thanks,

  • LandLuLandLu Member, Xamarin Team Xamurai

    Have you followed this to modify your renderer on Android side: https://docs.microsoft.com/en-us/xamarin/xamarin-forms/app-fundamentals/custom-renderer/hybridwebview#creating-the-custom-renderer-on-android?
    Add this in your SafeWebViewClient:

    string _javascript;
    
    public JavascriptWebViewClient(string javascript)
    {
        _javascript = javascript;
    }
    
    public override void OnPageFinished(WebView view, string url)
    {
        base.OnPageFinished(view, url);
        view.EvaluateJavascript(_javascript, null);
    }
    

    If we comment ShouldInterceptRequest method, the data filled on the form can be requested when submited.

    What do you mean about this?
    Why do you want to intercept when webview loads pages?

  • DaveSilveiraDaveSilveira USMember ✭✭

    LandLu,

    The ShouldInterceptRequest method is a code from the Certificate Pinning solution. Without that, the application only solves the JavaScripts requirements.

    The Certificate Pinning validades the Certificate to guarantee that the application is accessing the correct address. If the validation fails, the content will not show to the application user.

  • LandLuLandLu Member, Xamarin Team Xamurai

    @DaveSilveira If I haven't misunderstood your meaning, you had merged 1, 2, 4 points to your projects?
    But I'm confused about what you want to do about your third requirement.
    I didn't find the code about storage. And what issues are you facing now?

  • DaveSilveiraDaveSilveira USMember ✭✭

    LandLu,

    This is not a simple situation, where I can post one or two pieces of methods. I did an evaluation and I concluded that points 1, 2 and 4 are critical.

    I tryed to make the merge sample as simple as it can be.

  • LandLuLandLu Member, Xamarin Team Xamurai

    @DaveSilveira I'm a little confused now. What is your issue here?

Sign In or Register to comment.