From 455b2c51eed42f70c554e97aec92214205c6da9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julio=20C=C3=A9sar?= Date: Wed, 10 Jan 2018 18:11:33 +0100 Subject: [PATCH 1/2] Inject cordova.js and cordova plugins js --- ios/Capacitor/Capacitor/CAPBridge.swift | 18 ++++++++++ ios/Capacitor/Capacitor/JSExport.swift | 45 +++++++++++++++++++++++++ 2 files changed, 63 insertions(+) diff --git a/ios/Capacitor/Capacitor/CAPBridge.swift b/ios/Capacitor/Capacitor/CAPBridge.swift index 3c15703708..08c06cc932 100644 --- a/ios/Capacitor/Capacitor/CAPBridge.swift +++ b/ios/Capacitor/Capacitor/CAPBridge.swift @@ -33,7 +33,9 @@ enum BridgeError: Error { self.userContentController = userContentController super.init() exportCoreJS() + exportCordovaJS() registerPlugins() + registerCordovaPlugins() bindObservers() } @@ -87,6 +89,14 @@ enum BridgeError: Error { } } + func exportCordovaJS() { + do { + try JSExport.exportCordovaJS(userContentController: self.userContentController) + } catch { + CAPBridge.fatalError(error, error) + } + } + func registerPlugins() { var numClasses = UInt32(0); let classes = objc_copyClassList(&numClasses) @@ -146,6 +156,14 @@ enum BridgeError: Error { return self.dispatchQueue } + func registerCordovaPlugins() { + do { + try JSExport.exportCordovaPluginsJS(userContentController: self.userContentController) + } catch { + CAPBridge.fatalError(error, error) + } + } + public func isSimulator() -> Bool { var isSimulator = false #if arch(i386) || arch(x86_64) diff --git a/ios/Capacitor/Capacitor/JSExport.swift b/ios/Capacitor/Capacitor/JSExport.swift index 1f376380e9..f6c13dc9a2 100644 --- a/ios/Capacitor/Capacitor/JSExport.swift +++ b/ios/Capacitor/Capacitor/JSExport.swift @@ -23,6 +23,22 @@ public class JSExport { } } + public static func exportCordovaJS(userContentController: WKUserContentController) throws { + guard let jsUrl = Bundle.main.url(forResource: "public/cordova", withExtension: "js") else { + print("ERROR: Required cordova.js file in Avocado not found. Bridge will not function!") + throw BridgeError.errorExportingCoreJS + } + + do { + let data = try String(contentsOf: jsUrl, encoding: .utf8) + let userScript = WKUserScript(source: data, injectionTime: .atDocumentStart, forMainFrameOnly: true) + userContentController.addUserScript(userScript) + } catch { + print("ERROR: Unable to read required cordova.js file from Avocado framework. Bridge will not function!") + throw BridgeError.errorExportingCoreJS + } + } + /** * Export the JS required to implement the given plugin. */ @@ -109,4 +125,33 @@ public class JSExport { lines.append("}") return lines.joined(separator: "\n") } + + public static func exportCordovaPluginsJS(userContentController: WKUserContentController) throws { + if let pluginsJSFolder = Bundle.main.url(forResource: "public/plugins", withExtension: nil) { + self.injectFilesForFolder(folder: pluginsJSFolder, userContentController: userContentController) + } + } + + static func injectFilesForFolder(folder: URL, userContentController: WKUserContentController) { + let fileManager = FileManager.default + do { + let fileURLs = try fileManager.contentsOfDirectory(at: folder, includingPropertiesForKeys: nil, options: []) + for fileURL in fileURLs { + if fileURL.hasDirectoryPath { + injectFilesForFolder(folder: fileURL, userContentController: userContentController) + } else { + do { + let data = try String(contentsOf: fileURL, encoding: .utf8) + let userScript = WKUserScript(source: data, injectionTime: .atDocumentStart, forMainFrameOnly: true) + userContentController.addUserScript(userScript) + } catch { + print("Unable to inject js file from plugins folder") + } + } + } + } catch { + print("Error while enumerating files") + } + } + } From 787c00cd156ef0334f44532cf917f6a68aa9f341 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julio=20C=C3=A9sar?= Date: Thu, 11 Jan 2018 12:02:56 +0100 Subject: [PATCH 2/2] Inject cordova files only if needed --- ios/Capacitor/Capacitor/CAPBridge.swift | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/ios/Capacitor/Capacitor/CAPBridge.swift b/ios/Capacitor/Capacitor/CAPBridge.swift index 08c06cc932..b96f5218aa 100644 --- a/ios/Capacitor/Capacitor/CAPBridge.swift +++ b/ios/Capacitor/Capacitor/CAPBridge.swift @@ -33,9 +33,8 @@ enum BridgeError: Error { self.userContentController = userContentController super.init() exportCoreJS() - exportCordovaJS() + setupCordovaCompatibility() registerPlugins() - registerCordovaPlugins() bindObservers() } @@ -88,7 +87,24 @@ enum BridgeError: Error { CAPBridge.fatalError(error, error) } } - + + func setupCordovaCompatibility() { + var injectCordovaFiles = false + var numClasses = UInt32(0); + let classes = objc_copyClassList(&numClasses) + for i in 0..