-
-
Notifications
You must be signed in to change notification settings - Fork 836
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from LastFriday/master
iOS LocationPlugin rewritten in Objective-C instead of Swift
- Loading branch information
Showing
2 changed files
with
78 additions
and
90 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,8 +1,83 @@ | ||
#import "LocationPlugin.h" | ||
#import <location/location-Swift.h> | ||
|
||
@import CoreLocation; | ||
|
||
@interface LocationPlugin() <FlutterStreamHandler, CLLocationManagerDelegate> | ||
@property (strong, nonatomic) CLLocationManager *clLocationManager; | ||
@property (copy, nonatomic) FlutterResult flutterResult; | ||
@property (copy, nonatomic) FlutterEventSink flutterEventSink; | ||
@property (assign, nonatomic) BOOL flutterListening; | ||
@end | ||
|
||
@implementation LocationPlugin | ||
+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar { | ||
[SwiftLocationPlugin registerWithRegistrar:registrar]; | ||
|
||
+(void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar { | ||
FlutterMethodChannel *channel = [FlutterMethodChannel methodChannelWithName:@"lyokone/location" binaryMessenger:registrar.messenger]; | ||
FlutterEventChannel *stream = [FlutterEventChannel eventChannelWithName:@"lyokone/locationstream" binaryMessenger:registrar.messenger]; | ||
|
||
LocationPlugin *instance = [[LocationPlugin alloc] init]; | ||
[registrar addMethodCallDelegate:instance channel:channel]; | ||
[stream setStreamHandler:instance]; | ||
} | ||
|
||
-(instancetype)init { | ||
self = [super init]; | ||
if (self) { | ||
self.flutterListening = NO; | ||
} | ||
return self; | ||
} | ||
|
||
-(void)handleMethodCall:(FlutterMethodCall*)call result:(FlutterResult)result { | ||
if ([call.method isEqualToString:@"getLocation"]) { | ||
self.flutterResult = result; | ||
if ([CLLocationManager locationServicesEnabled]) { | ||
self.clLocationManager = [[CLLocationManager alloc] init]; | ||
self.clLocationManager.delegate = self; | ||
if ([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationWhenInUseUsageDescription"] != nil) { | ||
[self.clLocationManager requestWhenInUseAuthorization]; | ||
} | ||
else if ([[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSLocationAlwaysUsageDescription"] != nil) { | ||
[self.clLocationManager requestAlwaysAuthorization]; | ||
} | ||
else { | ||
[NSException raise:NSInternalInconsistencyException format:@"To use location in iOS8 you need to define either NSLocationWhenInUseUsageDescription or NSLocationAlwaysUsageDescription in the app bundle's Info.plist file"]; | ||
} | ||
|
||
self.clLocationManager.desiredAccuracy = kCLLocationAccuracyBest; | ||
[self.clLocationManager startUpdatingLocation]; | ||
} | ||
} | ||
else { | ||
result(FlutterMethodNotImplemented); | ||
} | ||
} | ||
|
||
-(FlutterError*)onListenWithArguments:(id)arguments eventSink:(FlutterEventSink)events { | ||
self.flutterEventSink = events; | ||
self.flutterListening = YES; | ||
return nil; | ||
} | ||
|
||
-(FlutterError*)onCancelWithArguments:(id)arguments { | ||
self.flutterListening = NO; | ||
return nil; | ||
} | ||
|
||
-(void)locationManager:(CLLocationManager*)manager didUpdateLocations:(NSArray<CLLocation*>*)locations { | ||
CLLocation *location = locations.firstObject; | ||
NSDictionary<NSString*,NSNumber*>* coordinatesDict = @{ | ||
@"latitude": @(location.coordinate.latitude), | ||
@"longitude": @(location.coordinate.longitude), | ||
@"accuracy": @(location.horizontalAccuracy), | ||
@"altitude": @(location.altitude), | ||
}; | ||
self.flutterResult(coordinatesDict); | ||
if (self.flutterListening) { | ||
self.flutterEventSink(coordinatesDict); | ||
} else { | ||
[self.clLocationManager stopUpdatingLocation]; | ||
} | ||
} | ||
|
||
@end |
This file was deleted.
Oops, something went wrong.