Opening Location Settings Page or Prompting user to Enable Location

hello,
i've been hacking my brain and googling away in vain. i am trying to find a way to prompt the user to switch location on either by going directly to the settings page or just tapping yes on the screen.
all code i've seen doesn't seem to work. does any one have something that works. a detailed example will be much appreciated. thank you.
I'm really new to Xamarin development

Answers

  • bob2609bob2609 Member ✭✭
    This is what I want to achieve.. Either one of the two will be nice.
  • JohnHardmanJohnHardman GBUniversity mod

    @bob2609 - I asked a similar question recently for Android, at https://forums.xamarin.com/discussion/105381/opening-privacy-settings-on-android-phone-programmatically#latest . Unfortunately, no answer so far. I have the equivalent working for iOS and UWP. Let me know if you need those.

  • bob2609bob2609 Member ✭✭
    Yes please share that as well.
  • JohnHardmanJohnHardman GBUniversity mod

    @bob2609

    For iOS (but still needs tweaking for iOS 10+):

    if (UIDevice.CurrentDevice.CheckSystemVersion(8, 0))
    {
        // For iOS 8 and 9, we can navigate automatically to the settings
        NSUrl url = new NSUrl(UIKit.UIApplication.OpenSettingsUrlString);
        bool result = UIApplication.SharedApplication.OpenUrl(url);
    }
    else
    {
        // Below iOS 8, the user has to navigate manually to the settings
        UIAlertView uiAlert = new UIAlertView(
            "Location Services Required",
            "Authorisation to use your location is required to use this feature of the app. To use this feature please go to the settings app and enable location services",
            null,
            "Ok");
        uiAlert.Show();
    }
    

    For UWP (but note the nasty hack, which can hopefully be removed at some point):

    private async Task OpenUrlAsync(string url)
    {
        await Windows.System.Launcher.LaunchUriAsync(new Uri(url));
    
        // TODO: Remove the next two lines (this is a nasty hack, as calling LaunchUriAsync once does not work reliably)
        await Task.Delay(500);
        await Windows.System.Launcher.LaunchUriAsync(new Uri(url));
    }
    
    public async Task OpenCalendarSettingsAsync()
    {
        await OpenUrlAsync("ms-settings:privacy-calendar");
    }
    
    public async Task OpenContactSettingsAsync()
    {
        await OpenUrlAsync("ms-settings:privacy-contacts");
    }
    
    public async Task OpenEmailSettingsAsync()
    {
        await OpenUrlAsync("ms-settings:privacy-email");
    }
    
    public async Task OpenLocationSettingsAsync()
    {
        await OpenUrlAsync("ms-settings:privacy-location");
    }
    
    public async Task OpenMessagingSettingsAsync()
    {
        await OpenUrlAsync("ms-settings:privacy-messaging");
    }
    
        // From https://www.nextofwindows.com/the-complete-list-of-ms-settings-shortcuts-to-windows-10-settings-applet
        //General ms-settings:privacy
        //Location     ms-settings:privacy-location
        //Camera   ms-settings:privacy-webcam
        //Microphone   ms-settings:privacy-microphone
        //Motion   ms-settings:privacy-motion
        //Speech, inking & typing ms-settings:privacy-speechtyping
        //Account info ms-settings:privacy-accountinfo
        //Contacts     ms-settings:privacy-contacts
        //Calendar     ms-settings:privacy-calendar
        //Call history ms-settings:privacy-callhistory
        //Email    ms-settings:privacy-email
        //Messaging    ms-settings:privacy-messaging
        //Radios   ms-settings:privacy-radios
        //Background Apps ms-settings:privacy-backgroundapps
        //Other devices ms-settings:privacy-customdevices
        //Feedback & diagnostics ms-settings:privacy-feedback
    
  • bob2609bob2609 Member ✭✭

    I managed to do it on Android day before yesterday.. i will share the solution later after i get off work

  • SheemuSheemu USMember ✭✭

    Thanks @bob2609, it's realy healpful for me.

    @bob2609 said:
    so after going through several tutorials and answers all over the internet i finally was able to find to accomplish what i wanted to achieve,

    i used a dependency service as one of the answers indicated here How to open setting from our application in xamarin

    there are few things that were not mentioned like registering the interface in order to use it in platform specific projects.

    here is the code for anyone who needs it

    the Interface : I called my Interface ** ILocSettings.cs**

    using System;
    using System.Collections.Generic;
    using System.Text;
    
    
    [assembly: Xamarin.Forms.Dependency(typeof(DE2.ILocSettings))]
    namespace DE2
    {
        public interface ILocSettings
        {
            void OpenSettings();
    
        }
    

    the form that has a button I called it

    DataEntryForm.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"
                 x:Class="DE2.DataEntryForm"
                 >
        <ContentPage.Content>
            <StackLayout>
    
                <Button x:Name="TurnLocationOn"
                        Text="Turn On Location"
                        Clicked="TurnLocationOn_OnClicked"/>
    
            </StackLayout>
        </ContentPage.Content>
    </ContentPage>
    

    then the DataEntryForm.cs

                    using System;
                    using System.Collections.Generic;
                    using System.Linq;
                    using System.Text;
                    using System.Threading.Tasks;
                    using Xamarin.Forms;
                    using Xamarin.Forms.Xaml;
                    using Plugin.pbXSettings;
                    using Plugin.Geolocator;
                    using Plugin.Permissions;
                    using Plugin.Permissions.Abstractions;
                    using Plugin.DeviceInfo;
                    using Plugin.DeviceInfo.Abstractions;
                    
                    [assembly: Xamarin.Forms.Dependency(typeof(DE2.ILocSettings))]
                    
                    namespace DE2
                    {
                    
                        using Xamarin.Forms.PlatformConfiguration;
                    
                        [XamlCompilation(XamlCompilationOptions.Compile)]
                        public partial class DataEntryForm : ContentPage
                        {
                            public DataEntryForm ()
                            {
                                InitializeComponent ();
                    
                            }
                    
                    
                            private async void  TurnLocationOn_OnClicked(object sender, global::System.EventArgs e)
                            {
                    
                                var myAction = await DisplayAlert("Location", "Please Turn On Location", "OK","CANCEL");
                                if (myAction)
                                {
                                    if (Device.RuntimePlatform == global::Xamarin.Forms.Device.Android)
                                    {
                    
                                        //DependencyService.Get<ISettingsService>().OpenSettings();
                                        global::Xamarin.Forms.DependencyService.Get<global::DE2.ILocSettings>().OpenSettings();
                    
                    
                    
                                    }
                                    else
                                    {
                                        DisplayAlert("Device", "You are using some other shit", "YEP");
                                    }
                                }
                                else
                                {
                                    DisplayAlert("Alert","User Denied Permission","OK");
                                }
                    
                    
                                // 
                            }
                        }
                    }
    

    Then I have this Class Placed on the Android Specific Platform LocationZ.cs

            using System;
            using System.Collections.Generic;
            using System.Linq;
            using System.Text;
            using Android.App;
            using Android.Locations;
            using Android.Content;
            using Android.OS;
            using Android.Runtime;
            using Android.Views;
            using Android.Widget;
            using Xamarin.Android;
            using Xamarin.Forms;
            using DE2;
            using DE2.Droid;
            
            
            //[assembly: Xamarin.Forms.Dependency(typeof(ILocSettings))]
            
            //Notice the use of LocationZ in registering below instead of ILocSettings
            
            [assembly: Xamarin.Forms.Dependency(typeof(LocationZ))]
            
            namespace DE2.Droid
            {
                using System.Runtime.Remoting.Messaging;
            
                using Android.Support.V4.View;
                using Android.Support.V7.App;
            
                using Xamarin.Forms;
                using DE2;
            
            
                public class LocationZ : ILocSettings
                {
                    public void OpenSettings()
                    {
                        LocationManager LM = (LocationManager)Forms.Context.GetSystemService(Context.LocationService);
            
            
                        if (LM.IsProviderEnabled(LocationManager.GpsProvider)==false)
                        {
            
            
                                        Context ctx = Forms.Context;
                                        ctx.StartActivity(new Intent(Android.Provider.Settings.ActionLocationSourceSettings));
            
            
                        }
                        else
                        {
                            //this is handled in the PCL
                        }
                    }
    



  • menna_magdymenna_magdy Member ✭✭
    edited December 2018

    @bob2609 Can you upload the project please ?!
    I want to understand the full implementation for iOS

Sign In or Register to comment.