陈嘉乐
2020-06-16 7167334c0e89dd84827d59e726123d14776e3a09
Shared.IOS/BaseApplicationDelegate.cs
@@ -21,11 +21,37 @@
        public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions)
        {
            registerForRemoteNotification(application);
            var musicInfo = new MusicInfo { };
            Volume.Init();
            //var musicInfo = new MusicInfo { };
            //Volume.Init();
            FinishedLaunchingAction?.Invoke(application, launchOptions);
            //GPS
            if (Shared.Application.IsGpsEnable)
            {
                if (MyCLLocationManager.Instance.IsLocationServicesEnabled)
                {
                    switch (Shared.Application.CurrentGpsUseMode)
                    {
                        case Application.GpsUseMode.Always:
                            MyCLLocationManager.Instance.RequestAlwaysAuthorization();
                            break;
                        case Application.GpsUseMode.WhenInUse:
                            MyCLLocationManager.Instance.RequestWhenInUseAuthorization();
                            break;
                    }
                    if (launchOptions != null && launchOptions.ObjectForKey(UIApplication.LaunchOptionsLocationKey) != null)
                    {
                       MyCLLocationManager.Instance.StartMonitoringSignificantLocationChanges();
                    }
                    else
                    {
                        MyCLLocationManager.Instance.StartUpdatingLocation();
                    }
                }
                else {
                    ///TODO 可以提示用户打开定位权限
                }
            }
            return true;
        }
@@ -49,9 +75,10 @@
                application.RegisterForRemoteNotificationTypes(UIRemoteNotificationType.NewsstandContentAvailability | UIRemoteNotificationType.Alert | UIRemoteNotificationType.Badge | UIRemoteNotificationType.Sound);
            }
        }
        class MyCLLocationManager : CLLocationManager
        {
            #region 定位
            public static MyCLLocationManager Instance
            {
                get
@@ -61,12 +88,11 @@
                        locationManager = new MyCLLocationManager();
                        //设置是否允许系统自动暂停定位
                        locationManager.PausesLocationUpdatesAutomatically = false;
                        //locationManager.AllowsBackgroundLocationUpdates = true;
                        locationManager.AllowsBackgroundLocationUpdates = false;
                        //设置定位精度
                        locationManager.DesiredAccuracy = CLLocation.AccuracyKilometer;
                        locationManager.init();
                    }
                    return locationManager;
                }
            }
@@ -92,22 +118,23 @@
                    locationManager.LocationsUpdated += (sender, e) =>
                    {
                        var location = e.Locations[e.Locations.Length - 1];
                        //adjustDistanceFilter(location);
                        System.Console.WriteLine($"位置信息变化 经度{location.Coordinate.Longitude}  纬度{location.Coordinate.Latitude}");
                        uploadToServer(location);
                        adjustDistanceFilter(location);
                        Shared.Application.LocationAction?.Invoke(location.Coordinate.Longitude, location.Coordinate.Latitude);
                        //System.Console.WriteLine($"位置信息变化 经度{location.Coordinate.Longitude}  纬度{location.Coordinate.Latitude}");
                        //uploadToServer(location);
                    };
                }
            }
            /**
    *  规则: 如果速度小于minSpeed m/s 则把触发范围设定为100m
    *  否则将触发范围设定为minSpeed*minInteval
    *  此时若速度变化超过10% 则更新当前的触发范围(这里限制是因为不能不停的设置distanceFilter,
    *  否则uploadLocation会不停被触发)
*/
            //*  规则: 如果速度小于minSpeed m/s 则把触发范围设定为100m
            //*  否则将触发范围设定为minSpeed*minInteval
            //*  此时若速度变化超过10% 则更新当前的触发范围(这里限制是因为不能不停的设置distanceFilter,
            //*  否则uploadLocation会不停被触发)
            void adjustDistanceFilter(CLLocation location)
            {
                //    NSLog(@"adjust:%f",location.speed);
                //NSLog(@"adjust:%f",location.speed);
                if (location.Speed < minSpeed)
                {
                    if (Math.Abs(DistanceFilter - minInteval) > 0.1f)
@@ -145,32 +172,30 @@
                }
            }
            void uploadToServer(CLLocation location) {
#warning 如果有较长时间的操作 比如HTTP上传 请使用beginBackgroundTaskWithExpirationHandler
                if (UIApplication.SharedApplication.ApplicationState == UIApplicationState.Active)
                {
                    //HTTP upload
                    Shared.Application.LocationAction?.Invoke(location.Coordinate.Longitude, location.Coordinate.Latitude);
                    endBackgroundUpdateTask();
                }
                else//后台定位
                {
                    //假如上一次的上传操作尚未结束 则直接return
                    if (taskIdentifier != UIApplication.BackgroundTaskInvalid)
                    {
                        return;
                    }
                    beingBackgroundUpdateTask();
                    //HTTP upload
                    Shared.Application.LocationAction?.Invoke(location.Coordinate.Longitude, location.Coordinate.Latitude);
                    //上传完成记得调用
                    endBackgroundUpdateTask();
                }
            }
//            void uploadToServer(CLLocation location)
//            {
//#warning 如果有较长时间的操作 比如HTTP上传 请使用beginBackgroundTaskWithExpirationHandler
//                if (UIApplication.SharedApplication.ApplicationState == UIApplicationState.Active)
//                {
//                    //HTTP upload
//                    Shared.Application.LocationAction?.Invoke(location.Coordinate.Longitude, location.Coordinate.Latitude);
//                    endBackgroundUpdateTask();
//                }
//                else//后台定位
//                {
//                    //假如上一次的上传操作尚未结束 则直接return
//                    if (taskIdentifier != UIApplication.BackgroundTaskInvalid)
//                    {
//                        return;
//                    }
//                    beingBackgroundUpdateTask();
//                    //HTTP upload
//                    Shared.Application.LocationAction?.Invoke(location.Coordinate.Longitude, location.Coordinate.Latitude);
//                    //上传完成记得调用
//                    endBackgroundUpdateTask();
//                }
//            }
            #endregion
        }
        public override void OnResignActivation(UIApplication application) => OnResignActivationAction?.Invoke(application);// Invoked when the application is about to move from active to inactive state.// This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) // or when the user quits the application and it begins the transition to the background state.// Games should use this method to pause the game.