簡體   English   中英

Flutter(在 iOS 上)- 在運行時請求位置權限,沒有出現對話框! (定位包)

[英]Flutter (on iOS) - Asking for location permission on runtime, no dialog appearing! (Location package)

我嘗試在 iOS 模擬器上使用 Location 包請求位置訪問權限,但對話框沒有顯示?

try/catch 塊甚至不會被調用

當我在 Android 設備上嘗試它時,它就像一個魅力

我的代碼:

try {
  if (await new Location().requestPermission()) {
    print("User granted location permission!");
    hasPermission = true;
    await checkLocationService();
  } else {
    print("User denied permission!");
  }
} catch (error) {
  print(error);
}

包: https : //pub.dev/packages/location

信息.plist:

<key>NSLocationWhenInUseUsageDescription</key>
<string>We need your location to find places near to you.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>We need your location to find places near to you.</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>We need your location to find places near to you.</string>

我已經運行了'flutter clean',沒有結果。 我已經在 iOS 模擬器上重新安裝了該應用程序,但沒有結果。 我什至重新啟動了 iOS 模擬器,沒有結果。

此致。

首先,我會檢查以確保該應用程序在手機設置本身中具有位置權限。

我個人會使用permission_handler 包。 它可以輕松處理 android 和 ios 的所有權限。 我在兩者上都使用了它並取得了巨大的成功。

https://pub.dev/packages/permission_handler#-readme-tab-

然后你可以使用下面的來獲取你可以放在你的主函數中的位置權限。 這也很棒,因為您可以輕松獲得任何其他許可,並且可以使用這兩行來完成。 如果許可已被接受,它將檢查、確認並繼續,如果沒有被接受,它將請求用戶許可。

import 'package:permission_handler/permission_handler.dart';

Map<PermissionGroup, PermissionStatus> permissions = await PermissionHandler().requestPermissions([PermissionGroup.location]);

您還需要將PERMISSION_LOCATION=1添加到您的podfile

post_install do |installer|
  installer.pods_project.targets.each do |target|
    flutter_additional_ios_build_settings(target)

    target.build_configurations.each do |config|
      config.build_settings['GCC_PREPROCESSOR_DEFINITIONS'] ||= [
        '$(inherited)',

        ## Add this line
         'PERMISSION_LOCATION=1'
      ]
    end
  end
end

然后,您可以使用以下方法請求權限:

final status = await Permission.locationWhenInUse.request();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM