MAUI.NET - IOS - 为什么当 ssl 证书无效时 Webview 显示黑屏?

问题描述 投票:0回答:1

我是 Maui.net 的新手,想为 ios 编写一个应用程序,该应用程序显示一个 web 视图,其中显示没有有效 ssl 证书的网站。

我已经成功地为 Android 做到了这一点。不幸的是我在IOS上失败了。有没有人有解决办法?

提前致谢!

我已经尝试过这个:

public class AppDelegate : MauiUIApplicationDelegate
{
    protected override MauiApp CreateMauiApp() => MauiProgram.CreateMauiApp();

    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        ServicePointManager.ServerCertificateValidationCallback =
            (message, certificate, chain, sslPolicyErrors) => true;

        return base.FinishedLaunching(app, options);

        // ...remaining code
    }

我已将 NSAppTransportSecurity 添加到我的 Info.plist 中:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>LSRequiresIPhoneOS</key>
    <true/>
    <key>UIDeviceFamily</key>
    <array>
        <integer>1</integer>
        <integer>2</integer>
    </array>
    <key>UIRequiredDeviceCapabilities</key>
    <array>
        <string>arm64</string>
    </array>
    <key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
    <key>UISupportedInterfaceOrientations~ipad</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationPortraitUpsideDown</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
    <key>XSAppIconAssets</key>
    <string>Assets.xcassets/appicon.appiconset</string>
    <key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleURLName</key>
            <string>URL-Typ 1</string>
            <key>CFBundleTypeRole</key>
            <string>Editor</string>
        </dict>
    </array>
    <key>NSAppTransportSecurity</key>
    <dict>
            <key>NSAllowsArbitraryLoads</key>
            <true/>
    </dict>
</dict>

</plist>
ios ssl webview certificate maui
1个回答
0
投票

我找到了适合我的解决方法:

namespace QHMI.App;

public class Program
{
    // This is the main entry point of the application.
    static void Main(string[] args)
    {
        Microsoft.Maui.Handlers.WebViewHandler.Mapper.AppendToMapping(nameof(WebView), (handler, view) =>
        {
            //handler.PlatformView.SetBackgroundColor(Android.Graphics.Color.Yellow);
            handler.PlatformView.NavigationDelegate = new NavigationDelegate();
        });

        // if you want to use a different Application Delegate class from "AppDelegate"
        // you can specify it here.
        UIApplication.Main(args, null, typeof(AppDelegate));
    }
}

public class NavigationDelegate : WKNavigationDelegate
{
    public override void DidReceiveAuthenticationChallenge(WKWebView webView, NSUrlAuthenticationChallenge challenge, Action<NSUrlSessionAuthChallengeDisposition, NSUrlCredential> completionHandler)
    {
        using (var cred = NSUrlCredential.FromTrust(challenge.ProtectionSpace.ServerSecTrust))
        {
            completionHandler.Invoke(NSUrlSessionAuthChallengeDisposition.UseCredential, cred);
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.