我有一个嵌入在 NavigationController 中的 View Controller (WKWebViewController)。这个 ViewController 呈现了一个 WKWebView。导航到任何网页后;并且在长按任何检测到的内容(例如电话号码或链接)时,会显示一个操作表,其中包含复制、共享等选项。问题是当此操作表被解除时,WKWebViewController 会随之被解除,并且显示根 View Controller !无论选择是什么,是复制、取消,还是点击屏幕上的任何地方。

我试过覆盖“present(_ viewControllerToPresent: UIViewController, animation flag: Bool, completion: (() -> Void)? = nil)”和“dismiss(animated flag: Bool, completion: (() ->无效)?)”试图了解正在发生的事情,但随后意识到操作表没有被呈现,也没有被其父 View Controller (WKWebViewController)驳回,事实上我在根 View Controller 上做了同样的事情并发现它也没有出现在它上面。

我做了很多搜索试图了解导致这种行为的原因,我什至只用一个简单的 WKWebView 构建了一个新项目,但总是以同样的问题告终。

这是代码:

import UIKit; import WebKit
class WKWebViewController: UIViewController, WKUIDelegate, WKNavigationDelegate {

var destinationUrlString: String?
var myWebView: WKWebView!

override func viewDidLoad() {
    super.viewDidLoad()

    let webConfiguration = WKWebViewConfiguration()
    webConfiguration.dataDetectorTypes = []
    let origin = CGPoint(x: 0, y: 0)
    let size  = CGSize(width: view.frame.size.width, height: view.frame.size.height)
    myWebView = WKWebView(frame: .init(origin: origin, size: size), configuration: webConfiguration)
    myWebView.uiDelegate = self
    myWebView.navigationDelegate = self
    myWebView.allowsLinkPreview = false
    view = myWebView

    destinationUrlString = "https://www.stackoverflow.com"
    guard let url = URL(string: destinationUrlString!) else {return}
    print(url)
    let request = URLRequest(url: url)
    myWebView.load(request)
}
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
    //show progress indicator
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
    //dismiss progress indicator
}
func webView(_ webView: WKWebView, didFail navigation: WKNavigation!, withError error: Error) {
    //show error
}
func webView(_ webView: WKWebView, didFailProvisionalNavigation navigation: WKNavigation!, withError error: Error) {
    //show error
}
}

我还附上了一个显示问题的 GIF:

ios - WKWebView 操作表在被解散后解散呈现 View  Controller-LMLPHP

我正在使用 Xcode 9.3 (9E145) 和 Swift 4.1。

我错过了什么吗?如何解决这个问题?
任何帮助将非常感激。

最佳答案

我也遇到过同样的问题。

为了处理这种情况,在 UIViewController 层次结构的根 View Controller 中(在您的情况下,这将是“根 View Controller ”——注意,在许多情况下,它可能是一个 UINavgiationController ,然后您必须对其进行子类化)覆盖 dismissViewControllerAnimated:completion: 以处理 WKWebView 多次调用dismiss。

Objective-C

@property (weak) UIViewController *lastPresentedController;

- (void)dismissViewControllerAnimated:(BOOL)flag completion:(void (^)(void))completion
{
    // WKWebView actions sheets workaround
    if (self.presentedViewController && self.lastPresentedController != self.presentedViewController ) {
        self.lastPresentedController = self.presentedViewController;
        [self.presentedViewController dismissViewControllerAnimated:YES completion:^{
            if( completion ) {
                completion();
            }
            self.lastPresentedController = nil;
        }];
    } else if( !self.lastPresentedController) {
        [super dismissViewControllerAnimated:flag completion:completion];
    }
}

swift
private weak var lastPresentedController : UIViewController?

override func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
    // WKWebView actions sheets workaround
    if presentedViewController != nil && lastPresentedController != presentedViewController  {
        lastPresentedController = presentedViewController;
        presentedViewController?.dismiss(animated: flag, completion: {
            completion?();
            self.lastPresentedController = nil;
        });

    } else if lastPresentedController == nil {
        super.dismiss(animated: flag, completion: completion);
    }
}

关于ios - WKWebView 操作表在被解散后解散呈现 View Controller ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49856616/

10-09 01:23