Flutter:位置许可:使用中或始终使用 [英] Flutter: Location permission: when in use or always
本文介绍了Flutter:位置许可:使用中或始终使用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想知道用户是否选择了仅在使用时"或始终"作为位置.
I'm trying to know if the user has selected "only when in use" or "always" for the location.
https://pub.dev/packages/location_permissions
我从包装中尝试了以下代码:
I tried this code from the package:
ServiceStatus serviceStatus =
await LocationPermissions().checkServiceStatus();
print(serviceStatus);
但它会打印:
ServiceStatus.enabled
是否有可能(也许与另一个软件包一起)知道这一点?
Is there a possibility (perhaps with another package) to know this?
提前谢谢!
推荐答案
permission_handler 可以做到这一点.
enum PermissionGroup {
...
/// Android: Fine and Coarse Location
/// iOS: CoreLocation - Always
locationAlways,
/// Android: Fine and Coarse Location
/// iOS: CoreLocation - WhenInUse
locationWhenInUse
}
请求权限:
final ph = PermissionHandler();
final requested = await ph.requestPermissions([
PermissionGroup.locationAlways,
PermissionGroup.locationWhenInUse
]);
final alwaysGranted = requested[PermissionGroup.locationAlways] == PermissionStatus.granted;
final whenInUseGranted = requested[PermissionGroup.locationWhenInUse] == PermissionStatus.granted;
将这些权限添加到<manifest>
标签内的AndroidManifest.xml:
Add these permissions to AndroidManifest.xml inside <manifest>
tag:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test_project">
<!-- add these -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
...
然后将这些添加到Info.plist的<dict>
标签:
And these to <dict>
tag of Info.plist:
<!-- Permission options for the `location` group -->
<key>NSLocationWhenInUseUsageDescription</key>
<string>Need location when in use</string>
<key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
<string>Always and when in use!</string>
<key>NSLocationUsageDescription</key>
<string>Older devices need location.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>Can I haz location always?</string>
这篇关于Flutter:位置许可:使用中或始终使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文