From e5a1c81fe81904dfd7e3f5100a04088173effc1c Mon Sep 17 00:00:00 2001 From: Chace Daniels Date: Fri, 23 Jun 2023 09:00:51 -0700 Subject: [PATCH] fix(ios): set cors headers in asset handler for live reload --- .../Capacitor/CAPBridgeViewController.swift | 1 + ios/Capacitor/Capacitor/CAPWebView.swift | 1 + ios/Capacitor/Capacitor/WebViewAssetHandler.swift | 12 ++++++++++++ 3 files changed, 14 insertions(+) diff --git a/ios/Capacitor/Capacitor/CAPBridgeViewController.swift b/ios/Capacitor/Capacitor/CAPBridgeViewController.swift index f08e53a97..56f243e1a 100644 --- a/ios/Capacitor/Capacitor/CAPBridgeViewController.swift +++ b/ios/Capacitor/Capacitor/CAPBridgeViewController.swift @@ -44,6 +44,7 @@ import Cordova // get the web view let assetHandler = WebViewAssetHandler(router: router()) assetHandler.setAssetPath(configuration.appLocation.path) + assetHandler.setServerUrl(configuration.serverURL) let delegationHandler = WebViewDelegationHandler() prepareWebView(with: configuration, assetHandler: assetHandler, delegationHandler: delegationHandler) view = webView diff --git a/ios/Capacitor/Capacitor/CAPWebView.swift b/ios/Capacitor/Capacitor/CAPWebView.swift index dde6dda81..244a16938 100644 --- a/ios/Capacitor/Capacitor/CAPWebView.swift +++ b/ios/Capacitor/Capacitor/CAPWebView.swift @@ -28,6 +28,7 @@ open class CAPWebView: UIView { private lazy var assetHandler: WebViewAssetHandler = { let handler = WebViewAssetHandler(router: router) handler.setAssetPath(configuration.appLocation.path) + handler.setServerUrl(bridge.config.serverURL) return handler }() diff --git a/ios/Capacitor/Capacitor/WebViewAssetHandler.swift b/ios/Capacitor/Capacitor/WebViewAssetHandler.swift index 6dce404ef..187ff360c 100644 --- a/ios/Capacitor/Capacitor/WebViewAssetHandler.swift +++ b/ios/Capacitor/Capacitor/WebViewAssetHandler.swift @@ -5,6 +5,7 @@ import MobileCoreServices // swiftlint:disable type_body_length internal class WebViewAssetHandler: NSObject, WKURLSchemeHandler { private var router: Router + private var serverUrl: URL? init(router: Router) { self.router = router @@ -15,6 +16,10 @@ internal class WebViewAssetHandler: NSObject, WKURLSchemeHandler { router.basePath = assetPath } + func setServerUrl(_ serverUrl: URL?) { + self.serverUrl = serverUrl + } + func webView(_ webView: WKWebView, start urlSchemeTask: WKURLSchemeTask) { let startPath: String let url = urlSchemeTask.request.url! @@ -36,6 +41,13 @@ internal class WebViewAssetHandler: NSObject, WKURLSchemeHandler { "Content-Type": mimeType, "Cache-Control": "no-cache" ] + + // if using live reload, then set CORS headers + if self.serverUrl != nil && self.serverUrl?.scheme != localUrl.scheme { + headers["Access-Control-Allow-Origin"] = self.serverUrl?.absoluteString + headers["Access-Control-Allow-Methods"] = "GET, OPTIONS" + } + if let rangeString = urlSchemeTask.request.value(forHTTPHeaderField: "Range"), let totalSize = try fileUrl.resourceValues(forKeys: [.fileSizeKey]).fileSize, isMediaExtension(pathExtension: url.pathExtension) {