pichillilorenzo / flutter_inappwebview

A Flutter plugin that allows you to add an inline webview, to use a headless webview, and to open an in-app browser window.
https://inappwebview.dev
Apache License 2.0
3.27k stars 1.6k forks source link

how InAppWebView deal with geolocation #491

Closed yang5664 closed 4 years ago

yang5664 commented 4 years ago

how to allow Location permission on android?

ios is working correctly but android.

flutter_inappwebview: ^4.0.0+4 flutter: 1.21.0-10.0.pre.113

yang5664 commented 4 years ago
  androidOnGeolocationPermissionsShowPrompt:
      (InAppWebViewController controller, String origin) async {
    return GeolocationPermissionShowPromptResponse(
      origin: origin,
      allow: true,
      retain: true
    );
  },
farazulkifli commented 4 years ago

Hi @yang5664 yang5664. I tried androidOnGeolocationPermissionsShowPrompt in my code. But it only worked on debug mode. After i create release of the app, cannot access location anymore. Any suggestion to fix that?

Yanghsuanming commented 4 years ago

Hi @yang5664 yang5664. I tried androidOnGeolocationPermissionsShowPrompt in my code. But it only worked on debug mode. After i create release of the app, cannot access location anymore. Any suggestion to fix that?

image

farazulkifli commented 4 years ago

Thank you @Xuan-Goldenf for your quick reply! You just saved me. Thanks again

suatkeskin commented 3 years ago

Hi @yang5664. I tried androidOnGeolocationPermissionsShowPrompt in my code. But it is not worked for me. I put a debug point inside the method but it seems the method not firing. I updated the AndroidManifest.xml correctly. Any suggestion to fix that?

Trend74X commented 3 years ago
  androidOnGeolocationPermissionsShowPrompt:
      (InAppWebViewController controller, String origin) async {
    return GeolocationPermissionShowPromptResponse(
      origin: origin,
      allow: true,
      retain: true
    );
  },

I did the same and also added in AndroidManifest.xml but it is not working, doesn't even fire the event

Trend74X commented 3 years ago
  androidOnGeolocationPermissionsShowPrompt:
      (InAppWebViewController controller, String origin) async {
    return GeolocationPermissionShowPromptResponse(
      origin: origin,
      allow: true,
      retain: true
    );
  },

I did the same and also added in AndroidManifest.xml but it is not working, doesn't even fire the event

I got it working I added geolocator: ^6.2.0 in pubspec.yaml and run that package in initState as LocationPermission permission = await Geolocator.checkPermission(); await Geolocator.requestPermission(); await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);

then passed in the inappwebview using androidOnGeolocationPermissionsShowPrompt: which worked as a charm

tadappnative commented 3 years ago

then passed in the inappwebview using androidOnGeolocationPermissionsShowPrompt:

How did you do this? do you have an example?

djks74 commented 3 years ago

Hi, can someone share full example code for this geolocation added and cookies implemented please?

Why I cannot find anywhere, even for the cookies. so sad!!!!

djks74 commented 3 years ago

I got it working for geo now, but cookies and openlink using ShoulOverloadingURL seems have little glitch! :-(

Little said.... its broken for url scheme :-(

import 'dart:async'; import 'dart:collection'; import 'dart:convert'; import 'dart:io'; import 'dart:typed_data'; import 'package:flutter/material.dart'; import 'package:flutter_inappwebview/flutter_inappwebview.dart'; import 'package:permission_handler/permission_handler.dart'; import 'package:url_launcher/url_launcher.dart';

Future main() async { WidgetsFlutterBinding.ensureInitialized();

if (Platform.isAndroid) { await AndroidInAppWebViewController.setWebContentsDebuggingEnabled(true); }

runApp(new MyApp()); }

class MyApp extends StatefulWidget { @override _MyAppState createState() => new _MyAppState(); }

class _MyAppState extends State { final GlobalKey webViewKey = GlobalKey();

InAppWebViewController webViewController; InAppWebViewGroupOptions options = InAppWebViewGroupOptions( crossPlatform: InAppWebViewOptions( useShouldOverrideUrlLoading: true, mediaPlaybackRequiresUserGesture: false, ), android: AndroidInAppWebViewOptions( useHybridComposition: true, ), ios: IOSInAppWebViewOptions( allowsInlineMediaPlayback: true, ));

PullToRefreshController pullToRefreshController; String url = ""; double progress = 0; final urlController = TextEditingController();

void requestPermission() async {}

@override void initState() { super.initState();

requestPermission();

pullToRefreshController = PullToRefreshController(
  options: PullToRefreshOptions(
    color: Colors.blue,
  ),
  onRefresh: () async {
    if (Platform.isAndroid) {
      webViewController?.reload();
    } else if (Platform.isIOS) {
      webViewController?.loadUrl(
          urlRequest: URLRequest(url: await webViewController?.getUrl()));
    }
  },
);

}

@override void dispose() { super.dispose(); }

@override Widget build(BuildContext context) { return MaterialApp( home: Scaffold( appBar: AppBar( backgroundColor: Colors.white, leading: IconButton( color: Colors.black, icon: Icon(Icons.arrow_back, color: Colors.black), onPressed: () => Navigator.of(context).pushNamedAndRemoveUntil( '/', (Route route) => false)), title: Text("CHECKOUT", style: TextStyle(color: Colors.black)), ), body: SafeArea( child: Column(children: [ Expanded( child: Stack( children: [ InAppWebView( key: webViewKey, initialUrlRequest: URLRequest( url: Uri.parse("https://pasarsegar.co.id/checkout/")), initialOptions: InAppWebViewGroupOptions( crossPlatform: InAppWebViewOptions(), android: AndroidInAppWebViewOptions( //useHybridComposition: true )), androidOnGeolocationPermissionsShowPrompt: (InAppWebViewController controller, String origin) async { bool result = await showDialog( context: context, barrierDismissible: false, // user must tap button! builder: (BuildContext context) { return AlertDialog( title: Text('Allow access location $origin'), content: SingleChildScrollView( child: ListBody( children: [ Text('Allow access location $origin'), ], ), ), actions: [ TextButton( child: Text('Allow'), onPressed: () { Navigator.of(context).pop(true); }, ), TextButton( child: Text('Denied'), onPressed: () { Navigator.of(context).pop(false); }, ), ], ); }, ); if (result) { return Future.value( GeolocationPermissionShowPromptResponse( origin: origin, allow: true, retain: true)); } else { return Future.value( GeolocationPermissionShowPromptResponse( origin: origin, allow: false, retain: false)); } }, onWebViewCreated: (controller) { webViewController = controller; }, onLoadStart: (controller, url) { setState(() { this.url = url.toString(); urlController.text = this.url; }); }, androidOnPermissionRequest: (controller, origin, resources) async { return PermissionRequestResponse( resources: resources, action: PermissionRequestResponseAction.GRANT); }, shouldOverrideUrlLoading: (controller, navigationAction) async { var uri = navigationAction.request.url;

                  if (![
                    "http",
                    "https",
                    "file",
                    "data",
                    "gojek://",
                    "shopeeid://",
                    "javascript",
                    "about"
                  ].contains(uri.scheme)) {
                    if (await canLaunch(url)) {
                      // Launch the App
                      await launch(
                        url,
                      );
                      // and cancel the request
                      return NavigationActionPolicy.CANCEL;
                    }
                  }

                  return NavigationActionPolicy.ALLOW;
                },
                onLoadStop: (controller, url) async {
                  pullToRefreshController.endRefreshing();
                  setState(() {
                    this.url = url.toString();
                    urlController.text = this.url;
                  });
                },
                onLoadError: (controller, url, code, message) {
                  pullToRefreshController.endRefreshing();
                },
                onProgressChanged: (controller, progress) {
                  if (progress == 100) {
                    pullToRefreshController.endRefreshing();
                  }
                  setState(() {
                    this.progress = progress / 100;
                    urlController.text = this.url;
                  });
                },
                onUpdateVisitedHistory: (controller, url, androidIsReload) {
                  setState(() {
                    this.url = url.toString();
                    urlController.text = this.url;
                  });
                },
                onConsoleMessage: (controller, consoleMessage) {
                  print(consoleMessage);
                },
              ),
              progress < 1.0
                  ? LinearProgressIndicator(value: progress)
                  : Container(),
            ],
          ),
        ),
      ]))),
);

} }

djks74 commented 3 years ago

Seems this plugin in broken for deeplink! hope someone can fixing the url launch scheme if I got coding wrong, please help to fix it. apreciatte!

fixail commented 2 years ago
  androidOnGeolocationPermissionsShowPrompt:
      (InAppWebViewController controller, String origin) async {
    return GeolocationPermissionShowPromptResponse(
      origin: origin,
      allow: true,
      retain: true
    );
  },

I did the same and also added in AndroidManifest.xml but it is not working, doesn't even fire the event

I got it working I added geolocator: ^6.2.0 in pubspec.yaml and run that package in initState as LocationPermission permission = await Geolocator.checkPermission(); await Geolocator.requestPermission(); await Geolocator.getCurrentPosition(desiredAccuracy: LocationAccuracy.high);

then passed in the inappwebview using androidOnGeolocationPermissionsShowPrompt: which worked as a charm

can you please show the code, how you solve it?

DandiGi-returns commented 2 years ago

Hi @yang5664 yang5664. I tried androidOnGeolocationPermissionsShowPrompt in my code. But it only worked on debug mode. After i create release of the app, cannot access location anymore. Any suggestion to fix that?

image

Information details regarding the feature can be read here... https://developer.android.com/guide/topics/manifest/uses-feature-element#permissions

image

Vidyasbl commented 2 years ago

@yang5664 could you please tell how you got it working for ios? What is the equivalent of androidOnGeolocationPermissionsShowPrompt in ios?

Yanghsuanming commented 2 years ago

@yang5664 could you please tell how you got it working for ios? What is the equivalent of androidOnGeolocationPermissionsShowPrompt in ios?

here is my plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleDevelopmentRegion</key>
    <string>$(DEVELOPMENT_LANGUAGE)</string>
    <key>CFBundleExecutable</key>
    <string>$(EXECUTABLE_NAME)</string>
    <key>CFBundleIdentifier</key>
    <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>原住民智慧部落</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleShortVersionString</key>
    <string>$(MARKETING_VERSION)</string>
    <key>CFBundleSignature</key>
    <string>????</string>
    <key>CFBundleVersion</key>
    <string>$(CURRENT_PROJECT_VERSION)</string>
    <key>EnableBackgroundLocationUpdates</key>
    <false/>
    <key>FirebaseAppDelegateProxyEnabled</key>
    <string>YES</string>
    <key>LSApplicationCategoryType</key>
    <string></string>
    <key>LSRequiresIPhoneOS</key>
    <true/>
    <key>MinimumOSVersion</key>
    <string>9</string>
    <key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <true/>
    </dict>
    <key>NSAppleMusicUsageDescription</key>
    <string>App 需要您的同意, 才能訪問媒體庫</string>
    <key>NSCalendarsUsageDescription</key>
    <string>App 需要您的同意, 才能訪問行事曆</string>
    <key>NSContactsUsageDescription</key>
    <string>App 需要您的同意, 才能訪問通訊錄</string>
    <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
    <string>地圖遊戲需要取用當前位置</string>
    <key>NSLocationAlwaysUsageDescription</key>
    <string>地圖遊戲需要取用當前位置</string>
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>地圖遊戲需要取用當前位置</string>
    <key>NSMotionUsageDescription</key>
    <string>App 需要您的同意, 才能訪問運動與健身</string>
    <key>NSSpeechRecognitionUsageDescription</key>
    <string>App 需要您的同意, 才能使用語音辨識</string>
    <key>UIBackgroundModes</key>
    <array>
        <string>fetch</string>
        <string>remote-notification</string>
    </array>
    <key>UILaunchStoryboardName</key>
    <string>LaunchScreen</string>
    <key>UIMainStoryboardFile</key>
    <string>Main</string>
    <key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
    <key>UISupportedInterfaceOrientations~ipad</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationPortraitUpsideDown</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
    <key>UIViewControllerBasedStatusBarAppearance</key>
    <false/>
    <key>io.flutter.embedded_views_preview</key>
    <true/>
</dict>
</plist>
Yanghsuanming commented 2 years ago

Hi @yang5664 yang5664. I tried androidOnGeolocationPermissionsShowPrompt in my code. But it only worked on debug mode. After i create release of the app, cannot access location anymore. Any suggestion to fix that?

image

Information details regarding the feature can be read here... https://developer.android.com/guide/topics/manifest/uses-feature-element#permissions

image

did you try to ask permission from user?

image
gaalha commented 1 year ago

What about iOS?

github-actions[bot] commented 3 weeks ago

This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug and a minimal reproduction of the issue.