Ios Xamarin在UIWebView中打开自签名证书网页的方法
如问题标题所述,我想在UIWebview(Xamarin.iOS)中打开一个自签名网页 默认情况下,自签名网页不会加载到UIWebView中 解决方案的重要要求:Ios Xamarin在UIWebView中打开自签名证书网页的方法,ios,uiwebview,xamarin.ios,xamarin,ssl-certificate,Ios,Uiwebview,Xamarin.ios,Xamarin,Ssl Certificate,如问题标题所述,我想在UIWebview(Xamarin.iOS)中打开一个自签名网页 默认情况下,自签名网页不会加载到UIWebView中 解决方案的重要要求: 当我想将应用提交到苹果应用商店时,苹果应该接受它(因此定制的NSURL请求不合适) 它应该正确加载css和javascript 我在stackoverflow上找到了一个可能的解决方案,但这是针对本机iOS的。 我还想知道上述解决方案是否需要使用NSURLConnectionLegate登录 理想的解决方案应该是用户可以使用UIW
- 当我想将应用提交到苹果应用商店时,苹果应该接受它(因此定制的NSURL请求不合适)
- 它应该正确加载css和javascript
提前感谢您的帮助。我知道这是一篇很老的帖子,但这是一个很有趣的问题,所以我不得不尝试一下。因此,如果您仍然需要它(很可能不需要),或者如果有人找到这篇文章,这里有一个支持自签名的本地UIWebView的移植版本。它可以用作常规的UIWebView,但以主机名作为附加参数除外,该参数应为禁用证书检查的页面的主机名
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);
}
}