首页 > 解决方案 > iOS 位置事件未在模拟器中触发

问题描述

我正在尝试在我正在开发的 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 后台模式功能

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

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

iOS 模拟器位置调试菜单

XCode 位置调试菜单

标签: iosswiftgeolocation

解决方案


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


推荐阅读