【问题标题】:wkwebview cannot visit certain urlswkwebview 无法访问某些 url
【发布时间】:2019-11-23 05:12:46
【问题描述】:

WKWebView 可以访问 https://example.com,但不能从 Swift 加载 https://example.com/https://example.com/subdir/file.html。 第一个和第二个 url 可以访问,但第三个无法加载。 并且无法加载本地html文件。

import UIKit
import WebKit

class ViewController: UIViewController, WKNavigationDelegate {
    var webView: WKWebView!
    var progressView: UIProgressView!

    var websites = ["apple.com", "hackingwithswift.com", "www.apple.com/index.html"]

    override func loadView() {
        webView = WKWebView()
        webView.navigationDelegate = self
        view = webView
    }

    override func viewDidLoad() {
        super.viewDidLoad()

//        let url = Bundle.main.url(forResource: "publish", withExtension: "html", subdirectory: "website")!
        let url = URL(string: "http://" + websites[2])!
//        webView.loadFileURL(url, allowingReadAccessTo: url)
        webView.load(URLRequest(url: url))
        webView.allowsBackForwardNavigationGestures = true

......
2019-07-14 08:51:39.261090+0800 Project4[12407:908948]-[WKWebView gotome]:无法识别的选择器发送到实例 0x7fdb7c017a00 2019-07-14 08:51:39.266969+0800 Project4[12407:908948] *** 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因:“-[WKWebView gotome]:无法识别的选择器发送到实例 0x7fdb7c017a00” *** 首先抛出调用堆栈: ( 0 核心基础 0x000000010e97c6fb __exceptionPreprocess + 331 1 libobjc.A.dylib 0x000000010df20ac5 objc_exception_throw + 48 2核心基础0x000000010e99aab4-[NSObject(NSObject)不识别选择器:]+132 3 UIKitCore 0x0000000114f6fc3d-[UIResponder doesNotRecognizeSelector:] + 287 4 核心基础 0x000000010e981443 ___转发___ + 1443 5 核心基础 0x000000010e983238 _CF_forwarding_prep_0 + 120 6 UIKitCore 0x0000000114f45204-[UIApplication sendAction:to:from:forEvent:] + 83 7 UIKitCore 0x00000001146af963 __45-[_UIButtonBarTargetAction _invoke:forEvent:]_block_invoke + 154 8 UIKitCore 0x00000001146af89c-[_UIButtonBarTargetAction_invoke:forEvent:] + 152 9 UIKitCore 0x0000000114f45204-[UIApplication sendAction:to:from:forEvent:] + 83 10 UIKitCore 0x000000011499ac19-[UIControl sendAction:to:forEvent:] + 67 11 UIKitCore 0x000000011499af36-[UIControl_sendActionsForEvents:withEvent:] + 450 12 UIKitCore 0x0000000114999eec-[UIControl touchesEnded:withEvent:] + 583 13 UIKitCore 0x0000000114f7deee-[UIWindow_sendTouchesForEvent:] + 2547 14 UIKitCore 0x0000000114f7f5d2 -[UIWindow 发送事件:] + 4079 15 UIKitCore 0x0000000114f5dd16 -[UIApplication sendEvent:] + 356 16 UIKitCore 0x000000011502e293 __dispatchPreprocessedEventFromEventQueue + 3232 17 UIKitCore 0x0000000115030bb9 __handleEventQueueInternal + 5911 18 核心基础 0x000000010e8e3be1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17 19 核心基础 0x000000010e8e3463 __CFRunLoopDoSources0 + 243 20 核心基础 0x000000010e8ddb1f __CFRunLoopRun + 1231 21 核心基础 0x000000010e8dd302 CFRunLoopRunSpecific + 626 22 图形服务 0x00000001184f02fe GSEventRunModal + 65 23 UIKitCore 0x0000000114f43ba2 UIApplicationMain + 140 24 项目 4 0x000000010d636cdb 主要 + 75 25 libdyld.dylib 0x00000001117cb541 开始 + 1 ) libc++abi.dylib:以 NSException 类型的未捕获异常终止

【问题讨论】:

  • 您正在访问索引2,但您只能访问索引01
  • 其实不是错误,第三个url忘记贴了,现在更新了。

标签: swift wkwebview


【解决方案1】:

我认为您的数组索引不正确。在您的 viewDidload() 更改如下:

    import UIKit
    import WebKit

    class ViewController: UIViewController, WKNavigationDelegate {
        var webView: WKWebView!
        var progressView: UIProgressView!

        var websites = ["apple.com", "hackingwithswift.com"]

        override func loadView() {
            webView = WKWebView()
            webView.navigationDelegate = self
            view = webView
        }

        override func viewDidLoad() {
            super.viewDidLoad()

    //        let url = Bundle.main.url(forResource: "publish", withExtension: "html", subdirectory: "website")!

//------Correction--------\\\

            let url = URL(string: "http://" + websites[1])! /// you can use index 0 / 1 according to site you wanna show in Web view \\\

//------Correction--------\\\

    //        webView.loadFileURL(url, allowingReadAccessTo: url)
            webView.load(URLRequest(url: url))
            webView.allowsBackForwardNavigationGestures = true

    ......

【讨论】:

  • 您可能只是指出了包含潜在错误的行。再次发布完整的代码没有意义。
  • 我不这么认为。根据他/她的问题类型和错误,他清楚地表明他是 Swift / iOS 的新手。我总是尝试为新手提供详细信息并给出解释清楚的答案,希望他们理解他们的错误并轻松纠正他们的代码。希望你明白我的意思。
猜你喜欢
  • 1970-01-01
  • 2022-12-21
  • 2013-10-27
  • 1970-01-01
  • 2017-07-29
  • 2021-09-18
  • 2017-02-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多