[英]iCLoud document picker from WKWebview dismissing container view
我有一個WKWebview加載基於Web的UI,我希望用戶能夠從他們的iCloud文檔上傳文件。 我已授予正確的權限,我可以瀏覽iCloud文檔。 但是,當我選擇文件或單擊取消按鈕時,以及取消我的WKWebview的父視圖的文檔選擇器視圖也被取消。
我試圖跟蹤解雇路徑。 我100%確定我沒有在我的視圖中調用dismiss函數。
有誰知道什么是觸發我的WKWebview容器上的解雇以及如何防止它?
我在使用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]; } }
我們做的是:
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)在呈現UIDocumentPickerViewController
的UIViewController
上覆蓋這兩個函數
//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
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.