wkwebview无法访问某些URL

时间:2019-07-14 00:58:23

标签: swift wkwebview

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]: 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 个答案:

答案 0 :(得分:0)

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

uploadFile(lst: Array<any>): Observable<any>
    {
        var formData = new FormData();
        lst.forEach(item =>
        {
            formData.append('file', item.fILE);
            formData.append('att', JSON.stringify(item.att));
        });
        return this.http.Post("/UploadFile", formData);
    }