【问题标题】:handling location event with didFinishLaunchingWithOptions in background mode or after app was terminated在后台模式或应用程序终止后使用 didFinishLaunchingWithOptions 处理位置事件
【发布时间】:2012-06-16 06:26:30
【问题描述】:

我在位置管理器 init 中调用 startMonitoringSignificantLocationChanges。

我希望如果有位置事件 didFinishLaunchingWithOptions 将被启动并执行以下代码。 (包括后台模式和应用程序是否被终止)

应用程序冻结(当我在不同位置的后台几个小时后尝试启动它时出现黑屏)

当我收到位置事件时,我可能做错了什么。如果有人知道问题出在哪里,我将不胜感激。

顺便说一句,没有办法模拟这种位置事件,而是物理地移动到具有不同蜂窝塔的不同位置。有没有? ... :(

LocationController.m:

- (id)init
{
    self = [super init];
    if (self != nil) {
        self.locationManager = [[[CLLocationManager alloc] init] autorelease];
        self.locationManager.delegate = self;
        self.locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
        [self.locationManager startUpdatingLocation];
        [self.locationManager startMonitoringSignificantLocationChanges];
        [NSTimer scheduledTimerWithTimeInterval:10 target:self selector:@selector(stop) userInfo:nil repeats:NO];
    }
    return self;
}
appdelegate.m : 
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    id locationValue = [launchOptions objectForKey:UIApplicationLaunchOptionsLocationKey];
    if (locationValue)
    {
        [[LocationController sharedInstance] startMonitoringSignificantLocationChanges];
        UIApplication *app  = [UIApplication sharedApplication];
        bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ 
            [app endBackgroundTask:bgTask]; 
            bgTask = UIBackgroundTaskInvalid;
            }];
        [self updateLocationService];
        return YES;
    }
}
- (void) updateLocationService {
    [[LocationController sharedInstance] start];
    [NSTimer scheduledTimerWithTimeInterval:2 target:self selector:@selector(stop) userInfo:nil repeats:NO];    
}
- (void) stop {
    [[LocationController sharedInstance] stop];
}

谢谢!

【问题讨论】:

    标签: background cllocationmanager cllocation


    【解决方案1】:

    如果没有设置 UIApplicationLaunchOptionsLocationKey,您的 didFinishLaunchingWithOptions: 方法不会返回任何内容。

    只需将 return 语句移到 if 子句之外:

    if (locationValue)
    { 
        ... 
    }
    return YES;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-02-19
      • 1970-01-01
      • 2020-09-01
      • 1970-01-01
      • 2015-05-01
      • 2020-10-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多