(Obj-C和Unity)使用CoreLocation

时间:2018-11-29 16:12:04

标签: ios objective-c unity3d

我正在尝试为目标C中的现有Unity应用程序编写一个IOS插件。当应用程序位于前景和背景中时,我设法使其工作,但是当应用程序终止时,它不会发生位置更改时唤醒应用程序。 我正在通过设置方法设置服务:

-(void)setup
{
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
    locationManager.allowsBackgroundLocationUpdates = YES;

    if([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)
    {
         [locationManager requestAlwaysAuthorization];
    }

    NSLog(@"LocationManager initalised..");

}

然后,我有以下委托覆盖,这些委托覆盖使用Unity提供的覆盖来访问这些功能:

- (void)applicationDidEnterBackground: (UIApplication*)application
{
    isFromBackground = @"background";

    [locationManager startMonitoringSignificantLocationChanges];

    return [super applicationDidEnterBackground:application];
}

- (void)applicationWillTerminate:(UIApplication *)application
{
    isFromBackground = @"terminated";

    [locationManager startMonitoringSignificantLocationChanges];

    return [super applicationWillTerminate:application];
}

-(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:    (NSDictionary *)launchOptions
{
    if ([launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey])
    {
        isFromBackground = @"terminated";

        //we launched from a location update
        //stop previous location updates
        [locationManager stopUpdatingLocation];
        [locationManager stopMonitoringSignificantLocationChanges];

        //create new location manager (and a new instance if we need to)
        [_sharedInstance setup];

        //start significant updates again
        [locationManager startMonitoringSignificantLocationChanges];
        [locationManager startUpdatingLocation];
    }
    else
    {
        //create new location manager (and a new instance if we need to)
        [_sharedInstance setup];

        isFromBackground = @"foreground";
        [locationManager startUpdatingLocation];

    }
    //this will call unity's own implementation of the app delegate function
    return [super application:application     didFinishLaunchingWithOptions:launchOptions];
}

以下代码处理来自CLLocation的回调:

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:    (NSArray<CLLocation *> *)locations
{
    //NSLog(@"%f", locations.lastObject.coordinate.latitude);
    //NSLog(@"%f", locations.lastObject.coordinate.longitude);

    CLLocation* loc = locations.lastObject;

    [_sharedInstance sendToApi:loc];
}

-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError     *)error
{
    NSLog(@"Location updates failed (might be because you're trying to stop     significant changes even before you start them - it's not really a     problem!) Error log follows:");
    NSLog(@"%@",error);
}

首先负责启动它,这在启动时从整体上称为:

-(void)startForegroundLocationService:(NSString *)_token
{
    token = _token;
    isFromBackground = @"foreground";
    [locationManager startUpdatingLocation];
}

现在,问题是在终止应用程序并更改了手机/模拟器的位置后,根本没有调用“ didFinishLaunchingWithOptions”。据我了解,更改位置应该可以唤醒应用程序,调用该函数并让其执行关闭前的操作。我知道我的代表正在工作,因为该函数是在初次启动时调用的,这意味着我已成功覆盖了Unity方法。

我这里缺少什么吗?还是Unity在应用程序生命周期中做了一些花哨的事情?

0 个答案:

没有答案