diff --git a/CapacitorInappbrowser.podspec b/CapacitorInappbrowser.podspec index 313598b..d361efa 100644 --- a/CapacitorInappbrowser.podspec +++ b/CapacitorInappbrowser.podspec @@ -12,7 +12,7 @@ Pod::Spec.new do |s| s.source = { :git => package['repository']['url'], :tag => s.version.to_s } s.source_files = 'ios/Sources/InAppBrowserPlugin/*.{swift,h,m,c,cc,mm,cpp}' s.ios.deployment_target = '14.0' - s.dependency 'OSInAppBrowserLib', spec='~> 2.0.1' + s.dependency 'OSInAppBrowserLib', spec='~> 2.1.0' s.dependency 'Capacitor' s.swift_version = '5.1' end diff --git a/README.md b/README.md index e289b23..66707c2 100644 --- a/README.md +++ b/README.md @@ -208,9 +208,10 @@ Removes all listeners for the browser events. Defines the options for opening a URL in the web view. -| Prop | Type | Description | -| ------------- | --------------------------------------------------------- | -------------------------------------------------------------------- | -| **`options`** | WebViewOptions | A structure containing some configurations to apply to the Web View. | +| Prop | Type | Description | +| ------------------- | --------------------------------------------------------- | -------------------------------------------------------------------- | +| **`options`** | WebViewOptions | A structure containing some configurations to apply to the Web View. | +| **`customHeaders`** | { [key: string]: string; } | A map of custom headers to be sent with the request. | #### WebViewOptions diff --git a/android/build.gradle b/android/build.gradle index 7322aac..dd98a8b 100644 --- a/android/build.gradle +++ b/android/build.gradle @@ -58,7 +58,7 @@ repositories { dependencies { // implementation fileTree(dir: 'libs', include: ['*.jar']) implementation project(':capacitor-android') - implementation "io.ionic.libs:ioninappbrowser-android:1.3.0" + implementation "io.ionic.libs:ioninappbrowser-android:1.4.0" implementation 'androidx.browser:browser:1.8.0' implementation "androidx.constraintlayout:constraintlayout:2.2.0" implementation "androidx.appcompat:appcompat:$androidxAppCompatVersion" diff --git a/android/src/main/java/com/capacitorjs/osinappbrowser/InAppBrowserPlugin.kt b/android/src/main/java/com/capacitorjs/osinappbrowser/InAppBrowserPlugin.kt index cd431ed..9b5bd99 100644 --- a/android/src/main/java/com/capacitorjs/osinappbrowser/InAppBrowserPlugin.kt +++ b/android/src/main/java/com/capacitorjs/osinappbrowser/InAppBrowserPlugin.kt @@ -73,12 +73,25 @@ class InAppBrowserPlugin : Plugin() { handleBrowserCall(call, OSInAppBrowserTarget.WEB_VIEW) { url -> val options = call.getObject("options") ?: return@handleBrowserCall sendErrorResult(call, OSInAppBrowserError.InputArgumentsIssue(OSInAppBrowserTarget.WEB_VIEW)) + + val customHeaders: Map? = call.getObject("customHeaders")?.let { jsObject -> + val result = mutableMapOf() + jsObject.keys().forEach { key -> + when (val value = jsObject.opt(key)) { + is String -> result[key] = value + is Number -> result[key] = value.toString() + } + } + result + } + close { val webViewOptions = buildWebViewOptions(options) val webViewRouter = OSIABWebViewRouterAdapter( context = context, lifecycleScope = activity.lifecycleScope, options = webViewOptions, + customHeaders = customHeaders, flowHelper = OSIABFlowHelper(), onBrowserPageLoaded = { notifyListeners(OSIABEventType.BROWSER_PAGE_LOADED.value, null) diff --git a/example-app/android/capacitor.settings.gradle b/example-app/android/capacitor.settings.gradle index 8e64f81..0940573 100644 --- a/example-app/android/capacitor.settings.gradle +++ b/example-app/android/capacitor.settings.gradle @@ -1,18 +1,18 @@ // DO NOT EDIT THIS FILE! IT IS GENERATED EACH TIME "capacitor update" IS RUN include ':capacitor-android' -project(':capacitor-android').projectDir = new File('../node_modules/.pnpm/@capacitor+android@7.0.0-rc.0_@capacitor+core@7.0.0-rc.0/node_modules/@capacitor/android/capacitor') +project(':capacitor-android').projectDir = new File('../node_modules/@capacitor/android/capacitor') include ':capacitor-app' -project(':capacitor-app').projectDir = new File('../node_modules/.pnpm/@capacitor+app@7.0.0-rc.0_@capacitor+core@7.0.0-rc.0/node_modules/@capacitor/app/android') +project(':capacitor-app').projectDir = new File('../node_modules/@capacitor/app/android') include ':capacitor-haptics' -project(':capacitor-haptics').projectDir = new File('../node_modules/.pnpm/@capacitor+haptics@7.0.0-rc.0_@capacitor+core@7.0.0-rc.0/node_modules/@capacitor/haptics/android') +project(':capacitor-haptics').projectDir = new File('../node_modules/@capacitor/haptics/android') include ':capacitor-inappbrowser' project(':capacitor-inappbrowser').projectDir = new File('../node_modules/@capacitor/inappbrowser/android') include ':capacitor-keyboard' -project(':capacitor-keyboard').projectDir = new File('../node_modules/.pnpm/@capacitor+keyboard@7.0.0-rc.0_@capacitor+core@7.0.0-rc.0/node_modules/@capacitor/keyboard/android') +project(':capacitor-keyboard').projectDir = new File('../node_modules/@capacitor/keyboard/android') include ':capacitor-status-bar' -project(':capacitor-status-bar').projectDir = new File('../node_modules/.pnpm/@capacitor+status-bar@7.0.0-rc.0_@capacitor+core@7.0.0-rc.0/node_modules/@capacitor/status-bar/android') +project(':capacitor-status-bar').projectDir = new File('../node_modules/@capacitor/status-bar/android') diff --git a/example-app/ios/App/Podfile b/example-app/ios/App/Podfile index a24db8e..226fe95 100644 --- a/example-app/ios/App/Podfile +++ b/example-app/ios/App/Podfile @@ -1,4 +1,4 @@ -require_relative '../../node_modules/.pnpm/@capacitor+ios@7.0.0-rc.0_@capacitor+core@7.0.0-rc.0/node_modules/@capacitor/ios/scripts/pods_helpers' +require_relative '../../node_modules/@capacitor/ios/scripts/pods_helpers' platform :ios, '14.0' use_frameworks! @@ -9,13 +9,13 @@ use_frameworks! install! 'cocoapods', :disable_input_output_paths => true def capacitor_pods - pod 'Capacitor', :path => '../../node_modules/.pnpm/@capacitor+ios@7.0.0-rc.0_@capacitor+core@7.0.0-rc.0/node_modules/@capacitor/ios' - pod 'CapacitorCordova', :path => '../../node_modules/.pnpm/@capacitor+ios@7.0.0-rc.0_@capacitor+core@7.0.0-rc.0/node_modules/@capacitor/ios' - pod 'CapacitorApp', :path => '../../node_modules/.pnpm/@capacitor+app@7.0.0-rc.0_@capacitor+core@7.0.0-rc.0/node_modules/@capacitor/app' - pod 'CapacitorHaptics', :path => '../../node_modules/.pnpm/@capacitor+haptics@7.0.0-rc.0_@capacitor+core@7.0.0-rc.0/node_modules/@capacitor/haptics' - pod 'CapacitorInappbrowser', :path => '../../node_modules/.pnpm/@capacitor+inappbrowser@file+.._@capacitor+core@7.0.0-rc.0/node_modules/@capacitor/inappbrowser' - pod 'CapacitorKeyboard', :path => '../../node_modules/.pnpm/@capacitor+keyboard@7.0.0-rc.0_@capacitor+core@7.0.0-rc.0/node_modules/@capacitor/keyboard' - pod 'CapacitorStatusBar', :path => '../../node_modules/.pnpm/@capacitor+status-bar@7.0.0-rc.0_@capacitor+core@7.0.0-rc.0/node_modules/@capacitor/status-bar' + pod 'Capacitor', :path => '../../node_modules/@capacitor/ios' + pod 'CapacitorCordova', :path => '../../node_modules/@capacitor/ios' + pod 'CapacitorApp', :path => '../../node_modules/@capacitor/app' + pod 'CapacitorHaptics', :path => '../../node_modules/@capacitor/haptics' + pod 'CapacitorInappbrowser', :path => '../../..' + pod 'CapacitorKeyboard', :path => '../../node_modules/@capacitor/keyboard' + pod 'CapacitorStatusBar', :path => '../../node_modules/@capacitor/status-bar' end target 'App' do diff --git a/example-app/package-lock.json b/example-app/package-lock.json index becbbf9..11c1514 100644 --- a/example-app/package-lock.json +++ b/example-app/package-lock.json @@ -49,7 +49,7 @@ }, "..": { "name": "@capacitor/inappbrowser", - "version": "2.1.0", + "version": "2.1.1", "license": "MIT", "devDependencies": { "@capacitor/android": "next", diff --git a/example-app/src/pages/Home.tsx b/example-app/src/pages/Home.tsx index 9bfff64..ee81c7d 100644 --- a/example-app/src/pages/Home.tsx +++ b/example-app/src/pages/Home.tsx @@ -89,6 +89,10 @@ const Home: React.FC = () => { animationEffect: iOSAnimation.CROSS_DISSOLVE, allowsBackForwardNavigationGestures: true } + }, + customHeaders: { + "X-Custom-Header": "CustomValue", + "X-Another-Header": "AnotherValue" } }); } diff --git a/ios/Sources/InAppBrowserPlugin/InAppBrowserPlugin.swift b/ios/Sources/InAppBrowserPlugin/InAppBrowserPlugin.swift index 152f1fc..265173a 100644 --- a/ios/Sources/InAppBrowserPlugin/InAppBrowserPlugin.swift +++ b/ios/Sources/InAppBrowserPlugin/InAppBrowserPlugin.swift @@ -53,10 +53,14 @@ public class InAppBrowserPlugin: CAPPlugin, CAPBridgedPlugin { guard let options: OSInAppBrowserWebViewModel = self.createModel(for: call.getObject("options")) else { return self.error(call, type: .inputArgumentsIssue(target: .webView)) } + let customHeaders = call.getObject("customHeaders")?.compactMapValues { + ($0 as? String) ?? ($0 as? NSNumber)?.stringValue + } DispatchQueue.main.async { engine.openWebView( - url, - options.toWebViewOptions(), + url: url, + options: options.toWebViewOptions(), + customHeaders: customHeaders, onDelegateClose: { [weak self] in self?.bridge?.viewController?.dismiss(animated: true) }, @@ -65,7 +69,7 @@ public class InAppBrowserPlugin: CAPPlugin, CAPBridgedPlugin { }, onDelegateAlertController: { [weak self] alert in self?.bridge?.viewController?.presentedViewController?.show(alert, sender: nil) - }, { [weak self] event, viewControllerToOpen, data in + }, completionHandler: { [weak self] event, viewControllerToOpen, data in self?.handleResult(event, for: call, checking: viewControllerToOpen, data: data, error: .failedToOpen(url: url.absoluteString, onTarget: .webView)) } ) @@ -75,22 +79,22 @@ public class InAppBrowserPlugin: CAPPlugin, CAPBridgedPlugin { @objc func close(_ call: CAPPluginCall) { guard loadEngineIfNeeded(call) != nil else { return } - if let openedViewController { - DispatchQueue.main.async { - openedViewController.dismiss(animated: true) { [weak self] in - self?.success(call) - } + guard let openedViewController else { + error(call, type: .noBrowserToClose) + return + } + DispatchQueue.main.async { + openedViewController.dismiss(animated: true) { [weak self] in + self?.success(call) } - } else { - self.error(call, type: .noBrowserToClose) } } } private extension InAppBrowserPlugin { func loadEngineIfNeeded(_ call: CAPPluginCall) -> OSInAppBrowserEngine? { - if self.engine == nil { self.load() } - guard let engine = self.engine else { + if self.engine == nil { load() } + guard let engine else { self.error(call, type: .bridgeNotInitialised) return nil } @@ -124,11 +128,11 @@ private extension InAppBrowserPlugin { func handleResult(_ event: OSIABEventType?, for call: CAPPluginCall, checking viewController: UIViewController?, data: [String: Any]?, error: OSInAppBrowserError) { if let event { if event == .pageClosed { - self.openedViewController = nil + openedViewController = nil } - self.notifyListeners(event.rawValue, data: data) + notifyListeners(event.rawValue, data: data) } else if let viewController { - self.present(viewController) { [weak self] in + present(viewController) { [weak self] in self?.openedViewController = viewController self?.success(call) } @@ -173,9 +177,10 @@ private extension InAppBrowserPlugin { } private extension OSInAppBrowserEngine { + func openExternalBrowser(_ url: URL, _ completionHandler: @escaping (Bool) -> Void) { let router = OSIABApplicationRouterAdapter() - self.openExternalBrowser(url, routerDelegate: router, completionHandler) + openExternalBrowser(url, routerDelegate: router, completionHandler) } func openSystemBrowser(_ url: URL, _ options: OSIABSystemBrowserOptions, _ completionHandler: @escaping (OSIABEventType?, UIViewController?) -> Void) { @@ -184,16 +189,17 @@ private extension OSInAppBrowserEngine { onBrowserPageLoad: { completionHandler(.pageLoadCompleted, nil) }, onBrowserClosed: { completionHandler(.pageClosed, nil) } ) - self.openSystemBrowser(url, routerDelegate: router) { completionHandler(nil, $0) } + openSystemBrowser(url, routerDelegate: router) { completionHandler(nil, $0) } } func openWebView( - _ url: URL, - _ options: OSIABWebViewOptions, + url: URL, + options: OSIABWebViewOptions, + customHeaders: [String: String]? = nil, onDelegateClose: @escaping () -> Void, onDelegateURL: @escaping (URL) -> Void, onDelegateAlertController: @escaping (UIAlertController) -> Void, - _ completionHandler: @escaping (OSIABEventType?, UIViewController?, [String: Any]?) -> Void + completionHandler: @escaping (OSIABEventType?, UIViewController?, [String: Any]?) -> Void ) { let callbackHandler = OSIABWebViewCallbackHandler( onDelegateURL: onDelegateURL, @@ -208,8 +214,8 @@ private extension OSInAppBrowserEngine { completionHandler(.pageNavigationCompleted, nil, ["url": url ?? ""]) } ) - let router = OSIABWebViewRouterAdapter(options, cacheManager: OSIABBrowserCacheManager(dataStore: .default()), callbackHandler: callbackHandler) - self.openWebView(url, routerDelegate: router) { completionHandler(nil, $0, nil) } + let router = OSIABWebViewRouterAdapter(options: options, customHeaders: customHeaders, cacheManager: OSIABBrowserCacheManager(dataStore: .default()), callbackHandler: callbackHandler) + openWebView(url, routerDelegate: router) { completionHandler(nil, $0, nil) } } } diff --git a/src/definitions.ts b/src/definitions.ts index 39828cd..821356f 100644 --- a/src/definitions.ts +++ b/src/definitions.ts @@ -165,6 +165,8 @@ export interface OpenInSystemBrowserParameterModel extends OpenInDefaultParamete export interface OpenInWebViewParameterModel extends OpenInDefaultParameterModel { /** A structure containing some configurations to apply to the Web View. */ options: WebViewOptions; + /** A map of custom headers to be sent with the request. */ + customHeaders?: { [key: string]: string }; } /**