Skip to content

Commit 5dae585

Browse files
committed
feat: add geolocation permission handling for Android and iOS
1 parent 3a10942 commit 5dae585

File tree

3 files changed

+44
-4
lines changed

3 files changed

+44
-4
lines changed

README.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,28 @@ Add the following to your `Info.plist` file:
9191
<string>We need access to the microphone to record audio.</string>
9292
```
9393

94+
### Location usage
95+
96+
#### Android
97+
98+
Add the following to your `AndroidManifest.xml` file:
99+
100+
```xml
101+
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
102+
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
103+
```
104+
105+
Then the permission will be asked when location is requested by a website in the webview.
106+
107+
#### iOS
108+
109+
Add the following to your `Info.plist` file:
110+
111+
```xml
112+
<key>NSLocationWhenInUseUsageDescription</key>
113+
<string>We need access to your location to provide location-based services.</string>
114+
```
115+
94116
### Two way communication
95117

96118
With this plugin you can send events from the main app to the inappbrowser and vice versa.

android/src/main/java/ee/forgr/capacitor_inappbrowser/WebViewDialog.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,15 @@ public void onPermissionRequestCanceled(PermissionRequest request) {
732732
}
733733
}
734734

735+
// Handle geolocation permission requests
736+
@Override
737+
public void onGeolocationPermissionsShowPrompt(String origin, android.webkit.GeolocationPermissions.Callback callback) {
738+
Log.i("INAPPBROWSER", "onGeolocationPermissionsShowPrompt for origin: " + origin);
739+
// Grant geolocation permission automatically for openWebView
740+
// This allows websites to access location when opened with openWebView
741+
callback.invoke(origin, true, false);
742+
}
743+
735744
// This method will be called at page load, a good place to inject customizations
736745
@Override
737746
public void onProgressChanged(WebView view, int newProgress) {

ios/Sources/InAppBrowserPlugin/WKWebViewController.swift

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1578,13 +1578,13 @@ extension WKWebViewController: WKUIDelegate {
15781578
}
15791579
}
15801580
}
1581-
1581+
15821582
public func webView(_ webView: WKWebView, createWebViewWith configuration: WKWebViewConfiguration, for navigationAction: WKNavigationAction, windowFeatures: WKWindowFeatures) -> WKWebView? {
15831583
// Handle target="_blank" links and popup windows
15841584
// When preventDeeplink is true, we should load these in the same webview instead of opening externally
15851585
if let url = navigationAction.request.url {
15861586
print("[InAppBrowser] Handling popup/new window request for URL: \(url.absoluteString)")
1587-
1587+
15881588
// If preventDeeplink is true, load the URL in the current webview
15891589
if preventDeeplink {
15901590
print("[InAppBrowser] preventDeeplink is true, loading popup URL in current webview")
@@ -1593,14 +1593,23 @@ extension WKWebViewController: WKUIDelegate {
15931593
}
15941594
return nil
15951595
}
1596-
1596+
15971597
// Otherwise, check if we should handle it externally
15981598
// But since preventDeeplink is false here, we won't block it
15991599
return nil
16001600
}
1601-
1601+
16021602
return nil
16031603
}
1604+
1605+
@available(iOS 15.0, *)
1606+
public func webView(_ webView: WKWebView, requestGeolocationPermissionFor origin: WKSecurityOrigin, initiatedByFrame frame: WKFrameInfo, decisionHandler: @escaping (WKPermissionDecision) -> Void) {
1607+
print("[InAppBrowser] Geolocation permission requested for origin: \(origin.host)")
1608+
1609+
// Grant geolocation permission automatically for openWebView
1610+
// This allows websites to access location when opened with openWebView
1611+
decisionHandler(.grant)
1612+
}
16041613
}
16051614

16061615
// MARK: - Host Blocking Utilities

0 commit comments

Comments
 (0)