我有一个WKWebview加载基于Web的UI,我希望用户能够从他们的iCloud文档上传文件。我已授予正确的权限,我可以浏览iCloud文档。但是,当我选择文件或单击取消按钮时,以及取消我的WKWebview的父视图的文档选择器视图也被取消。
我试图跟踪解雇路径。我100%肯定我没有在我的观点上调用解雇功能。
有没有人知道什么是触发我的WKWebview容器上的解雇以及如何防止它?
答案 0 :(得分:3)
我在使用WKWebView的Objective-C和iOS11上遇到了同样的问题,并使用此解决方法解决了它。您应该可以轻松地将其迁移到Swift:
在此视图控制器中添加此弱属性
@property (weak, nonatomic) UIDocumentPickerViewController *_Nullable docPickerPtr;
在同一视图控制器中覆盖这两个方法,这两个方法最初是UIViewController基类的一部分
- (void)presentViewController:(UIViewController *)viewControllerToPresent animated: (BOOL)flag completion:(void (^ __nullable)(void))completion
{
if ([viewControllerToPresent isKindOfClass:[UIDocumentPickerViewController class]])
{
_docPickerPtr = (UIDocumentPickerViewController*)viewControllerToPresent;
}
[super presentViewController:viewControllerToPresent animated:flag completion:completion];
}
- (void)dismissViewControllerAnimated:(BOOL)flag
completion:(void (^)(void))completion
{
if (_docPickerPtr != nil && self.presentedViewController == nil)
{
NSLog(@">>>>>>>>>>>>PREVENT FROM DOING 2nd DISMISS!");
}
else
{
[super dismissViewControllerAnimated:flag completion:completion];
}
}
我们所做的是:
答案 1 :(得分:3)
UIDocumentPickerViewController
中存在错误。
1)在视图控制器呈现UIDocumentPickerViewController
的内部保存对UIDocumentPickerViewController
的弱引用。 (这通常最终成为UINavigationController
,因此您可能需要继承UINavigationController
来解决此问题。)
///Due to a bug in UIDocumentPickerViewController we need to stop the UIDocumentPickerViewController from dismissing this navigation controller. Or at least provide control. This is a weak reference to a UIDocumentPickerController that this controller presents
weak var documentPicker: UIDocumentPickerViewController?
2)在呈现UIViewController
UIDocumentPickerViewController
上覆盖这两个函数
//MARK: Overrides
override public func dismiss(animated flag: Bool, completion: (() -> Void)? = nil) {
if self.presentedViewController == nil && self.documentPicker != nil {
self.documentPicker = nil
}else{
super.dismiss(animated: flag, completion: completion)
}
}
public override func present(_ viewControllerToPresent: UIViewController, animated flag: Bool, completion: (() -> Void)? = nil) {
if viewControllerToPresent is UIDocumentPickerViewController {
self.documentPicker = viewControllerToPresent as? UIDocumentPickerViewController
}
super.present(viewControllerToPresent, animated: flag, completion: completion)
}
现在来自UIDocumentPickerViewController
的第二个电话不会驳回呈现UIViewController
。