簡體   English   中英

IPad模擬器很好,但是當使用WKWebview上傳圖像時,IPad會崩潰應用程序

[英]IPad Simulator fine but IPad crashes app when uploading image using WKWebview

背景和簡短摘要

我正在使用WkWebview來顯示我的應用程序的網頁。 我有它,你可以從相機或照片庫中選擇一個圖像。 但是,在選擇圖像時應用程序崩潰似乎存在問題。

眼鏡

我在平板電腦上使用IOS 10.0.2,在使用Swift 3的模擬器上運行IOS 10.0。我在XCode 8上運行。

在模擬器上,我在嘗試上傳圖像時遇到“錯誤”

我收到以下消息:

2016-10-19 02:15:36.150670 z4[31561:14708540] [Generic] 
Creating an image format with an unknown type is an error

圖像很好,我可以使用它上傳。 我認為這種行為很奇怪,但我讀到它與IOS上的內存管理有關

在平板電腦上我得到以下內容

Terminating app due to uncaught exception 'NSGenericException', 
reason: 'Your application has presented a 
UIAlertController (<UIAlertController: 0x151e80350>) 
of style UIAlertControllerStyleActionSheet. 

The modalPresentationStyle of a UIAlertController 
with this style is UIModalPresentationPopover. 
You must provide location information for this 
popover through the alert controller's popoverPresentationController. 


You must provide either a sourceView and sourceRect or a barButtonItem.  
If this information is not known when you present the alert controller,
 you may provide it in the UIPopoverPresentationControllerDelegate method 
-prepareForPopoverPresentation.'

該應用程序似乎在AppDelegate中崩潰。 我不知道如何做他們的建議。 我也不知道這是否是一個更深層次問題的一部分,或者我是否遺漏了一些非常簡單的事情。

我與UIAlerts有關的代碼

以下是我與UIAlertController相關的3個函數

   func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {


        let alertController = UIAlertController(title: nil, message: message, preferredStyle: .actionSheet)

        alertController.addAction(UIAlertAction(title: "Ok", style: .default, handler: { (action) in
            completionHandler()
        }))
        self.popoverPresentationController?.sourceView = self.view
        self.popoverPresentationController?.sourceRect = self.view.bounds
        self.present(alertController, animated: true, completion: nil)
    }

    func webView(_ webView: WKWebView, runJavaScriptConfirmPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (Bool) -> Void) {


        let alertController = UIAlertController(title: nil, message: message, preferredStyle: .actionSheet)

        alertController.addAction(UIAlertAction(title: "Yes", style: .default, handler: { (action) in
            completionHandler(true)
        }))

        alertController.addAction(UIAlertAction(title: "No", style: .default, handler: { (action) in
            completionHandler(false)
        }))
        self.popoverPresentationController?.sourceView = self.view
        self.popoverPresentationController?.sourceRect = self.view.bounds
        self.present(alertController, animated: true, completion: nil)
    }

    func webView(_ webView: WKWebView, runJavaScriptTextInputPanelWithPrompt prompt: String, defaultText: String?, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping (String?) -> Void) {


        let alertController = UIAlertController(title: nil, message: prompt, preferredStyle: .actionSheet)

        alertController.addTextField { (textField) in
            textField.text = defaultText
        }

        alertController.addAction(UIAlertAction(title: "Yes", style: .default, handler: { (action) in
            if let text = alertController.textFields?.first?.text {
                completionHandler(text)
            } else {
                completionHandler(defaultText)
            }

        }))

        alertController.addAction(UIAlertAction(title: "No", style: .default, handler: { (action) in

            completionHandler(nil)

        }))

        self.popoverPresentationController?.sourceView = self.view
        self.popoverPresentationController?.sourceRect = self.view.bounds
        self.present(alertController, animated: true, completion: nil)
    }

如何處理此異常並解決此問題,以便我的應用程序不會崩潰? 如果需要,我可以提供更多細節和代碼。 提前謝謝你的幫助。

您必須對代碼進行小的更改才能在iPad中工作。 我正在添加代碼的缺失行。

self.popoverPresentationController = alertController.popoverPresentationController
alertController.modalPresentationStyle = .Popover

在3個函數中添加這兩行代碼。

請使用以下Objective-C代碼作為參考。 所以它可能對你有用。

- (void)showAlertWithTitle:(NSString *)title withMessage:(NSString *)message withStyle:(UIAlertControllerStyle) alertStyle andActions:(NSArray *)actions andSource:(UIView *)sourceView{

UIAlertController *alertController= [UIAlertController
                                     alertControllerWithTitle:title
                                     message:message
                                     preferredStyle:alertStyle];
for (UIAlertAction *action in actions) {
    [alertController addAction:action];
}

UIWindow *alertWindow = [[UIWindow alloc]initWithFrame:[UIScreen mainScreen].bounds];
alertWindow.rootViewController = [[UIViewController alloc]init];
alertWindow.windowLevel = UIWindowLevelAlert + 1;
[alertWindow makeKeyAndVisible];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad) {
    [alertController setModalPresentationStyle:UIModalPresentationPopover];
    UIPopoverPresentationController *popPresenter = [alertController
                                                     popoverPresentationController];
    popPresenter.sourceView = sourceView;
    popPresenter.sourceRect = sourceView.bounds;
}
[alertWindow.rootViewController presentViewController:alertController animated:YES completion:nil];

}

- (void)dismissAlertController{
UIWindow *topWindow = [UIApplication sharedApplication].windows.lastObject;
[topWindow.rootViewController dismissViewControllerAnimated:YES completion: nil];}

要使用上述方法,請參閱以下示例。

 __weak typeof(self) weakSelf = self;
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
    // Cancel button tappped.
    [weakSelf dismissAlertController];
}];
UIAlertAction *removeAction = [UIAlertAction actionWithTitle:@"Remove" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
    [weakSelf dismissAlertController];
    //Do your actions
}];
if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
   [self showAlertWithTitle:@"Are you sure you want to remove?" withMessage:nil withStyle:UIAlertControllerStyleActionSheet andActions:@[removeAction,cancelAction]];
}
else if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad){
   [self showAlertWithTitle:@"Are you sure you want to remove?" withMessage:nil withStyle:UIAlertControllerStyleActionSheet andActions:@[removeAction,cancelAction] andSource:sender];
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM