简体   繁体   中英

Is there a way i can use the webview.postURL on xamarin.forms?

I'm creating an app wrapping my website with Xamarin.forms. I want to implement login page on my mobile app that will by pass the login on the website using webview.postURL. Does postURL works for both iOS and android? or android only? If it only works on android is there a way to do it for both platform?

I'm new to xamarin and c#, example codes or a github link would help me a lot.

You can implement it by using CustomRenderer .

in Forms,define a subclass of Webview

using Xamarin.Forms;

namespace xxx
{
    public class MyWebview:WebView
    {
        public string data; //Parameters that you want to pass
        public string url;
        public MyWebview()
        {

        }
    }
}

in contentPage

public MainPage()
{
   InitializeComponent();

   Content = new StackLayout
     {
        Children =
          {
            new MyWebview()
             {
               url="your url",
               WidthRequest = 300,
               HeightRequest = 500,
               data = "userName=xxx"
              },
          },
          VerticalOptions = LayoutOptions.FillAndExpand,
          HorizontalOptions=LayoutOptions.FillAndExpand

      };
}

in iOS project

using Foundation;
using UIKit;


using xxx;
using xxx.iOS;

using Xamarin.Forms;
using Xamarin.Forms.Platform.iOS;

[assembly:ExportRenderer(typeof(MyWebview),typeof(MyWebViewRenderer))]
namespace xxx.iOS
{
    public class MyWebViewRenderer:WebViewRenderer
    {
        protected override void OnElementChanged(VisualElementChangedEventArgs e)
        {
            base.OnElementChanged(e);

            if(NativeView!=null)
            {

                var mywebview = Element as MyWebview;

                var request = new NSMutableUrlRequest(new NSUrl(new NSString(mywebview.url)));
                request.Body = mywebview.data;
                request.HttpMethod = "POST";
                LoadRequest(request);

            }
        }        

    }
}

Notes

For iOS 9 onwards and MacOS, if you wish to access unsecure sites you may need to configure or disable ATS

<key>NSAppTransportSecurity</key>
<dict>
<key>NSAllowsArbitraryLoads</key><true/>
</dict>

in Android Project

using System.Text;

using Android.App;
using Android.Content;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Webkit;
using Android.Widget;

using xxx;
using xxx.Droid;

using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: ExportRenderer(typeof(MyWebview), typeof(MyWebViewRenderer))]
namespace xxx.Droid
{
    public class MyWebViewRenderer:WebViewRenderer
    {
        public MyWebViewRenderer(Context context):base(context)
        {

        }

        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
        {
            base.OnElementChanged(e);

            if(Control!=null)
            {
                var mywebview = Element as MyWebview;

                var postData = Encoding.UTF8.GetBytes(mywebview.data);
                Control.PostUrl(mywebview.url, postData);

            }

        }

    }
}

For more detail about custom renderer you can refer to here .

POST Request Over Webview For WkWebview

[assembly: ExportRenderer(typeof(PaymentWebview), typeof(PaymentWebViewRenderer))] namespace MMFInvestorApp.iOS.Utils { public class PaymentWebViewRenderer: WkWebViewRenderer { protected override void OnElementChanged(VisualElementChangedEventArgs e) { base.OnElementChanged(e);

        if (NativeView != null)
        {
            var request = new NSMutableUrlRequest(new NSUrl(new NSString(paymentwebview.url))); //Your Url
            request.HttpMethod = "POST";
            request.Body = NSData.FromString(paymentwebview.data); //Data for POST
            request["Content-Length"] = req.Body.Length.ToString();
            request["Content-Type"] = "application/x-www-form-urlencoded charset=utf-8";
            LoadRequest(request);
        }

    }


}

}

For UIWebview (Deprecated from April 2020

[assembly: ExportRenderer(typeof(PaymentWebview), typeof(PaymentWebViewRenderer))] namespace MMFInvestorApp.iOS.Utils { public class PaymentWebViewRenderer: WebViewRenderer { protected override void OnElementChanged(VisualElementChangedEventArgs e) { base.OnElementChanged(e);

        if (NativeView != null)
        {
            var paymentwebview = Element as PaymentWebview;
            var request = new NSMutableUrlRequest(new NSUrl(new NSString(paymentwebview.url)));//Your Url
            request.Body = paymentwebview.data; //Data for POST
            request.HttpMethod = "POST";
            LoadRequest(request);
        }

    }


}

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM