iOS 位置事件未在模拟器中触发
iOS location events not firing in simulator
我正在尝试让位置事件在我正在处理的 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>
我的后台模式功能:
位置管理器似乎已成功获得权限("Location authorization allowed when in use"
已记录)。
这里还有令人困惑的位置调试菜单:
按照Docs 这个方法
func requestWhenInUseAuthorization()
this method runs asynchronously and prompts the user to grant permission to the app to use location services. The user prompt contains the text from the NSLocationWhenInUseUsageDescription key in your app’s Info.plist file, and the presence of that key is required when calling this method. After the status is determined, the location manager delivers the results to the delegate’s locationManager(:didChangeAuthorization:) method. If the current authorization status is anything other than CLAuthorizationStatus.notDetermined, this method does nothing and does not call the locationManager(:didChangeAuthorization:) method
//
虽然this
func startUpdatingLocation()
Calling this method causes the location manager to obtain an initial location fix (which may take several seconds) and notify your delegate by calling its locationManager(_:didUpdateLocations:) method. After that, the receiver generates update events primarily when the value in the distanceFilter property is exceeded. Updates may be delivered in other situations though. For example, the receiver may send another notification if the hardware gathers a more accurate location reading
希望你解决了问题。无论如何,调试器位置模拟器有很多错误。
我挣扎了数周,因为它运行良好,然后在更新 Xcode 后它停止正常工作。有时工作,有时不工作。而且我不会更改有关位置和地理围栏的任何内容。
显然我已经在设备上试用了该应用程序并且它始终有效
我正在尝试让位置事件在我正在处理的 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>
我的后台模式功能:
位置管理器似乎已成功获得权限("Location authorization allowed when in use"
已记录)。
这里还有令人困惑的位置调试菜单:
按照Docs 这个方法
func requestWhenInUseAuthorization()
this method runs asynchronously and prompts the user to grant permission to the app to use location services. The user prompt contains the text from the NSLocationWhenInUseUsageDescription key in your app’s Info.plist file, and the presence of that key is required when calling this method. After the status is determined, the location manager delivers the results to the delegate’s locationManager(:didChangeAuthorization:) method. If the current authorization status is anything other than CLAuthorizationStatus.notDetermined, this method does nothing and does not call the locationManager(:didChangeAuthorization:) method
//
虽然this
func startUpdatingLocation()
Calling this method causes the location manager to obtain an initial location fix (which may take several seconds) and notify your delegate by calling its locationManager(_:didUpdateLocations:) method. After that, the receiver generates update events primarily when the value in the distanceFilter property is exceeded. Updates may be delivered in other situations though. For example, the receiver may send another notification if the hardware gathers a more accurate location reading
希望你解决了问题。无论如何,调试器位置模拟器有很多错误。 我挣扎了数周,因为它运行良好,然后在更新 Xcode 后它停止正常工作。有时工作,有时不工作。而且我不会更改有关位置和地理围栏的任何内容。 显然我已经在设备上试用了该应用程序并且它始终有效