gpt4 book ai didi

ios - 如何在 UIView 中呈现 SFSafariViewController

转载 作者:搜寻专家 更新时间:2023-11-01 06:14:02 25 4
gpt4 key购买 nike

我有一个具有按钮属性的自定义 UIView,我想向它添加一个操作以在应用程序中打开 webview。为此,我想使用 SFSafariViewController。以下是我正在努力实现这一目标的方法。

import UIKit
import SafariServices

class CustomViewScreen: UIView, SFSafariViewControllerDelegate {

@IBAction func privacyNoteBtnAction(_ sender: UIButton) {
if #available(iOS 9.0, *) {
let urlString = "https://stackoverflow.com/"
if let url = URL(string: urlString) {
let vc = SFSafariViewController(url: url, entersReaderIfAvailable: true)
vc.delegate = self
UIApplication.shared.keyWindow?.rootViewController?.present(vc, animated: true, completion: nil)
}
} else {
// Fallback on earlier versions
}
}

func safariViewControllerDidFinish(_ controller: SFSafariViewController) {
controller .dismiss(animated: true, completion: nil) // also not able to trigger this method when i tap "Done"
}

我什至无法正确使用这种方式,因为我得到“谁的 View 不在窗口层次结构中!”此外,我无法在点击“完成”按钮后调用 SFSafariViewControllerDelegate 方法来关闭 View 。当我在 UIView 中时有什么想法可以给出 Action ,或者有什么想法可以将此 Action 连接到我的主 Controller 以使用正确呈现的方法吗?

最佳答案

这样做,

@IBAction func privacyNoteBtnAction(_ sender: UIButton) {
if #available(iOS 9.0, *) {
let urlString = "https://stackoverflow.com/"
if let url = URL(string: urlString) {
let vc = SFSafariViewController(url: url, entersReaderIfAvailable: true)
vc.delegate = self

if var topController = UIApplication.shared.keyWindow?.rootViewController {
while let presentedViewController = topController.presentedViewController {
topController = presentedViewController
}

topController.present(vc, animated: true, completion: nil)
}

}
} else {
// Fallback on earlier versions
}
}

关于ios - 如何在 UIView 中呈现 SFSafariViewController,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49104443/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com