Skip to content

Commit

Permalink
Merge pull request #13 from LastFriday/master
Browse files Browse the repository at this point in the history
iOS LocationPlugin rewritten in Objective-C instead of Swift
  • Loading branch information
Lyokone authored Oct 19, 2017
2 parents 1867adb + 21bf751 commit ee90294
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 90 deletions.
81 changes: 78 additions & 3 deletions ios/Classes/LocationPlugin.m
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
87 changes: 0 additions & 87 deletions ios/Classes/SwiftLocationPlugin.swift

This file was deleted.

0 comments on commit ee90294

Please sign in to comment.