Ios 如何修复从我自己的类加载webview url时出现的黑屏

Ios 如何修复从我自己的类加载webview url时出现的黑屏,ios,swift,wkwebview,Ios,Swift,Wkwebview,我正在创建一个SDK来从用户那里获取url并加载。所以我正在用WKWebview创建我自己的类。但我得到了一些关于实例成员webView不能用于MyWebViewUIview类型的问题 我的代码: import Foundation import WebKit public class MyWebView: UIView, WKNavigationDelegate { // initialize the view var webView: WKWebView! /

我正在创建一个SDK来从用户那里获取url并加载。所以我正在用WKWebview创建我自己的类。但我得到了一些关于实例成员webView不能用于MyWebViewUIview类型的问题

我的代码:

import Foundation
import WebKit

public class MyWebView: UIView, WKNavigationDelegate {

    // initialize the view
     var webView: WKWebView!

    // get the url and load the page
    public func passUrl(url: String) {
        guard let url = URL(string: url) else { return }
        webView = WKWebView()
        webView.navigationDelegate = self
        webView.load(URLRequest(url: url))
        webView.allowsBackForwardNavigationGestures = true
        webView.addObserver(self, forKeyPath: "URL", options: .new, context: nil)
    }
    
    // Observe value
    override public func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
        if let key = change?[NSKeyValueChangeKey.newKey] {
            print("URL Changes: \(key)")
            let alert = UIAlertController(title: "URL Changed", message: "\(key)", preferredStyle: .alert)
            alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: nil))
            alert.addAction(UIAlertAction(title: "No", style: .cancel, handler: nil))
            self.window?.rootViewController?.present(alert, animated: true, completion: nil)
        }
    }
    
}
我的视图控制器:

class ViewController: UIViewController {

    var webView =  MyWebView()
    

    override func viewDidLoad() {
        super.viewDidLoad()
        view = webView
        webView.passUrl(url: "https://www.swiggy.com")
    }
}
当我跑步时,我的屏幕变黑了。不知道我在做什么。任何帮助都会很好


谢谢

我不会将您的web视图分配给视图控制器中的主视图。尝试将其添加为子视图:

webView.translatesAutoresizingMaskIntoConstraints=false view.addSubviewwebView NSLayoutConstraint.activate[ webView.topAnchor.constraintequalTo:view.topAnchor, webView.leadingAnchor.constraintequalTo:view.leadingAnchor, webView.trailingAnchor.constraintequalTo:view.trailingAnchor, webView.bottomAnchor.constraintequalTo:view.bottomAnchor, ] 按如下方式更新passUrl ass:

public func passUrlurl:字符串{ guard let url=URLstring:url else{return} webView=WKWebView webView.navigationDelegate=self webView.loadURLRequesturl:url webView.AllowsBackforwardNavigationPigests=true webView.addObserverself,forKeyPath:URL,选项:。新建,上下文:nil webView.translatesAutoresizingMaskIntoConstraints=false addSubviewwebView NSLayoutConstraint.activate[ webView.topAnchor.constraintequalTo:topAnchor, webView.leadingAnchor.constraintequalTo:leadingAnchor, webView.trailingAnchor.constraintequalTo:trailingAnchor, webView.bottomAnchor.constraintequalTo:bottomAnchor, ] } 这样你就可以看到网页了

由于只能调用passUrl方法一次,否则它将继续创建web视图,并将它们添加到视图层次结构中,因此我将如下更新MyWebView类:

公共类MyWebView:UIView,WKNavigationDelegate{ //初始化视图 var-webView:WKWebView! //重写init以仅创建WKWebView的一个实例并将其添加到视图中 公共重写initframe:CGRect{ super.initframe:frame webView=WKWebView webView.navigationDelegate=self webView.AllowsBackforwardNavigationPigests=true webView.addObserverself,forKeyPath:URL,选项:。新建,上下文:nil webView.translatesAutoresizingMaskIntoConstraints=false addSubviewwebView NSLayoutConstraint.activate[ webView.topAnchor.constraintequalTo:topAnchor, webView.leadingAnchor.constraintequalTo:leadingAnchor, webView.trailingAnchor.constraintequalTo:trailingAnchor, webView.bottomAnchor.constraintequalTo:bottomAnchor, ] } 所需初始编码:NSCoder{ fatalErrorinitcoder:尚未实现 } //获取url并加载页面 public func passUrlurl:字符串{ guard let url=URLstring:url else{return} webView.loadURLRequesturl:url } //观察值 重写公共函数observeValueforKeyPath键路径:字符串?,对象的类型:任何?,更改:[NSKeyValueChangeKey:Any]?,上下文:UnsaFemeutableRawPointer{ 如果let key=change?[NSKeyValueChangeKey.newKey]{ printURL更改:\key let alert=UIAlertControllertitle:URL已更改,消息:\key,首选样式:。警报 alert.addActionUIAlertActiontitle:是,样式:。默认值,处理程序:nil alert.addActionUIAlertActiontitle:否,样式:。取消,处理程序:nil self.window?.rootViewController?.presentalert,动画:true,完成:nil } } } 我在你发布的项目上试用过,效果很好


我不会将您的web视图指定给视图控制器中的主视图。尝试将其添加为子视图:

webView.translatesAutoresizingMaskIntoConstraints=false view.addSubviewwebView NSLayoutConstraint.activate[ webView.topAnchor.constraintequalTo:view.topAnchor, webView.leadingAnchor.constraintequalTo:view.leadingAnchor, webView.trailingAnchor.constraintequalTo:view.trailingAnchor, webView.bottomAnchor.constraintequalTo:view.bottomAnchor, ] 按如下方式更新passUrl ass:

public func passUrlurl:字符串{ guard let url=URLstring:url else{return} webView=WKWebView webView.navigationDelegate=self webView.loadURLRequesturl:url webView.AllowsBackforwardNavigationPigests=true webView.addObserverself,forKeyPath:URL,选项:。新建,上下文:nil webView.translatesAutoresizingMaskIntoConstraints=false addSubviewwebView NSLayoutConstraint.activate[ webView.topAnchor.constraintequalTo:topAnchor, webView.leadingAnchor.constraintequalTo:leadingAnchor, webView.trailingAnchor.constraintequalTo:trailingAnchor, webView.bottomAnchor.constraintequalTo:bottomAnchor, ] } 这样你就应该 我想看看网页

由于只能调用passUrl方法一次,否则它将继续创建web视图,并将它们添加到视图层次结构中,因此我将如下更新MyWebView类:

公共类MyWebView:UIView,WKNavigationDelegate{ //初始化视图 var-webView:WKWebView! //重写init以仅创建WKWebView的一个实例并将其添加到视图中 公共重写initframe:CGRect{ super.initframe:frame webView=WKWebView webView.navigationDelegate=self webView.AllowsBackforwardNavigationPigests=true webView.addObserverself,forKeyPath:URL,选项:。新建,上下文:nil webView.translatesAutoresizingMaskIntoConstraints=false addSubviewwebView NSLayoutConstraint.activate[ webView.topAnchor.constraintequalTo:topAnchor, webView.leadingAnchor.constraintequalTo:leadingAnchor, webView.trailingAnchor.constraintequalTo:trailingAnchor, webView.bottomAnchor.constraintequalTo:bottomAnchor, ] } 所需初始编码:NSCoder{ fatalErrorinitcoder:尚未实现 } //获取url并加载页面 public func passUrlurl:字符串{ guard let url=URLstring:url else{return} webView.loadURLRequesturl:url } //观察值 重写公共函数observeValueforKeyPath键路径:字符串?,对象的类型:任何?,更改:[NSKeyValueChangeKey:Any]?,上下文:UnsaFemeutableRawPointer{ 如果let key=change?[NSKeyValueChangeKey.newKey]{ printURL更改:\key let alert=UIAlertControllertitle:URL已更改,消息:\key,首选样式:。警报 alert.addActionUIAlertActiontitle:是,样式:。默认值,处理程序:nil alert.addActionUIAlertActiontitle:否,样式:。取消,处理程序:nil self.window?.rootViewController?.presentalert,动画:true,完成:nil } } } 我在你发布的项目上试用过,效果很好


试试这个代码。为我测试和工作。 MyWebView类

ViewController类


检查它,如果这不能解决问题,请告诉我。

尝试此代码。为我测试和工作。 MyWebView类

ViewController类



检查它,如果它不能解决问题,请让我知道。

这是在我的视图控制器或mywebview类中?它代替了view=WebView我在这里观看了我的示例项目哦,是的,我错过了。创建的子视图会生成web视图,但不会将其添加到视图中。我将更新我的答案。在我的视图控制器和mywebview类中,。我确实喜欢这个webView=MyWebView创建这样的新实例。这是正确的方法还是我做错了这是在我的视图控制器中还是在我的WebView类中?它不是view=WebView我在这里观看了我的示例项目哦,是的,我错过了。创建的子视图会生成web视图,但不会将其添加到视图中。我将更新我的答案。在我的视图控制器和mywebview类中,。我确实喜欢这个webView=MyWebView创建这样的新实例。这是正确的方法还是我做错了我有一个疑问。在同一个mywebview类中,我需要调用一些alamofire请求,并将该响应值用作警报。与我的视图控制器无关。如果我安装alamofire并在mywebview类中使用它。当我创建SDK时,用户是否需要安装alamofire pod?您可以安装alamofire并为alamofire创建不同的类,您可以在ViewController中调用alamofire请求。我的问题是你说的用户是谁?比方说,我正在创建一个SDK,它将显示某个web视图URL。因此,我正在创建自己的类并在视图控制器中使用它。直到我做完这件事。但我需要在我自己的类中调用一个AlamofireAPI,并在web视图加载后显示一些警报。此alamofire与我的视图控制器无关。当我从视图控制器获取url时,我需要调用我的AlamofireAPI。我的疑问是,1。我是否需要在我自己的类项目中安装pod,然后需要创建一个SDK 2。或者我会要求用户导入alamofire并使用pod安装到他们的项目中?安装alamofire pod并导入alamofire。我是否应该告诉正在使用我的SDK安装alamofire pod并导入alamofire的用户。或者我应该安装alamofire吊舱并导入alamofire,然后我需要完全创建SDK?我有一个疑问。在同一个mywebview类中,我需要调用一些alamofire请求,并将该响应值用作警报。与我的视图控制器无关。如果我安装alamofire并在mywebview类中使用它。当我创建SDK时,用户是否需要安装alamofire pod?您可以安装alamofire并为alamofire创建不同的类,您可以在ViewController中调用alamofire请求。我的问题是你说的用户是谁?比方说,我正在创建一个SDK whi
ch将显示某个web视图URL。因此,我正在创建自己的类并在视图控制器中使用它。直到我做完这件事。但我需要在我自己的类中调用一个AlamofireAPI,并在web视图加载后显示一些警报。此alamofire与我的视图控制器无关。当我从视图控制器获取url时,我需要调用我的AlamofireAPI。我的疑问是,1。我是否需要在我自己的类项目中安装pod,然后需要创建一个SDK 2。或者我会要求用户导入alamofire并使用pod安装到他们的项目中?安装alamofire pod并导入alamofire。我是否应该告诉正在使用我的SDK安装alamofire pod并导入alamofire的用户。或者我应该安装alamofire吊舱并导入alamofire,然后我需要完全创建SDK?
import Foundation
import WebKit

public class MyWebView: UIView, WKNavigationDelegate {

// initialize the view
let view: WKWebView = {
    let view = WKWebView()
    return view
}()

// get the url and load the page
public func passUrl(url: String) {
    guard let url = URL(string: url) else { return }
    view.navigationDelegate = self
    view.load(URLRequest(url: url))
    view.allowsBackForwardNavigationGestures = true
    view.addObserver(self, forKeyPath: "URL", options: .new, context: nil)
}

// Observe value
override public func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
    if let key = change?[NSKeyValueChangeKey.newKey] {
        print("URL Changes: \(key)")
        let alert = UIAlertController(title: "URL Changed", message: "\(key)", preferredStyle: .alert)
        alert.addAction(UIAlertAction(title: "Yes", style: .default, handler: nil))
        alert.addAction(UIAlertAction(title: "No", style: .cancel, handler: nil))
        self.window?.rootViewController?.present(alert, animated: true, completion: nil)
    }
}

public override init(frame: CGRect) {
    super.init(frame: frame)
    setupView()
}

required init?(coder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

private func setupView() {
    view.translatesAutoresizingMaskIntoConstraints = false
    addSubview(view)
    NSLayoutConstraint.activate([
        view.topAnchor.constraint(equalTo: topAnchor),
        view.leadingAnchor.constraint(equalTo: leadingAnchor),
        view.trailingAnchor.constraint(equalTo: trailingAnchor),
        view.bottomAnchor.constraint(equalTo: bottomAnchor),
    ])
}
}
import UIKit

class ViewController: UIViewController {

var webView =  MyWebView()

override func viewDidLoad() {
    super.viewDidLoad()
    webView.passUrl(url: "https://www.swiggy.com")
}

override func loadView() {
    view = webView
}
}