IPad Simulator fine but IPad crashes app when uploading image using WKWebview

笑着哭i 提交于 2019-12-11 06:54:23

问题


Background & Short Summary

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.

Specs

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.

On the simulator I am getting an "error" when trying to upload images

I get the following message:

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

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

On the tablet itself I get the following

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.'

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.

Code that I have related to UIAlerts

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

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.


回答1:


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

Add these two lines of code in your 3 functions.




回答2:


Please use below Objective-C code as reference. So it may be work for you.

- (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];}

To use above methods, Here is the sample.

 __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];
}


来源:https://stackoverflow.com/questions/40123838/ipad-simulator-fine-but-ipad-crashes-app-when-uploading-image-using-wkwebview

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!