SwiftUI-没有使用HTTP基本身份验证自动填充凭据
如果我想访问使用http基本身份验证的网页,我正在努力实现一个自动填充选项 在我的应用程序的“正常”登录页面上,访问自动填充可以正常工作(“自动填充凭据提供程序”功能设置正确) 我首先尝试使用UIViewControllerRepresentable,然后使用单独的HostingController,但两者的行为相同: UIViewControllerRepresentable:SwiftUI-没有使用HTTP基本身份验证自动填充凭据,swift,uikit,swiftui,sfsafariviewcontroller,Swift,Uikit,Swiftui,Sfsafariviewcontroller,如果我想访问使用http基本身份验证的网页,我正在努力实现一个自动填充选项 在我的应用程序的“正常”登录页面上,访问自动填充可以正常工作(“自动填充凭据提供程序”功能设置正确) 我首先尝试使用UIViewControllerRepresentable,然后使用单独的HostingController,但两者的行为相同: UIViewControllerRepresentable: import SwiftUI import SafariServices struct SafariView:
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>) {
}
}
导入快捷界面
进口狩猎服务
结构SafariView:UIViewControllerRepresentable{
让url:url
func makeUIViewController(上下文:UIViewControllerRepresentableContext)->SFSafariViewController{
返回SFSafariViewController(url:url)
}
func updateUIViewController(uViewController:SFSafariViewController,上下文:UIViewControllerRepresentableContext){
}
}
独立主机控制器:
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)
}
}
<代码>导入基础
导入快捷键
进口狩猎服务
类HSHosting{
静态变量控制器:UIViewController?
静态变量nextModalPresentationStyle:UIModalPresentationStyle?
静态func openSafari(url:url,tint:UIColor?=nil){
guard let controller=控制器else{
预处理失败(“不存在控制器。您是否记得在SceneDelegate中使用HSHostingController而不是UIHostingController?”)
}
设vc=SFSafariViewController(url:url)
vc.preferredBarTintColor=色调
控制器.present(vc,动画:true)
}
}
类HSHostingController:UIHostingController,其中内容:视图{
重写初始化(根视图:内容){
super.init(rootView:rootView)
HSHosting.controller=self
}
@objc所需的动态初始化?(编码器aDecoder:NSCoder){
fatalError(“初始化(编码者:)尚未实现”)
}
覆盖函数存在(viewControllerToPresent:UIViewController,动画标志:Bool,完成:(()->Void)?=nil){
如果让nextStyle=HSHosting.nextModalPresentationStyle{
viewControllerToPresent.modalPresentationStyle=nextStyle
HSHosting.nextModalPresentationStyle=nil
}
super.present(viewControllerToPresent,动画:标志,完成:完成)
}
}
当我在Safari中直接打开相同的URL时,我会获得预期的自动填充选项: