ios – WKWebView操作表在被解雇后解除显示视图控制器

前端之家收集整理的这篇文章主要介绍了ios – WKWebView操作表在被解雇后解除显示视图控制器前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个嵌入在NavigationController中的视图控制器(WKWebViewController).这个ViewController提供了一个WKWebView.导航到任何网页后;并且在长按任何检测到的内容(例如电话号码或链接)时,会显示一个操作表,其中包含复制,共享等选项.问题是当此操作表被取消时,WKWebViewController将被解除,并且显示根ViewController!无论选择是什么,它都可以复制,取消,甚至可以点击屏幕上的任何位置.

我已经尝试重写“现在(_ viewControllerToPresent:UIViewController,动画标志:Bool,完成:(() – > Void)?= nil)”和“dismiss(动画标志:Bool,完成:(() – > Void)?)“试图理解发生了什么,但后来意识到动作表没有呈现,既没有被其父视图控制器(WKWebViewController)解雇,事实上我在根视图控制器上做了同样的事情并且发现它既没有出现在它上面.

我已经做了很多搜索,试图了解是什么导致了这种行为,我甚至用一个简单的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:

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

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

解决方法

我遇到了同样的问题.

为了处理这种情况,在UIViewController层次结构的根视图控制器中(在你的情况下,这将是“根视图控制器” – 注意,在许多情况下,它可能是你必须子类的UINavgiationController)覆盖dismissViewControllerAnimated :完成:多次处理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];
    }
}

迅速

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相关文章