Last changes to iOS implementation
MODIFY
geolocator/example/lib/main.dart
geolocator/example/lib/main.dart
@@ -115,13 +115,13 @@ class _GeolocatorWidgetState extends State<GeolocatorWidget> {
onPressed: _toggleListening,
label: Text(() {
if (_positionStreamSubscription == null) {
- return "Start Position Updates stream";
+ return "Start position updates stream";
} else {
final buttonText = _positionStreamSubscription!.isPaused
? "Resume"
: "Pause";
- return "$buttonText stream";
+ return "$buttonText position updates stream";
}
}()),
backgroundColor: _determineButtonColor(),
@@ -134,16 +134,17 @@ class _GeolocatorWidgetState extends State<GeolocatorWidget> {
onPressed: _toggleLocationServiceListener,
label: Text(() {
if (_locationServiceStatusSubscription == null) {
- return "Start Location Service stream";
+ return "Start location service stream";
} else {
- final buttonText = _locationServiceStatusSubscription!.isPaused
+ final buttonText =
+ _locationServiceStatusSubscription!.isPaused
? "Resume"
: "Pause";
- return "$buttonText stream";
+ return "$buttonText location service stream";
}
- }())
- )
- ),
+ }()),
+ backgroundColor: _determineLocationServiceButtonColor(),
+ )),
Positioned(
bottom: 290.0,
right: 10.0,
@@ -180,14 +181,23 @@ class _GeolocatorWidgetState extends State<GeolocatorWidget> {
bool _isListening() => !(_positionStreamSubscription == null ||
_positionStreamSubscription!.isPaused);
+ bool _isLocationServiceListening() =>
+ !(_locationServiceStatusSubscription == null ||
+ _locationServiceStatusSubscription!.isPaused);
+
Color _determineButtonColor() {
return _isListening() ? Colors.green : Colors.red;
}
+ Color _determineLocationServiceButtonColor() {
+ return _isLocationServiceListening() ? Colors.green : Colors.red;
+ }
+
void _toggleLocationServiceListener() {
if (_locationServiceStatusSubscription == null) {
final serviceStatusStream = Geolocator.getServiceStatusStream();
- _locationServiceStatusSubscription = serviceStatusStream.handleError((error){
+ _locationServiceStatusSubscription = serviceStatusStream
+ .handleError((error) {
_locationServiceStatusSubscription?.cancel();
_locationServiceStatusSubscription = null;
}).listen((status) => setState(() => _positionItems.add(_PositionItem(
@@ -242,11 +252,7 @@ class _GeolocatorWidgetState extends State<GeolocatorWidget> {
}
}
-enum _PositionItemType {
- permission,
- position,
- locationservicestatus
-}
+enum _PositionItemType { permission, position, locationservicestatus }
class _PositionItem {
_PositionItem(this.type, this.displayValue);

MODIFY
geolocator/ios/Classes/GeolocatorPlugin.m
geolocator/ios/Classes/GeolocatorPlugin.m
#import "Utils/LocationDistanceMapper.h"
#import "Utils/LocationMapper.h"
-@interface GeolocatorPlugin() <FlutterStreamHandler>
+@interface GeolocatorPlugin() <FlutterStreamHandler, CLLocationManagerDelegate>
@property (strong, nonatomic) GeolocationHandler *geolocationHandler;
@property (strong, nonatomic) PermissionHandler *permissionHandler;
+@property (strong, nonatomic) CLLocationManager *locationManager;
@end
@implementation GeolocatorPlugin {
FlutterEventSink _eventSink;
+ FlutterEventSink _locationServiceEventSink;
}
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {
FlutterEventChannel *eventChannel = [FlutterEventChannel
eventChannelWithName:@"flutter.baseflow.com/geolocator_updates"
binaryMessenger:registrar.messenger];
+ FlutterEventChannel *locationServiceEventChannel = [FlutterEventChannel
+ eventChannelWithName:@"flutter.baseflow.com/geolocator_service_updates"
+ binaryMessenger:registrar.messenger];
GeolocatorPlugin *instance = [[GeolocatorPlugin alloc] init];
[registrar addMethodCallDelegate:instance channel:methodChannel];
[eventChannel setStreamHandler:instance];
+ [locationServiceEventChannel setStreamHandler:instance];
}
- (void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result {
}
- (FlutterError *_Nullable)onListenWithArguments:(id _Nullable)arguments eventSink:(FlutterEventSink)eventSink {
+ if(arguments != nil){
if (_eventSink) {
return [FlutterError errorWithCode: GeolocatorErrorLocationSubscriptionActive
message: @"Already listening for location updates. If you want to restart listening please cancel other subscriptions first."
[weakSelf onLocationFailureWithErrorCode:errorCode
errorDescription:errorDescription];
}];
+ }else{
+ _locationServiceEventSink = eventSink;
+ if(self.locationManager == nil){
+ self.locationManager = [[CLLocationManager alloc] init];
+ self.locationManager.delegate = self;
+ }
+ }
return nil;
}
- (FlutterError *_Nullable)onCancelWithArguments:(id _Nullable)arguments {
[[self geolocationHandler] stopListening];
_eventSink = nil;
-
+ _locationServiceEventSink = nil;
+ self.locationManager = nil;
return nil;
}
}];
}
+- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
+ if([CLLocationManager locationServicesEnabled]){
+ _locationServiceEventSink([NSNumber numberWithBool:YES]);
+ }else{
+ _locationServiceEventSink([NSNumber numberWithBool:NO]);
+ }
+}
+
- (void)openSettings:(FlutterResult)result {
if (@available(iOS 10, *)) {
[[UIApplication sharedApplication]

Last changes to iOS implementation