IPad模拟器很好,但是当使用WKWebview上传图像时,IPad会崩溃应用程序

时间:2016-10-19 06:32:19

标签: ios swift ipad ios-simulator swift3

背景&简短摘要

我正在使用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.'

App似乎在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)
    }

如何处理此异常并解决此问题,以便我的应用程序不会崩溃?如果需要,我可以提供更多细节和代码。感谢您的帮助。

2 个答案:

答案 0 :(得分:3)

您必须对代码进行细微更改才能在iPad中使用。我正在添加代码的缺失行。

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

在3个函数中添加这两行代码。

答案 1 :(得分:0)

请使用以下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];
}