2017-03-13 9 views
1

this answerのソリューションを使用していますが、javascriptの警告/確認/ etcボックスが正しく表示され、iphoneでうまく機能します。 JavaScriptのアクションを使って何をクリックiPadの上しかしiOS WKWebView javascriptの警告がipadでクラッシュする

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

    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: "OK", style: .default, handler: { (action) in 
     completionHandler(true) 
    })) 

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

    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: "OK", style: .default, handler: { (action) in 
     if let text = alertController.textFields?.first?.text { 
      completionHandler(text) 
     } else { 
      completionHandler(defaultText) 
     } 
    })) 

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

    present(alertController, animated: true, completion: nil) 
} 

[UIPopoverPresentationController presentationTransitionWillBegin]

致命的な例外を持つアプリケーションをクラッシュ:NSGenericException アプリケーションは、スタイルUIAlertControllerStyleActionSheetのUIAlertController()を提示しました。このスタイルのUIAlertControllerのmodalPresentationStyleはUIModalPresentationPopoverです。アラートコントローラのpopoverPresentationControllerを使用して、このポップオーバーの位置情報を指定する必要があります。 sourceViewとsourceRect、またはbarButtonItemのいずれかを指定する必要があります。アラートコントローラを表示するときにこの情報が不明な場合は、UIPopoverPresentationControllerDelegateメソッド-prepareForPopoverPresentationで指定することができます。

私はこの問題を解決するためにここに迷っています。誰もこれについていくつかのアドバイスを与えることができますか?私はUIPopoverPresentationControllerDelegate方法 -prepareForPopoverPresentation

を使用するためのさまざまな方法を試してみました。」

私のコードでは、成功しませんでした。これについて何かアドバイスをいただきました。ありがとう。

答えて

1

これを調べると、これに関係する他の人のためにメモを残したかったのです。機能の末尾に小さな変更:

if let presenter = alertController.popoverPresentationController { 
     presenter.sourceView = self.view 
    } 

    self.present(alertController, animated: true, completion: nil) 
+0

present(alertController, animated: true, completion: nil) 

を交換一度に複数のアラートを表示しようとすると、どこのアプリがクラッシュする問題が発生したしましたか? iPhoneでうまく動作しますが、iPadでクラッシュします。 – Crashalot

関連する問題