首页 > 解决方案 > SwiftUI - 没有使用 HTTP 基本身份验证的自动填充凭据

问题描述

如果我想访问使用 http 基本身份验证的网页,我正在努力实现自动填充选项。

在我的应用程序中

在我的应用程序的“正常”登录页面上访问自动填充工作正常(功能“自动填充凭据提供程序”设置正确)。

我首先尝试了 UIViewControllerRepresentable ,然后我使用了单独的 HostingController 但两者都有相同的行为:

UIViewController 可表示:

import SwiftUI
import SafariServices

struct SafariView: UIViewControllerRepresentable {
    
    let url: URL
    
    func makeUIViewController(context: UIViewControllerRepresentableContext<SafariView>) -> SFSafariViewController {
        return SFSafariViewController(url: url)
    }
    
    func updateUIViewController(_ uiViewController: SFSafariViewController, context: UIViewControllerRepresentableContext<SafariView>) {
    }
    
}

单独的 HostingController:

import Foundation
import SwiftUI
import SafariServices

class HSHosting {
    static var controller:UIViewController?
    static var nextModalPresentationStyle:UIModalPresentationStyle?

    static func openSafari(url:URL,tint:UIColor? = nil) {
        guard let controller = controller else {
            preconditionFailure("No controller present. Did you remember to use HSHostingController instead of UIHostingController in your SceneDelegate?")
        }

        let vc = SFSafariViewController(url: url)

        vc.preferredBarTintColor = tint

        controller.present(vc, animated: true)
    }
}

class HSHostingController<Content> : UIHostingController<Content> where Content : View {

    override init(rootView: Content) {
        super.init(rootView: rootView)

        HSHosting.controller = self
    }

    @objc required dynamic init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    override func present(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil) {

        if let nextStyle = HSHosting.nextModalPresentationStyle {
            viewControllerToPresent.modalPresentationStyle = nextStyle
            HSHosting.nextModalPresentationStyle = nil
        }

        super.present(viewControllerToPresent, animated: flag, completion: completion)
    }

}

当我直接在 Safari 中打开相同的 URL 时,我按预期获得了 AutoFill 选项:

在 Safari 中工作

标签: swiftuikitswiftuisfsafariviewcontroller

解决方案


推荐阅读