Posts

Post not yet marked as solved
7 Replies
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?
Post not yet marked as solved
7 Replies
john danielPlease find below steps which indicates my actual issue.1) Download ".zip" file from server and save it on application library directory for offline use.2) ".zip" file contents html and javascript files. It works when the HTML page is simple. But if the HTML references something else, i.e. a JavaScript file also in the library directory (with an absolute path like <script src="file:////var/mobile/Containers/Data/Application/762035C9-2BF2-4CDD-B5B1-574A0E2B0728/Documents/xxxxx.js">), it will fail to load.3) I want to setup local server in my device, On application launch local server starts and upload the application library directory on local server and then load the content using local host. See below example- Using Library directory pathfile:////Users/prince/Library/Developer/CoreSimulator/Devices/75EF3DD7-DFD1-4EDB-9A6B-9FBA60F4D98C/data/Containers/Data/Application/32F774B2-E811-4281-A37A-DC8A5183B7C8/Library/Application%20Support/adededfe6c1c7bd8b5d5adb19861b9e0/Responses/36b8f6a8d6f58d58f117856dc80b0d37/abc.html- Using localhost pathhttp://localhost:8080/Application%20Support/adededfe6c1c7bd8b5d5adb19861b9e0/Responses/36b8f6a8d6f58d58f117856dc80b0d37/abc.htmlDoes anyone know why this happens, and how to resolve the issue?