Xamarin's way to open your own certificate subscription web page in UIWebView

As described in the title of the question, I want to open a self-signed web page in UIWebview (Xamarin.iOS) By default, self-signed web pages do not load in UIWebView.

Important solution requirements:

  • It must be accepted by Apple when I want to send the application to the Apple app store (so custom NSUrlRequest is not suitable).
  • It should load css and javascript correctly.

I found a possible solution for stackoverflow, but this is for native iOS. https://stackoverflow.com/questions/114590/ ... I was also wondering if the solution described above needs to be logged in using NSUrlConnectionDelegate.

The desired solution should be that the user can fill in the credentials on their own using UIWebView.

Can anyone provide a Xamarin solution for this? I tried it myself, but could not get it to work.

Thanks in advance for your help.

+6
source share
1 answer

I know this is a pretty old post, but it was a pretty interesting question, so I had to go for it. Therefore, if you still need it (most likely not), or if someone finds this post, here is a ported version of your own UIWebView with support for self-signing. It can be used as a regular UIWebView, except that as an additional parameter a host name is required, which should be the host name on the page where certificate verification should be disabled.

public class InsecureWebView : UIWebView, INSUrlConnectionDataDelegate, IUIWebViewDelegate { public InsecureWebView(string baseUrl) : base() { Setup (baseUrl); } public InsecureWebView(CoreGraphics.CGRect rect, string baseUrl) : base(rect) { Setup (baseUrl); } public InsecureWebView(NSCoder coder, string baseUrl) : base(coder) { Setup (baseUrl); } public InsecureWebView(NSObjectFlag t, string baseUrl) : base(t) { Setup (baseUrl); } public InsecureWebView(IntPtr handler, string baseUrl) : base(handler) { Setup (baseUrl); } string baseUrl = null; bool authenticated; NSUrlRequest failedRequest; private void Setup(string baseUrl) { this.Delegate = this; this.baseUrl = baseUrl; } [Foundation.Export ("webView:shouldStartLoadWithRequest:navigationType:")] public bool ShouldStartLoad (UIKit.UIWebView webView, Foundation.NSUrlRequest request, UIKit.UIWebViewNavigationType navigationType) { var result = authenticated; if (!authenticated) { failedRequest = request; NSUrlConnection.FromRequest (request, this); } return result; } [Foundation.Export ("connection:willSendRequestForAuthenticationChallenge:")] public void WillSendRequestForAuthenticationChallenge (NSUrlConnection connection, NSUrlAuthenticationChallenge challenge) { if (challenge.ProtectionSpace.AuthenticationMethod == NSUrlProtectionSpace.AuthenticationMethodServerTrust) { var baseUrl = new NSUrl (this.baseUrl); if (challenge.ProtectionSpace.Host == baseUrl.Host) { challenge.Sender.UseCredential (NSUrlCredential.FromTrust (challenge.ProtectionSpace.ServerSecTrust), challenge); } } challenge.Sender.ContinueWithoutCredential (challenge); } [Foundation.Export ("connection:didReceiveResponse:")] public void DidReceivedResponse(NSUrlConnection connection, NSUrlResponse response) { authenticated = true; connection.Cancel (); LoadRequest (failedRequest); } } 
+2
source

Source: https://habr.com/ru/post/975425/


All Articles