Post

Replies

Boosts

Views

Activity

Reply to load local file in webview ios using http protocol. (Create Webview server in iOS)
Hereby i'm sharing the whole demo code that i'm trying to achive the result :CocoaPod Used :platform :ios, '11.0'inhibit_all_warnings!use_frameworks!target 'LocalWKWebView' do pod 'Alamofire' source 'https://github.com/CocoaPods/Specs.git' pod 'Zip', '~> 1.1' pod 'Swifter', '~> 1.4.7'endSource Code :import UIKitimport WebKitimport Alamofireimport Zipimport Swifterimport Dispatch//class ViewController: UIViewController, WKNavigationDelegate {class ViewController: UIViewController,UIWebViewDelegate { @IBOutlet weak var webView : UIWebView! /* Used to download and unzip the zip file with the custom CSS, JS */ func downloadFile(completionHandler: @escaping (URL?, Error?) -> ()) { let destination: DownloadRequest.DownloadFileDestination = { _, _ in let documentsURL = FileManager.default.urls(for: .documentDirectory, in: .userDomainMask)[0] let fileURL = documentsURL.appendingPathComponent("314431eb61594e1f9ad759e579760ef8.zip") return (fileURL, [.removePreviousFile, .createIntermediateDirectories]) } let downloadURL = "https://abc.s3.amazonaws.com/314431eb61594e1f9ad759e579760ef8.zip" let downloadParameters : Parameters = ["":""] Alamofire.download(downloadURL, method: .get, parameters: downloadParameters, encoding: JSONEncoding.default, to: destination) .downloadProgress { progress in print("Download Progress: \(progress.fractionCompleted)") } .response(completionHandler: { (DefaultDownloadResponse) in if DefaultDownloadResponse.response?.statusCode == 200 { let fm = FileManager.default let documentsURL = try! fm.url(for:.documentDirectory, in: .userDomainMask, appropriateFor: nil, create: false) let fileURL = documentsURL.appendingPathComponent("314431eb61594e1f9ad759e579760ef8.zip") if(fm.fileExists(atPath: fileURL.path)) { print("Download complete at: \(fileURL.absoluteString)") do { try Zip.unzipFile(fileURL, destination: documentsURL, overwrite: true, password: "", progress: { (progress) -> () in print("Unzip Progress: \(progress)") }) completionHandler(fileURL, nil) } catch { print("Couldn't unzip") completionHandler(nil, error) } } } }) } override func viewDidLoad() { super.viewDidLoad() let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0] as String let url = NSURL(fileURLWithPath: path) if let pathComponent = url.appendingPathComponent("314431eb61594e1f9ad759e579760ef8") { let filePath = pathComponent.path let fileManager = FileManager.default if fileManager.fileExists(atPath: filePath) { print("FILE AVAILABLE") let folderURL = url let htmlPath = folderURL.appendingPathComponent("ator.html")?.path let folderPath = folderURL.path print(folderPath) let baseUrl = URL(fileURLWithPath: folderPath!, isDirectory: true) print(baseUrl) let htmlURL = URL(fileURLWithPath: htmlPath!, isDirectory: false) // webView.loadFileURL(htmlURL, allowingReadAccessTo: folderURL!) //Thitrd party swiftwer used to create localhost server let httpServer = demoServer(folderPath ?? "") // httpServer["/:prince"] = shareFilesFromDirectory(Bundle.main.path(forResource: "Web", ofType: nil)!) httpServer["/:path"] = shareFilesFromDirectory(folderPath ?? "") do { try httpServer.start(8080) let myRequest = NSURLRequest(url: NSURL(string: "http://localhost:8080/story_html5.html")! as URL) print("myRequest>>",myRequest) self.webView.loadRequest(myRequest as URLRequest) }catch{ print("erororor") } } else { print("FILE NOT AVAILABLE") self.downlaodfileandunzip() } } else { print("FILE PATH NOT AVAILABLE") self.downlaodfileandunzip() } } func downlaodfileandunzip(){ let loadCustomFiles = true if(loadCustomFiles) { // download & load custom files from remote URL downloadFile(completionHandler: { url, error in if(url != nil) { let folderURL = url?.deletingPathExtension() let htmlPath = folderURL?.appendingPathComponent("ator.html").path let folderPath = folderURL?.path print(folderPath) let baseUrl = URL(fileURLWithPath: folderPath!, isDirectory: true) print(baseUrl) let htmlURL = URL(fileURLWithPath: htmlPath!, isDirectory: false) // webView.loadFileURL(htmlURL, allowingReadAccessTo: folderURL!) let httpServer = demoServer(folderPath ?? "") // httpServer["/:prince"] = shareFilesFromDirectory(Bundle.main.path(forResource: "Web", ofType: nil)!) httpServer["/:path"] = shareFilesFromDirectory(folderPath ?? "") do { try httpServer.start(8080) let myRequest = NSURLRequest(url: NSURL(string: "http://localhost:8080/ator.html.html")! as URL) print("myRequest>>",myRequest) self.webView.loadRequest(myRequest as URLRequest) }catch{ print("erororor") } } else { print(error) } }) } else { // load normal files from /web let htmlPath = Bundle.main.path(forResource: "index", ofType: "html") let folderPath = Bundle.main.bundlePath let baseUrl = URL(fileURLWithPath: folderPath, isDirectory: true) do { let htmlString = try NSString(contentsOfFile: htmlPath!, encoding: String.Encoding.utf8.rawValue) webView.loadHTMLString(htmlString as String, baseURL: baseUrl) // / webView.navigationDelegate = self self.view = webView } catch { // error handling } } } override func didReceiveMemoryWarning() { super.didReceiveMemoryWarning() }}issue : I'm trying to load the file using localhost as well as using webView.loadFileURL(htmlURL, allowingReadAccessTo: folderURL!) but it wont work.Reference link Stack Overflow : https://stackoverflow.com/questions/58946593/load-local-file-in-webview-ios-using-http-protocol-create-webview-server-in-anDropBox : https://www.dropbox.com/sh/70fgj7bwcmwz0k9/AACR5sLB-zrE7rkEmta_GoVqa?dl=0Can anyone help me with the localhost server setup?
Nov ’19