ios - swift 位置管理器

标签 ios iphone swift location cllocationmanager

我在 Swift 中遇到位置问题。

在模拟器上工作正常但在设备上不工作

我使用模拟器 iPhone 6 iOS 8.3 和设备 iPhone 6 iOS 8.3

我的代码:

import UIKit
import CoreLocation

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate,
CLLocationManagerDelegate {

  var window: UIWindow?
  var locationManager: CLLocationManager! = nil
  var isExecutingInBackground = false

  func locationManager(manager: CLLocationManager!,
    didUpdateToLocation newLocation: CLLocation!,
    fromLocation oldLocation: CLLocation!){
      if isExecutingInBackground{
        println(newLocation);
        locationManager.stopUpdatingLocation()
      } else {
        /* We are in the foreground. Do any processing that you wish */
      }
  }

 func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool
 {
    locationManager = CLLocationManager()
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
    locationManager.requestAlwaysAuthorization()
    locationManager.delegate = self
    locationManager.startUpdatingLocation()
    return true
  }



  func applicationDidEnterBackground(application: UIApplication) {
    isExecutingInBackground = true
    var timer = NSTimer.scheduledTimerWithTimeInterval(30, target: self, selector: Selector("update"), userInfo: nil, repeats: true)


    /* Reduce the accuracy to ease the strain on
    iOS while we are in the background */
    locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters
  }

    func update() {
        println("test");
        locationManager.startUpdatingLocation()
    }

  func applicationWillEnterForeground(application: UIApplication) {
    isExecutingInBackground = false

    /* Now that our app is in the foreground again, let's increase the location
    detection accuracy */
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
  }
}

我想每 30 秒获取一次位置

那么为什么不在设备上工作呢?

最佳答案

最重要的是,对于后台更新,您要么使用“重大更改”服务,要么必须请求后台位置服务(Apple 将其限制为对位置更新有迫切需求的应用程序,即实际导航应用程序)。

参见 Tracking the User’s Location iOS 应用程序编程指南中的部分:

Tracking the User’s Location

There are several ways to track the user’s location in the background, most of which do not actually require your app to run continuously in the background:

  • The significant-change location service (Recommended)
  • Foreground-only location services
  • Background location services

The significant-change location service is highly recommended for apps that do not need high-precision location data. With this service, location updates are generated only when the user’s location changes significantly; thus, it is ideal for social apps or apps that provide the user with noncritical, location-relevant information. If the app is suspended when an update occurs, the system wakes it up in the background to handle the update. If the app starts this service and is then terminated, the system relaunches the app automatically when a new location becomes available. This service is available in iOS 4 and later, and it is available only on devices that contain a cellular radio.

The foreground-only and background location services both use the standard location Core Location service to retrieve location data. The only difference is that the foreground-only location services stop delivering updates if the app is ever suspended, which is likely to happen if the app does not support other background services or tasks. Foreground-only location services are intended for apps that only need location data while they are in the foreground.

You enable location support from the Background modes section of the Capabilities tab in your Xcode project. (You can also enable this support by including the UIBackgroundModes key with the location value in your app’s Info.plist file.) Enabling this mode does not prevent the system from suspending the app, but it does tell the system that it should wake up the app whenever there is new location data to deliver. Thus, this key effectively lets the app run in the background to process location updates whenever they occur.

Important: You are encouraged to use the standard services sparingly or use the significant location change service instead. Location services require the active use of an iOS device’s onboard radio hardware. Running this hardware continuously can consume a significant amount of power. If your app does not need to provide precise and continuous location information to the user, it is best to minimize the use of location services.

有关如何在您的应用中使用各种不同位置服务的信息,请参阅位置和 map 编程指南。

关于ios - swift 位置管理器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30407629/

相关文章:

android - Geolocation cordova Android 插件不起作用

ios - iOS 9.1/9.2 上带有 EXC_BAD_ACCESS CODE 1 的 AppDelegate 类声明时应用程序崩溃

iOS 6.1 兼容性

swift 3.0 : Convert server UTC time to local time and vice-versa

swift - Realm - 属性不能标记为动态,因为它的类型不能在 Objective-C 中表示

ios - 自定义 tableview 单元格下的 UILabel 未更新

ios - dequeueReusableCellWithIdentifier 返回相同索引路径的不同单元格

带有扩展导航栏的 iOS 过渡

iphone - ObjectiveC/CMTime - 将 AVPlayer.duration 转换为毫秒

ios - UIAlert 关闭后恢复 session