Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CapacitorInappbrowser.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -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
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`** | <code><a href="#webviewoptions">WebViewOptions</a></code> | A structure containing some configurations to apply to the Web View. |
| Prop | Type | Description |
| ------------------- | --------------------------------------------------------- | -------------------------------------------------------------------- |
| **`options`** | <code><a href="#webviewoptions">WebViewOptions</a></code> | A structure containing some configurations to apply to the Web View. |
| **`customHeaders`** | <code>{ [key: string]: string; }</code> | A map of custom headers to be sent with the request. |


#### WebViewOptions
Expand Down
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String>? = call.getObject("customHeaders")?.let { jsObject ->
val result = mutableMapOf<String, String>()
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)
Expand Down
10 changes: 5 additions & 5 deletions example-app/android/capacitor.settings.gradle
Original file line number Diff line number Diff line change
@@ -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/@[email protected]_@[email protected]/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/@[email protected]_@[email protected]/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/@[email protected]_@[email protected]/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/@[email protected]_@[email protected]/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/@[email protected]_@[email protected]/node_modules/@capacitor/status-bar/android')
project(':capacitor-status-bar').projectDir = new File('../node_modules/@capacitor/status-bar/android')
16 changes: 8 additions & 8 deletions example-app/ios/App/Podfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
require_relative '../../node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@capacitor/ios/scripts/pods_helpers'
require_relative '../../node_modules/@capacitor/ios/scripts/pods_helpers'

platform :ios, '14.0'
use_frameworks!
Expand All @@ -9,13 +9,13 @@ use_frameworks!
install! 'cocoapods', :disable_input_output_paths => true

def capacitor_pods
pod 'Capacitor', :path => '../../node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@capacitor/ios'
pod 'CapacitorCordova', :path => '../../node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@capacitor/ios'
pod 'CapacitorApp', :path => '../../node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@capacitor/app'
pod 'CapacitorHaptics', :path => '../../node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@capacitor/haptics'
pod 'CapacitorInappbrowser', :path => '../../node_modules/.pnpm/@capacitor+inappbrowser@file+.._@[email protected]/node_modules/@capacitor/inappbrowser'
pod 'CapacitorKeyboard', :path => '../../node_modules/.pnpm/@[email protected]_@[email protected]/node_modules/@capacitor/keyboard'
pod 'CapacitorStatusBar', :path => '../../node_modules/.pnpm/@[email protected]_@[email protected]/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
Expand Down
2 changes: 1 addition & 1 deletion example-app/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions example-app/src/pages/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ const Home: React.FC = () => {
animationEffect: iOSAnimation.CROSS_DISSOLVE,
allowsBackForwardNavigationGestures: true
}
},
customHeaders: {
"X-Custom-Header": "CustomValue",
"X-Another-Header": "AnotherValue"
}
});
}
Expand Down
50 changes: 28 additions & 22 deletions ios/Sources/InAppBrowserPlugin/InAppBrowserPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
},
Expand All @@ -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))
}
)
Expand All @@ -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
}
Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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) {
Expand All @@ -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,
Expand All @@ -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) }
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
}

/**
Expand Down