【问题标题】:WKWebView Alert crash on iOS 13 SceneDelegateiOS 13 SceneDelegate 上的 WKWebView 警报崩溃
【发布时间】:2020-01-20 11:54:25
【问题描述】:

*** 由于未捕获的异常“NSObjectNotAvailableException”而终止应用程序,原因:“UIAlertView 已被弃用并且对于基于 UIScene 的应用程序不可用,请使用 UIAlertController!”

在 WKWebView 发出警报时,它会抛出上述错误。

UIAlertView 是从网页中的 Javascript 调用的。我没有手动调用 UIAlertView。

import UIKit
import WebKit

class ViewController: UIViewController{

    @IBOutlet weak var webView: WKWebView!
    override func viewDidLoad() {
        super.viewDidLoad()

        let url = URL(string: "https://www.google.com/maps")!
        webView.load(URLRequest(url: url))
    }
}

当我点击“您的位置”时加载地图后,应用程序崩溃。

【问题讨论】:

  • 如果您不需要 JavaScript,请在​​ WKPreferences() 中禁用 javascript。您的应用不会崩溃。
  • @Manikandan 我需要 JS。
  • 您是否在 plist 文件中添加了以下键?因为您的应用可能会尝试显示位置许可警报的警报。 NSLocationAlwaysAndWhenInUseUsageDescription,NSLocationWhenInUseUsageDescription 注意
  • @Manikandan 是的,我拥有这两个权限。
  • 尝试在你的WKWebView上设置一个UIDelegate,然后实现JavaScript调用alert()、confirm()和prompt()时调用的WKUIDelegate协议的3个方法。在您的实现中使用 UIAlertController。我想知道默认处理是否使用旧的 UIAlertView。

标签: ios swift uialertview ios13 uialertcontroller


【解决方案1】:

我能够在模拟器上重现错误(版本 13.2.2)。我做了一个快速搜索,发现我们可以提供一个替代源来处理位置权限,并为 WKWebView 提供位置更新。下面是可以完成工作的代码。

ViewController.swift

import UIKit
import WebKit
class ViewController: UIViewController {

    var navigatorGeolocation = NavigatorGeolocation();
    var web: WKWebView!
    override func viewDidLoad() {
        super.viewDidLoad()

        let webViewConfiguration = WKWebViewConfiguration();
        navigatorGeolocation.setUserContentController(webViewConfiguration: webViewConfiguration);
        web = WKWebView(frame:.zero , configuration: webViewConfiguration)
        web.navigationDelegate = self;
        navigatorGeolocation.setWebView(webView: web);
        view.addSubview(web);
        web.translatesAutoresizingMaskIntoConstraints = false
        let constraints = [
            web.topAnchor.constraint(equalTo: view.topAnchor),
            web.leadingAnchor.constraint(equalTo: view.leadingAnchor),
            web.bottomAnchor.constraint(equalTo: view.bottomAnchor),
            web.trailingAnchor.constraint(equalTo: view.trailingAnchor)
        ]
        NSLayoutConstraint.activate(constraints)
        let url = URL(string: "https://www.google.com/maps")!
        web.load(URLRequest(url: url))
    }
}
extension ViewController: WKNavigationDelegate{
    func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
        webView.evaluateJavaScript(navigatorGeolocation.getJavaScripToEvaluate());
    }
}

NavigatorGeolocation.swift

import WebKit
import CoreLocation

class NavigatorGeolocation: NSObject, WKScriptMessageHandler, CLLocationManagerDelegate {

    var locationManager = CLLocationManager();
    var listenersCount = 0;
    var webView: WKWebView!;
    var controller: WKUserContentController?

    override init() {
        super.init();
        locationManager.delegate = self;
    }

    func setUserContentController(webViewConfiguration: WKWebViewConfiguration) {
        controller = WKUserContentController();
        controller?.add(self, name: "listenerAdded")
        controller?.add(self, name: "listenerRemoved")
        webViewConfiguration.userContentController = controller!
    }

    func setWebView(webView: WKWebView) {
        self.webView = webView;
    }

    func locationServicesIsEnabled() -> Bool {
        return (CLLocationManager.locationServicesEnabled()) ? true : false;
    }

    func authorizationStatusNeedRequest(status: CLAuthorizationStatus) -> Bool {
        return (status == .notDetermined) ? true : false;
    }

    func authorizationStatusIsGranted(status: CLAuthorizationStatus) -> Bool {
        return (status == .authorizedAlways || status == .authorizedWhenInUse) ? true : false;
    }

    func authorizationStatusIsDenied(status: CLAuthorizationStatus) -> Bool {
        return (status == .restricted || status == .denied) ? true : false;
    }

    func onLocationServicesIsDisabled() {
        webView.evaluateJavaScript("navigator.geolocation.helper.error(2, 'Location services disabled');");
    }

    func onAuthorizationStatusNeedRequest() {
        locationManager.requestWhenInUseAuthorization();
    }

    func onAuthorizationStatusIsGranted() {
        locationManager.startUpdatingLocation();
    }

    func onAuthorizationStatusIsDenied() {
        webView.evaluateJavaScript("navigator.geolocation.helper.error(1, 'App does not have location permission');");
    }

    func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
        if (message.name == "listenerAdded") {
            listenersCount += 1;

            if (!locationServicesIsEnabled()) {
                onLocationServicesIsDisabled();
            }
            else if (authorizationStatusIsDenied(status: CLLocationManager.authorizationStatus())) {
                onAuthorizationStatusIsDenied();
            }
            else if (authorizationStatusNeedRequest(status: CLLocationManager.authorizationStatus())) {
                onAuthorizationStatusNeedRequest();
            }
            else if (authorizationStatusIsGranted(status: CLLocationManager.authorizationStatus())) {
                onAuthorizationStatusIsGranted();
            }
        }
        else if (message.name == "listenerRemoved") {
            listenersCount -= 1;
            // no listener left in web view to wait for position
            if (listenersCount == 0) {
                locationManager.stopUpdatingLocation();
            }
        }
    }

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        // didChangeAuthorization is also called at app startup, so this condition checks listeners
        // count before doing anything otherwise app will start location service without reason
        if (listenersCount > 0) {
            if (authorizationStatusIsDenied(status: status)) {
                onAuthorizationStatusIsDenied();
            }
            else if (authorizationStatusIsGranted(status: status)) {
                onAuthorizationStatusIsGranted();
            }
        }
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        if let location = locations.last {
            webView.evaluateJavaScript("navigator.geolocation.helper.success('\(location.timestamp)', \(location.coordinate.latitude), \(location.coordinate.longitude), \(location.altitude), \(location.horizontalAccuracy), \(location.verticalAccuracy), \(location.course), \(location.speed));");
        }
    }

    func locationManager(_ manager: CLLocationManager, didFailWithError error: Error) {
        webView.evaluateJavaScript("navigator.geolocation.helper.error(2, 'Failed to get position (\(error.localizedDescription))');");
    }

    func getJavaScripToEvaluate() -> String {
        let javaScripToEvaluate = """
            // management for success and error listeners and its calling
            navigator.geolocation.helper = {
                listeners: {},
                noop: function() {},
                id: function() {
                    var min = 1, max = 1000;
                    return Math.floor(Math.random() * (max - min + 1)) + min;
                },
                clear: function(isError) {
                    for (var id in this.listeners) {
                        if (isError || this.listeners[id].onetime) {
                            navigator.geolocation.clearWatch(id);
                        }
                    }
                },
                success: function(timestamp, latitude, longitude, altitude, accuracy, altitudeAccuracy, heading, speed) {
                    var position = {
                        timestamp: new Date(timestamp).getTime() || new Date().getTime(), // safari can not parse date format returned by swift e.g. 2019-12-27 15:46:59 +0000 (fallback used because we trust that safari will learn it in future because chrome knows that format)
                        coords: {
                            latitude: latitude,
                            longitude: longitude,
                            altitude: altitude,
                            accuracy: accuracy,
                            altitudeAccuracy: altitudeAccuracy,
                            heading: (heading > 0) ? heading : null,
                            speed: (speed > 0) ? speed : null
                        }
                    };
                    for (var id in this.listeners) {
                        this.listeners[id].success(position);
                    }
                    this.clear(false);
                },
                error: function(code, message) {
                    var error = {
                        PERMISSION_DENIED: 1,
                        POSITION_UNAVAILABLE: 2,
                        TIMEOUT: 3,
                        code: code,
                        message: message
                    };
                    for (var id in this.listeners) {
                        this.listeners[id].error(error);
                    }
                    this.clear(true);
                }
            };

            // @override getCurrentPosition()
            navigator.geolocation.getCurrentPosition = function(success, error, options) {
                var id = this.helper.id();
                this.helper.listeners[id] = { onetime: true, success: success || this.noop, error: error || this.noop };
                window.webkit.messageHandlers.listenerAdded.postMessage("");
            };

            // @override watchPosition()
            navigator.geolocation.watchPosition = function(success, error, options) {
                var id = this.helper.id();
                this.helper.listeners[id] = { onetime: false, success: success || this.noop, error: error || this.noop };
                window.webkit.messageHandlers.listenerAdded.postMessage("");
                return id;
            };

            // @override clearWatch()
            navigator.geolocation.clearWatch = function(id) {
                var idExists = (this.helper.listeners[id]) ? true : false;
                if (idExists) {
                    this.helper.listeners[id] = null;
                    delete this.helper.listeners[id];
                    window.webkit.messageHandlers.listenerRemoved.postMessage("");
                }
            };
        """;

        return javaScripToEvaluate;
    }
}

NavigatorGeolocation 代码取自 Answer 发布的 mikep

(提示:也请阅读 cmets)

输出(模拟器->调试->位置->苹果):

【讨论】:

    【解决方案2】:

    我已经在 iOS 13.4 beta 上尝试过这个(在我自己遇到这个错误之后),它似乎已经解决了。

    【讨论】:

      【解决方案3】:

      它会给你答案:

      由于未捕获的异常“NSObjectNotAvailableException”而终止应用程序,原因:“UIAlertView 已被弃用并且对于基于 UIScene 的应用程序不可用,请使用 UIAlertController!”

      你需要使用 UIAlertController

      let alert = UIAlertController(title: "My Alert", 
                                    message: "This is an alert.", 
                                    preferredStyle: .alert) 
      alert.addAction(UIAlertAction(title: NSLocalizedString("OK", comment: "Default action"), style: .default, handler: { _ in 
          print("The \"OK\" alert occured.")
      }))
      self.present(alert, animated: true, completion: nil)
      

      【讨论】:

      • 此警报由 Javascript 弹出。我没有为它编写任何代码。默认情况下,它会崩溃。
      • 网站或 javascript 一般不使用 UIAlertView,这是一个旧的 iOS 东西。您是否使用 JS 框架来创建原生应用程序?
      • 我只是在加载一个网站,并且在那个应用程序崩溃期间有一些警报。我没有添加任何JS代码。
      • 你能展示你的 WKWebView 代码吗?网站不能只加载 UIAlertView。它不是 Web 的东西,它特别是 iOS 的东西。该网站会在桌面或安卓上显示什么?它不能显示本机警报。一定有一些代码在某处创建它
      • stackoverflow.com/questions/58188069/… 检查这个。加载地图后,单击您的位置。
      【解决方案4】:

      应用程序崩溃,因为场景基础应用程序不支持 UIAlert 视图。 要解决此问题,您需要删除场景基础应用程序,您可以使用窗口基础应用程序

      您需要删除以下内容

      1 - 删除 SceneDelegate 文件

      2 - 移除 UISceneSession Lifecycle 两个方法 (configurationForConnecting connectedSceneSession 和 didDiscardSceneSessions sceneSessions )

      3 - 删除应用场景清单条目信息 plist

      现在运行项目

      确保您在 AppDelegate 中有 window 属性

      【讨论】:

      • 可以在不删除 SceneDelagate 的情况下做到这一点吗?我也想要你的 SceneDelegate。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-15
      相关资源
      最近更新 更多