Quantcast
Channel: Recent Threads — Xamarin Community Forums
Viewing all articles
Browse latest Browse all 204402

StartMonitoringSignificantLocationChanges not firing LocationsUpdated in background

$
0
0

I'm a bit new to the Location Service code, but I thought I had this figure out. Please help...been wrestling with this for a few days.

I built an app that needs to subscribe to the CLLocationManager.StartMonitoringSignificantLocationChanges LocationsUpdated event when the app is in the background/not started. I first wrote and tested the code in a ViewController...it works perfectly. The simulator fires events as I "Freeway Drive" and the app on my phone work great.I then moved the code into the AppDelegate. I step through the code OK, but the event never fires. Can some tell me what's wrong with the code or does anyone have a real example of this working?

AppDelegate: using System; using System.Threading.Tasks; using MonoTouch.Foundation; using MonoTouch.UIKit; using MonoTouch.AVFoundation; using MonoTouch.CoreMotion; using MonoTouch.CoreLocation;

namespace ExceedIPA.iOS { [Register ("AppDelegate")] public partial class AppDelegate : UIApplicationDelegate { public override UIWindow Window { get; set; }

    // Private vars
    CLLocationManager locMan;
    CLGeocoder geoCoder;
    int locationCt = 0;

    public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
    {
        // For iOS8 we must get permission to show local notifications.
        if (UIDevice.CurrentDevice.CheckSystemVersion (8, 0))
        {
            var settings = UIUserNotificationSettings.GetSettingsForTypes (UIUserNotificationType.Alert, new NSSet ());
            if (UIApplication.SharedApplication.CurrentUserNotificationSettings != settings)
            {
                UIApplication.SharedApplication.RegisterUserNotificationSettings (settings);
            }
        }

        // Create an instance of location manager
        this.locMan = new CLLocationManager {
            ActivityType = CLActivityType.OtherNavigation,
            //DesiredAccuracy = 1,
            DistanceFilter = 200
        };

        // Apple restricted location services usage in iOS8. We must explicitly prompt the user for permission
        // See also: http://motzcod.es/post/97662738237/scanning-for-ibeacons-in-ios-8
        if (UIDevice.CurrentDevice.CheckSystemVersion (8, 0))
        {
            if (CLLocationManager.LocationServicesEnabled) {
                this.InitLocationServices ();
            }

            this.locMan.AuthorizationChanged += (object sender, CLAuthorizationChangedEventArgs e) => {
                if (e.Status == CLAuthorizationStatus.AuthorizedAlways || e.Status == CLAuthorizationStatus.AuthorizedWhenInUse) {
                    this.InitLocationServices ();
                }
            };
            this.locMan.RequestAlwaysAuthorization ();
        }
        else
        {
            this.InitLocationServices ();
        }

        Console.WriteLine ("FinishedLaunching fired");
        return true;
    }

    void InitLocationServices ()
    {
        // For reverse searches
        this.geoCoder = new CLGeocoder ();

        // Start getting significant updates from the location manager.
        //this.locMan.StartMonitoringSignificantLocationChanges ();

        // Handle location updates
        this.locMan.LocationsUpdated += HandleLocationsUpdated;
    }

    async void HandleLocationsUpdated (object sender, CLLocationsUpdatedEventArgs e)
    {
        // Get location
        var location = e.Locations [e.Locations.Length - 1];

        // Convert to string to display
        string locationInfo = await Helpers.GetInfoForLocation (this.geoCoder, location);
        locationInfo = "AD: " + locationCt.ToString () + ". " + locationInfo + " -> " +
            string.Format ("Coordinates: {0:F3} / {1:F3}", location.Coordinate.Latitude, location.Coordinate.Longitude);

        // Increment counter
        locationCt++;

        // Write to console
        Console.WriteLine (locationInfo);

        // Show push message
        UILocalNotification notification = new UILocalNotification();
        notification.FireDate = DateTime.Now.AddMinutes(1);
        notification.AlertAction = "Significant Location Change";
        notification.AlertBody = locationInfo;
        UIApplication.SharedApplication.ScheduleLocalNotification(notification);
    }

    public override void WillEnterForeground (UIApplication application)
    {
        this.locMan.StopMonitoringSignificantLocationChanges ();
        Console.WriteLine ("WillEnterForeground fired");
    }

    public override void DidEnterBackground (UIApplication application)
    {
        this.locMan.StartMonitoringSignificantLocationChanges ();
        Console.WriteLine ("DidEnterBackground fired");
    }
}

}


Viewing all articles
Browse latest Browse all 204402

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>