@capgo/inappbrowser 7.7.1 → 7.7.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -478,7 +478,20 @@ open class WKWebViewController: UIViewController, WKScriptMessageHandler {
|
|
|
478
478
|
semaphore.signal()
|
|
479
479
|
} else if message.name == "close" {
|
|
480
480
|
closeView()
|
|
481
|
-
|
|
481
|
+
} else if message.name == "magicPrint" {
|
|
482
|
+
if let webView = self.webView {
|
|
483
|
+
let printController = UIPrintInteractionController.shared
|
|
484
|
+
|
|
485
|
+
let printInfo = UIPrintInfo(dictionary: nil)
|
|
486
|
+
printInfo.outputType = .general
|
|
487
|
+
printInfo.jobName = "Print Job"
|
|
488
|
+
|
|
489
|
+
printController.printInfo = printInfo
|
|
490
|
+
printController.printFormatter = webView.viewPrintFormatter()
|
|
491
|
+
|
|
492
|
+
printController.present(animated: true, completionHandler: nil)
|
|
493
|
+
}
|
|
494
|
+
}
|
|
482
495
|
}
|
|
483
496
|
|
|
484
497
|
func injectJavaScriptInterface() {
|
|
@@ -496,9 +509,17 @@ open class WKWebViewController: UIViewController, WKScriptMessageHandler {
|
|
|
496
509
|
};
|
|
497
510
|
}
|
|
498
511
|
"""
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
512
|
+
DispatchQueue.main.async {
|
|
513
|
+
self.webView?.evaluateJavaScript(script) { result, error in
|
|
514
|
+
if let error = error {
|
|
515
|
+
print("JavaScript evaluation error: \(error)")
|
|
516
|
+
} else if let result = result {
|
|
517
|
+
print("JavaScript result: \(result)")
|
|
518
|
+
} else {
|
|
519
|
+
print("JavaScript executed with no result")
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
}
|
|
502
523
|
}
|
|
503
524
|
|
|
504
525
|
open func initWebview(isInspectable: Bool = true) {
|
|
@@ -513,14 +534,35 @@ open class WKWebViewController: UIViewController, WKScriptMessageHandler {
|
|
|
513
534
|
userContentController.add(self, name: "preShowScriptError")
|
|
514
535
|
userContentController.add(self, name: "preShowScriptSuccess")
|
|
515
536
|
userContentController.add(self, name: "close")
|
|
537
|
+
userContentController.add(self, name: "magicPrint")
|
|
538
|
+
|
|
539
|
+
// Inject JavaScript to override window.print
|
|
540
|
+
let script = WKUserScript(
|
|
541
|
+
source: """
|
|
542
|
+
window.print = function() {
|
|
543
|
+
window.webkit.messageHandlers.magicPrint.postMessage('magicPrint');
|
|
544
|
+
};
|
|
545
|
+
""",
|
|
546
|
+
injectionTime: .atDocumentStart,
|
|
547
|
+
forMainFrameOnly: false
|
|
548
|
+
)
|
|
549
|
+
userContentController.addUserScript(script)
|
|
550
|
+
|
|
516
551
|
webConfiguration.allowsInlineMediaPlayback = true
|
|
517
552
|
webConfiguration.userContentController = userContentController
|
|
553
|
+
|
|
518
554
|
let webView = WKWebView(frame: .zero, configuration: webConfiguration)
|
|
519
555
|
|
|
520
|
-
if webView.responds(to: Selector(("setInspectable:"))) {
|
|
521
|
-
// Fix: https://stackoverflow.com/questions/76216183/how-to-debug-wkwebview-in-ios-16-4-1-using-xcode-14-2/76603043#76603043
|
|
522
|
-
webView.perform(Selector(("setInspectable:")), with: isInspectable)
|
|
523
|
-
}
|
|
556
|
+
// if webView.responds(to: Selector(("setInspectable:"))) {
|
|
557
|
+
// // Fix: https://stackoverflow.com/questions/76216183/how-to-debug-wkwebview-in-ios-16-4-1-using-xcode-14-2/76603043#76603043
|
|
558
|
+
// webView.perform(Selector(("setInspectable:")), with: isInspectable)
|
|
559
|
+
// }
|
|
560
|
+
|
|
561
|
+
if #available(iOS 16.4, *) {
|
|
562
|
+
webView.isInspectable = true
|
|
563
|
+
} else {
|
|
564
|
+
// Fallback on earlier versions
|
|
565
|
+
}
|
|
524
566
|
|
|
525
567
|
if self.blankNavigationTab {
|
|
526
568
|
// First add the webView to view hierarchy
|
|
@@ -1252,13 +1294,38 @@ fileprivate extension WKWebViewController {
|
|
|
1252
1294
|
// MARK: - WKUIDelegate
|
|
1253
1295
|
extension WKWebViewController: WKUIDelegate {
|
|
1254
1296
|
public func webView(_ webView: WKWebView, runJavaScriptAlertPanelWithMessage message: String, initiatedByFrame frame: WKFrameInfo, completionHandler: @escaping () -> Void) {
|
|
1297
|
+
// Create a strong reference to the completion handler to ensure it's called
|
|
1298
|
+
let strongCompletionHandler = completionHandler
|
|
1299
|
+
|
|
1255
1300
|
// Ensure UI updates are on the main thread
|
|
1256
|
-
DispatchQueue.main.async {
|
|
1301
|
+
DispatchQueue.main.async { [weak self] in
|
|
1302
|
+
guard let self = self else {
|
|
1303
|
+
// View controller was deallocated
|
|
1304
|
+
strongCompletionHandler()
|
|
1305
|
+
return
|
|
1306
|
+
}
|
|
1307
|
+
|
|
1308
|
+
// Check if view is available and ready for presentation
|
|
1309
|
+
guard self.view.window != nil, !self.isBeingDismissed, !self.isMovingFromParent else {
|
|
1310
|
+
print("[InAppBrowser] Cannot present alert - view not in window hierarchy or being dismissed")
|
|
1311
|
+
strongCompletionHandler()
|
|
1312
|
+
return
|
|
1313
|
+
}
|
|
1314
|
+
|
|
1257
1315
|
let alertController = UIAlertController(title: nil, message: message, preferredStyle: .alert)
|
|
1258
1316
|
alertController.addAction(UIAlertAction(title: "OK", style: .default, handler: { _ in
|
|
1259
|
-
|
|
1317
|
+
strongCompletionHandler()
|
|
1260
1318
|
}))
|
|
1261
|
-
|
|
1319
|
+
|
|
1320
|
+
// Try to present the alert
|
|
1321
|
+
do {
|
|
1322
|
+
self.present(alertController, animated: true, completion: nil)
|
|
1323
|
+
} catch {
|
|
1324
|
+
// This won't typically be triggered as present doesn't throw,
|
|
1325
|
+
// but adding as a safeguard
|
|
1326
|
+
print("[InAppBrowser] Error presenting alert: \(error)")
|
|
1327
|
+
strongCompletionHandler()
|
|
1328
|
+
}
|
|
1262
1329
|
}
|
|
1263
1330
|
}
|
|
1264
1331
|
}
|