- Home /
Question by
unity_4J5vHhZVP1K9vg · May 29, 2020 at 06:35 AM ·
iosscript.plugin
How to get location from ios plugin in unity?
I want to get latitude and longitude value from ios plugin in unity. My plugin code is in .mm file as below.
#import <CoreLocation/CoreLocation.h>
#import "UnityAppController.h"
@interface LocationUpdate : NSObject{
}
@end
@interface LocationServiceDelegate : NSObject <CLLocationManagerDelegate>
@end
@implementation LocationServiceDelegate
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(@"didFailWithError: %@", error);
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray<CLLocation *> *)locations{
CLLocation *currentLocation = [locations lastObject];
if (currentLocation != nil) {
NSString *latitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
NSString *longitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
// [locationManager stopUpdatingLocation];
NSLog(@"latitude: %@", latitude);
NSLog(@"longitude: %@", longitude);
//i am not sure UnitySendMessage will be appropriate to send both lat and longitude value back to unity or not
///UnitySendMessage("iOSPluginCallBacks", [callback UTF8String], "");
}
}
@end
@implementation LocationUpdate
CLLocationManager *locationManager;
NSString *latitude, *longitude;
LocationServiceDelegate* delegate;
+(void)getCurrentLocation{
locationManager = [[CLLocationManager alloc] init];
// here i have tried locationManager.delegate = self also
locationManager.delegate = delegate;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager requestAlwaysAuthorization];
[locationManager startMonitoringSignificantLocationChanges];
[locationManager startUpdatingLocation];
latitude = @"";
longitude = @"";
}
@end
//Exports:
extern "C" {
void _getLocation(){
[LocationUpdate getCurrentLocation];
}
}
My .cs code is:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.Runtime.InteropServices;
public class iosPlugin : MonoBehaviour
{
#if UNITY_IOS
[DllImport ("__Internal")]
private static extern void _getLocation();
public static void GetCoordinates()
{
_getLocation();
}
#endif
}
i am calling function from my another .cs
iosPlugin.GetCoordinates();
I will get popup for allow permission but after allowing, when i am changing my location from xcode i am not getting call at my delegate methods. and my second concern is after that how to get that values back to unity? UnitySendMessage seems easy and appropriate option but not sure about it. Thanks in advance.
Comment