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

iOS位置事件未在模拟器中触发,ios,swift,geolocation,Ios,Swift,Geolocation,我正在尝试在正在使用的iOS应用程序中触发位置事件,但由于某些原因,当我更改调试位置时,这些事件不会触发 我还对XCode/iOS模拟器位置调试选项感到非常困惑。似乎有两个不同的位置调试菜单。一个在XCode中,一个在iOS模拟器中。我已经尝试更改这两个事件,但这两个事件都不会导致触发位置更改事件 以下是我的应用程序代码: import UIKit import CoreLocation @UIApplicationMain class AppDelegate: UIResponder, UI

我正在尝试在正在使用的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>
以及我的后台模式功能:

位置管理器似乎已成功获取权限记录使用中允许的位置授权

这里还有令人困惑的位置调试菜单:

根据这种方法

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

//

调用此方法会导致位置管理器获得初始位置修复,这可能需要几秒钟的时间,并通过调用其locationManager\uUdidUpdateLocations:方法通知您的代理。之后,接收器主要在超过distanceFilter属性中的值时生成更新事件。不过,在其他情况下可能会提供更新。例如,如果硬件收集到更准确的位置读数,则接收器可以发送另一个通知


我希望你解决了这个问题。无论如何,调试器位置模拟器有很多bug。 我挣扎了好几个星期,因为它工作得很好,然后在更新Xcode之后,它就正确地停止了工作。有时工作,有时不工作。我不会改变任何关于地理位置和地理围栏的事情。
显然,我已经在一台设备上试用过该应用程序,但它总是能正常工作

您是否打电话给locationManager。startUpdatingLocation@Sh_Khan不。这可能就是问题所在。好吧,试试看,确认这确实是问题所在。继续吧,把它作为答案贴出来,我会接受的。谢谢。显然,在提问之前,我需要花更多的时间阅读文档。是的,现在已经解决了。我对Android和iOS位置API都有过类似的体验。有些事情似乎工作了一段时间,然后突然停止工作,没有任何明确的原因。我正在构建的应用程序在后台运行或终止时需要位置事件,这对测试来说是一个巨大的痛苦。对我来说,同样,我需要在后台更改位置。好吧,至少你可以有个好借口去散步
func startUpdatingLocation()