首页 > 解决方案 > 关闭包含显示 PDF 的 WKWebView 的 ViewController 时触发 iOS 断言

问题描述

我在关闭UIViewController. 有UIViewController一个UIView,我在其中添加了一个WKWebView. WKWebView我已经加载了一个 PDF 文件。PDF 显示和功能正常。当我关闭窗口时,Xcode 会报告一个断言:

-[PDFExtensionTopView _accessibilityUnregisterRemoteView]: unrecognized selector sent to instance

如果我有我的All Objective-C Exceptions断点,执行就会停止。我可以继续,但这相当具有破坏性。用户从来没有看到任何问题,所以这纯粹是一个开发时的问题。我通过在使用此视图时禁用断点来解决此问题,但更愿意找到真正的修复程序。我在 Xcode 11.3.1 上,在这个类中使用 ObjC。

我创建了一个简化的虚拟项目。整个 UIViewController 的代码如下。它是从一个简单的 UIButton 呈现出来的,它不向这个 UIViewController 传递任何内容。

移动 displayPDF 方法调用在 中viewWillAppear,但在 DidAppear、DidLoad 等中没有变化。它与特定的 PDF 无关(我尝试过来自多个来源的文件)。

如果我使用 displayHTML(将虚拟 HTML 放入 webView),我不会遇到问题。

我可能只是对 ObjC 生疏了,并且遗漏了一些明显的东西。有什么想法吗?

#import "MyPDFViewController.h"
#import <WebKit/WebKit.h>
@interface MyPDFViewController ()
@property (strong, nonatomic) IBOutlet UIView *pdfContainerView;
- (IBAction)closeButtonPressed:(id)sender;
@property (nonatomic, strong) WKWebView *webView;
@end

@implementation MyPDFViewController

-(void)viewWillAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    [self displayPDF];
}

-(void)displayPDF {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"MyPDFFile" ofType:@"pdf"];
    NSData *data = [NSData dataWithContentsOfFile:path];

    self.webView = [[WKWebView alloc] initWithFrame:self.pdfContainerView.frame configuration:[[WKWebViewConfiguration alloc] init]];
    [self.webView loadData:data MIMEType:@"application/pdf" characterEncodingName:@"utf-8" baseURL:[NSURL URLWithString:@""]];

    [self.pdfContainerView addSubview:self.webView];
}

-(void)displayHTML {
    [self.webView loadHTMLString:@"<html><body>Hi.</body></html>" baseURL:nil];
}

- (IBAction)closeButtonPressed:(id)sender {
    [self dismissViewControllerAnimated:YES completion:^{
        //
    }];
}

@end

标签: iospdfwebkitassertion

解决方案


推荐阅读