iOS位置事件未在模拟器中触发

时间:2018-07-16 19:03:49

标签: ios swift geolocation

我正在尝试在正在使用的iOS应用中触发位置事件,但是由于某些原因,当我更改调试位置时,事件未触发。

我对XCode / iOS模拟器位置调试选项也很困惑。似乎有两个不同的位置调试菜单。 XCode中的一种,iOS模拟器中的一种。我尝试过更改两者,但是都没有引发位置更改事件。

这是我的应用代码:

import UIKit
import CoreLocation

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {

  var window: UIWindow?
  var isLocationLaunch: Bool = false;
  var locationManager: CLLocationManager!;


  func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
    self.isLocationLaunch = (launchOptions?[.location] as! Bool?) ?? false;
    self.locationManager = CLLocationManager();
    self.locationManager.delegate = self;
    self.enableLocationServices();
    return true
  }

  func applicationWillResignActive(_ application: UIApplication) {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
  }

  func applicationDidEnterBackground(_ application: UIApplication) {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
  }

  func applicationWillEnterForeground(_ application: UIApplication) {
    // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
  }

  func applicationDidBecomeActive(_ application: UIApplication) {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
  }

  func applicationWillTerminate(_ application: UIApplication) {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
  }

  func enableLocationServices() {
    locationManager.delegate = self

    switch CLLocationManager.authorizationStatus() {
      case .notDetermined:
        // Request when-in-use authorization initially
        NSLog("Requesting location authorization");
        locationManager.requestWhenInUseAuthorization()
        break

      case .restricted, .denied:
        NSLog("Location authorization restricted or denied")
        // Disable location features
        break

      case .authorizedWhenInUse:
        NSLog("Location authorization allowed when in use")
        // Enable basic location features
        break

      case .authorizedAlways:
        NSLog("Location authorization allowed always")
        // Enable any of your app's location features
        break
    }
  }

  func locationManager(_ manager: CLLocationManager,
           didUpdateLocations locations: [CLLocation]) {
    NSLog("didUpdateLocations %@", locations);
  }


}

还有我的info.plist(我可以找到每个位置权限说明):

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>CFBundleDevelopmentRegion</key>
    <string>$(DEVELOPMENT_LANGUAGE)</string>
    <key>CFBundleExecutable</key>
    <string>$(EXECUTABLE_NAME)</string>
    <key>CFBundleIdentifier</key>
    <string>$(PRODUCT_BUNDLE_IDENTIFIER)</string>
    <key>CFBundleInfoDictionaryVersion</key>
    <string>6.0</string>
    <key>CFBundleName</key>
    <string>$(PRODUCT_NAME)</string>
    <key>CFBundlePackageType</key>
    <string>APPL</string>
    <key>CFBundleShortVersionString</key>
    <string>1.0</string>
    <key>CFBundleVersion</key>
    <string>1</string>
    <key>LSRequiresIPhoneOS</key>
    <true/>
    <key>NSLocationAlwaysAndWhenInUseUsageDescription</key>
    <string>GoNote requires location services at all times.</string>
    <key>NSLocationAlwaysUsageDescription</key>
    <string>GoNote required location services at all times.</string>
    <key>NSLocationUsageDescription</key>
    <string>GoNote uses location services to find nearby notes.</string>
    <key>NSLocationWhenInUseUsageDescription</key>
    <string>GoNote required location services when in use.</string>
    <key>UIBackgroundModes</key>
    <array>
        <string>location</string>
    </array>
    <key>UILaunchStoryboardName</key>
    <string>LaunchScreen</string>
    <key>UIMainStoryboardFile</key>
    <string>Main</string>
    <key>UIRequiredDeviceCapabilities</key>
    <array>
        <string>armv7</string>
    </array>
    <key>UISupportedInterfaceOrientations</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
    </array>
    <key>UISupportedInterfaceOrientations~ipad</key>
    <array>
        <string>UIInterfaceOrientationPortrait</string>
        <string>UIInterfaceOrientationPortraitUpsideDown</string>
        <string>UIInterfaceOrientationLandscapeLeft</string>
        <string>UIInterfaceOrientationLandscapeRight</string>
    </array>
</dict>
</plist>

我的后台模式功能:

XCode background modes capabilities

位置管理器似乎正在成功获取权限("Location authorization allowed when in use"已被记录)。

这也是令人困惑的位置调试菜单:

iOS simulator location debug menu

XCode location debug menu

2 个答案:

答案 0 :(得分:1)

使用Docs 此方法编码

func requestWhenInUseAuthorization()
  

此方法异步运行,并提示用户向应用授予使用位置服务的权限。用户提示包含应用程序的Info.plist文件中NSLocationWhenInUseUsageDescription键中的文本,并且在调用此方法时需要该键的存在。确定状态后,位置管理器会将结果传递给委托人的locationManager(:didChangeAuthorization :)方法。如果当前授权状态不是CLAuthorizationStatus.notDetermined,则此方法不执行任何操作,也不调用locationManager(:didChangeAuthorization :)方法

//

this

func startUpdatingLocation()
  

调用此方法将导致位置管理器获得初始位置修复(可能需要几秒钟),并通过调用其locationManager(_:didUpdateLocations :)方法来通知您的委托。之后,接收者主要在超过distanceFilter属性中的值时生成更新事件。但是,在其他情况下也可能会提供更新。例如,如果硬件收集到更准确的位置读数,则接收方可能会发送另一条通知

答案 1 :(得分:1)

希望您解决了这个问题。无论如何,调试器位置模拟器有很多错误。 我挣扎了好几个星期,因为它运行良好,然后在更新Xcode之后它就停止了正常工作。有时工作,有时则不行。而且我不会更改任何有关位置和地理围栏的内容。 显然,我已经在设备上尝试过该应用,并且始终可以正常工作