0
votes

WKWebView can visit https://example.com but cannot load https://example.com/ or https://example.com/subdir/file.html from Swift. The first one and second url is able to visit however the third cannot load. And it cannot load local html files.

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]: unrecognized selector sent to instance 0x7fdb7c017a00
2019-07-14 08:51:39.266969+0800 Project4[12407:908948] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[WKWebView gotome]: unrecognized selector sent to instance 0x7fdb7c017a00'
*** First throw call stack:
(
    0   CoreFoundation                      0x000000010e97c6fb __exceptionPreprocess + 331
    1   libobjc.A.dylib                     0x000000010df20ac5 objc_exception_throw + 48
    2   CoreFoundation                      0x000000010e99aab4 -[NSObject(NSObject) doesNotRecognizeSelector:] + 132
    3   UIKitCore                           0x0000000114f6fc3d -[UIResponder doesNotRecognizeSelector:] + 287
    4   CoreFoundation                      0x000000010e981443 ___forwarding___ + 1443
    5   CoreFoundation                      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 sendEvent:] + 4079
    15  UIKitCore                           0x0000000114f5dd16 -[UIApplication sendEvent:] + 356
    16  UIKitCore                           0x000000011502e293 __dispatchPreprocessedEventFromEventQueue + 3232
    17  UIKitCore                           0x0000000115030bb9 __handleEventQueueInternal + 5911
    18  CoreFoundation                      0x000000010e8e3be1 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION__ + 17
    19  CoreFoundation                      0x000000010e8e3463 __CFRunLoopDoSources0 + 243
    20  CoreFoundation                      0x000000010e8ddb1f __CFRunLoopRun + 1231
    21  CoreFoundation                      0x000000010e8dd302 CFRunLoopRunSpecific + 626
    22  GraphicsServices                    0x00000001184f02fe GSEventRunModal + 65
    23  UIKitCore                           0x0000000114f43ba2 UIApplicationMain + 140
    24  Project4                            0x000000010d636cdb main + 75
    25  libdyld.dylib                       0x00000001117cb541 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
1
You are accessing index 2 but you can only access index 0 or 1Ahmad
It’s not the error actually, I forgot to paste the third url here and now I have updated it.victor zhao

1 Answers

0
votes

I think your array index is incorrect. In your viewDidload() change as following :

    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

    ......