【问题标题】:iOS Significant Location Change and didUpdateLocationsiOS 重大位置更改和 didUpdateLocations
【发布时间】:2014-11-25 07:48:25
【问题描述】:

我显然在这里遗漏了一些基本的东西。我正忙于监控重大位置变化的应用程序。我运行应用程序。然后完全关闭它,我的委托中的 didUpdateLocations 在必要时被触发,但我的位置为零。

- (void)locationManager:(CLLocationManager *)manager
     didUpdateLocations:(NSArray *)locations {
    NSLog(@"didUpdateLocations: %@", [locations lastObject]);
    CLLocation* location = [locations lastObject];
    NSLog(@"latitude %+.6f, longitude %+.6f\n",
          location.coordinate.latitude,
          location.coordinate.longitude); 
}

有什么想法/帮助吗?

谢谢

【问题讨论】:

  • 你在哪个 iOS 上尝试这个?
  • 最新版本 8.1.1

标签: ios location


【解决方案1】:

将此添加到您的 plist 中

<key>NSLocationAlwaysUsageDescription</key>
<string></string>
<key>NSLocationWhenInUseUsageDescription</key>
<string></string>

还有这段代码

#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)


    if(IS_OS_8_OR_LATER) {
            [locationManager requestAlwaysAuthorization];
        }

【讨论】:

  • 我没有真正检查过,因为我不支持 iOS 6,但我会尝试 iOS 7 看看是否可行
  • 使用此代码。您可以在要调用它的时间后设置时间。我尝试了很多代码,但我发现这是最准确的并列出了电池问题。这对于后台定位服务也很棒。
【解决方案2】:

在 iOS 9 中这是我所做的,在 plist 中我添加了:

<key>NSLocationAlwaysUsageDescription</key>
<string>Location is required to find out where you are</string>

在课堂上您使用核心位置管理器

 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {

     BOOL isInBackground = NO;
    if ([UIApplication sharedApplication].applicationState == UIApplicationStateBackground)
    {
        isInBackground = YES;
    }

    if (isInBackground) {
        // Do something while in the background
    } else {
        // while in use
    }

 }

在代理中,我开始监控重要的位置服务

- (void)applicationDidEnterBackground:(UIApplication *)application {
    NSLog(@"App going to the background");
    [_locationManager startMonitoringSignificantLocationChanges];
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-05-27
    • 1970-01-01
    • 1970-01-01
    • 2016-04-25
    • 1970-01-01
    • 2017-06-04
    • 2014-08-13
    • 1970-01-01
    相关资源
    最近更新 更多