Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

swift - GCDWebServer on ios : WKWebView does not show a local file

I am trying GCDWebServer on ios.

I am using Xcode_10.3 and building for ios 12.4 because my project is required to run on iPad Air.

I compiled all the good information from the related Github and various posts found :

swisspol/GCDWebServer

GCDWebServer static website on Swift

WKWebView not executing any js code

Access local files in WKWebView using JavaScript

iOS: GCDWebServer does not work on device

I have a www folder under the app root folder that contains an index.html file showing "Hello World" that I want to display in the WKWebView.

I checked that folderPath is correct and the server Logs are showing the server is up and reachable at the usual address.

I even added a label in the story board therefore I am sure that the built app runs but it does not display the expected "Hello World"

What did I do wrong ?

Thanks for help.

My ViewController.swift file :

import UIKit
import WebKit
import GCDWebServer

class ViewController: UIViewController {

    @IBOutlet weak var webView: WKWebView!
    
    var webServer = GCDWebServer()

    override func viewDidLoad() {
        super.viewDidLoad()
    
        let folderPath = Bundle.main.path(forResource: "www", ofType: nil)
        
        webServer.addGETHandler(forBasePath: "/", directoryPath: folderPath!, indexFilename: "index.html", cacheAge: 0, allowRangeRequests: true)
        
        webServer.start(withPort: 8080, bonjourName: "GCD Web Server")

        
        let myURL = URL(string:"http://localhost:8080/index.html")
        //let myURL = URL(string:"https://www.apple.com") // OK

        let myRequest = URLRequest(url: myURL!)
        
        webView.load(myRequest)
    }


}

The full Xcode project is here :

https://www.dropbox.com/s/j03nh5mcmqapxdu/test_server.zip?dl=0

Thanks for help.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to MLink Developer Q&A Community for programmer and developer-Open, Learning and Share
...