IPad Simulator很好,但是当使用WKWebview上传图像时,IPad会崩溃应用程序 [英] IPad Simulator fine but IPad crashes app when uploading image using WKWebview

查看:455
本文介绍了IPad Simulator很好,但是当使用WKWebview上传图像时,IPad会崩溃应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

背景&简短摘要

我正在使用WkWebview来显示我的应用程序的网页。我有它,你可以从相机或照片库中选择一个图像。但是,在选择图像时应用程序崩溃似乎存在问题。

I am using WkWebview in order to show web pages for my app. I have it so that you can choose an image from camera or photo library. However there seems to be an issue with the app crashing on selecting the image.

规格

我在平板电脑上运行IOS 10.0.2, IOS 10.0在模拟器上使用Swift 3.我从XCode 8运行。

I am running on IOS 10.0.2 on Tablet , and IOS 10.0 on the simulator using Swift 3. I am running both from 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上的内存管理有关

The image is fine and I am able to use it for upload. This behavior I thought was weird but I read that it has to do with memory management on 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中崩溃。我不知道如何做他们的建议。我也不知道这是否是更深层问题的一部分,或者我错过了一些非常简单的事情。

The app seems to crash in the AppDelegate. I have no idea how to do their recommendations. I also don't know if this is part of a deeper issue, or if I am missing something really simple.

我与UIAlerts相关的代码

以下是我与UIAlertController相关的3个函数

The following are 3 functions I have related to UIAlertController

   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)
    }

我该怎么办? andle这个例外并解决了这个问题所以我的应用程序不会崩溃我?如果需要,我可以提供更多细节和代码。感谢您的帮助。

How can I handle this exception and fix this problem so my app doesn't crash on me? I can provide more details and code if needed. Thank you for your help in advance.

推荐答案

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

You have to do small changes in your code to work in iPad. I am adding the missing line of your code.

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

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

Add these two lines of code in your 3 functions.

这篇关于IPad Simulator很好,但是当使用WKWebview上传图像时,IPad会崩溃应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆