【问题标题】:GeolocationException on Xamarin FormsXamarin 表单中的地理位置异常
【发布时间】:2018-05-25 18:44:29
【问题描述】:

我正在尝试将地图添加到我的 Xamarin 表单应用程序。当我打开应用程序时,它会出现以下错误:

Plugin.Geolocator.Abstractions.GeolocationException 已被抛出

发生地理定位错误:未经授权

第一次,当我运行应用程序时会出现一个弹出窗口,询问我是否可以访问位置,我接受了。运行应用程序时不再出现弹出窗口,它只是错误。 ItemsPage 是我被引导的第一个屏幕,应用程序崩溃的地方。我已经包含了我的 .plist 文件。我正在关注 Xamarin 的 this 指南。

Main.cs (iOS)

public class Application
{
    // This is the main entry point of the application.
    static void Main(string[] args)
    {
        // if you want to use a different Application Delegate class from "AppDelegate"
        // you can specify it here.
        UIApplication.Main(args, null, "AppDelegate"); // error thrown here
    }
}

ItemsPage.xaml

<?xml version="1.0" encoding="utf-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
    xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
    xmlns:maps="clr-namespace:Xamarin.Forms.Maps;assembly=Xamarin.Forms.Maps"
    x:Class="Divco.ItemsPage" 
    Title="{Binding Title}" 
    x:Name="BrowseItemsPage">
<ContentPage.ToolbarItems>
    <ToolbarItem Text="Logout" Command="{Binding LogoutCommand}"/>
</ContentPage.ToolbarItems>
<ContentPage.Content>
    <StackLayout>
        <!--<maps:Map WidthRequest="320" HeightRequest="300"
            x:Name="MyMap"
            IsShowingUser="false"
            MapType="Street"/>-->
        <ListView x:Name="ItemsListView" ItemsSource="{Binding Items}" VerticalOptions="FillAndExpand" HasUnevenRows="true" RefreshCommand="{Binding LoadItemsCommand}" IsPullToRefreshEnabled="true" IsRefreshing="{Binding IsBusy, Mode=OneWay}" CachingStrategy="RecycleElement" ItemSelected="OnItemSelected">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <StackLayout Padding="10">
                            <Label Text="{Binding id}" LineBreakMode="NoWrap" Style="{DynamicResource ListItemTextStyle}" FontSize="16" />
                            <Label Text="{Binding description}" LineBreakMode="NoWrap" Style="{DynamicResource ListItemDetailTextStyle}" FontSize="13" />
                        </StackLayout>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</ContentPage.Content>
</ContentPage>

AppDelegate.cs

[Register("AppDelegate")]
public partial class AppDelegate : global::Xamarin.Forms.Platform.iOS.FormsApplicationDelegate
{
    public override bool FinishedLaunching(UIApplication app, NSDictionary options)
    {
        global::Xamarin.Forms.Forms.Init();
        Xamarin.FormsMaps.Init();

        LoadApplication(new App());



        return base.FinishedLaunching(app, options);
    }
}

Info.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>CFBundleDisplayName</key>
<string>{Hidden}</string>
<key>CFBundleName</key>
<string>{Hidden}</string>
<key>CFBundleIdentifier</key>
<string>{Hidden}</string>
<key>CFBundleShortVersionString</key>
<string>1.0</string>
<key>CFBundleVersion</key>
<string>1.0</string>
<key>LSRequiresIPhoneOS</key>
<true/>
<key>MinimumOSVersion</key>
<string>8.0</string>
<key>UIDeviceFamily</key>
<array>
    <integer>1</integer>
    <integer>2</integer>
</array>
<key>UILaunchStoryboardName</key>
<string>LaunchScreen</string>
<key>UIRequiredDeviceCapabilities</key>
<array>
    <string>armv7</string>
</array>
<key>UISupportedInterfaceOrientations</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
</array>
<key>UISupportedInterfaceOrientations~ipad</key>
<array>
    <string>UIInterfaceOrientationPortrait</string>
    <string>UIInterfaceOrientationPortraitUpsideDown</string>
    <string>UIInterfaceOrientationLandscapeLeft</string>
    <string>UIInterfaceOrientationLandscapeRight</string>
</array>
<key>XSAppIconAssets</key>
<string>Assets.xcassets/AppIcon.appiconset</string>
<key>CFBundleDocumentTypes</key>
<array>
    <dict>
        <key>CFBundleTypeName</key>
        <string>MKDirectionsRequest</string>
        <key>LSItemContentTypes</key>
        <array>
            <string>com.apple.maps.directionsrequest</string>
        </array>
    </dict>
</array>
<key>MKDirectionsApplicationSupportedModes</key>
<array>
    <string>MKDirectionsModeCar</string>
</array>
<key>UIBackgroundModes</key>
<array>
    <string>location</string>
    <string>fetch</string>
    <string>remote-notification</string>
</array>
<key>NSLocationAlwaysUsageDescription</key>
<string>This app needs access always to location.</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app needs access location when open.</string>
</dict>
</plist>

最后,我想注释掉 xaml 中的地图部分以显示用户的当前位置。

【问题讨论】:

  • 你添加了 Xamarin.FormsMaps.Init();在 AppDelegate 中?
  • @SteveChadbourne 是的。我会把它添加到问题中。

标签: xamarin.ios xamarin.forms


【解决方案1】:

info.plistiOS 11 之后也需要NSLocationAlwaysAndWhenInUseUsageDescription 以请求始终授权。

详情参考Requesting Always Authorization

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-15
    • 2019-10-02
    • 1970-01-01
    相关资源
    最近更新 更多