我在AppDelegate文件中使用Swift在我的应用程序中添加了CLLocationManager。
在Appdelegate.swift文件中,
import CoreLocation
@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {
var locationManager: CLLocationManager!
在didbecomeActive方法中:
func applicationDidBecomeActive(application: UIApplication) {
if((locationManager) != nil)
{
locationManager.stopMonitoringSignificantLocationChanges()
locationManager.delegate = nil
locationManager = nil
}
locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.activityType = CLActivityType.OtherNavigation
locationManager.requestAlwaysAuthorization()
locationManager.startMonitoringSignificantLocationChanges()
}
如果我使用startUpdatingLocation
,则会调用didUpdateLocations
方法,但不会调用startMonitoringSignificantLocationChanges
。
为什么没有为startMonitoringSignificantLocationChanges
打电话。
我在ios模拟器中测试了这个。我不知道如何办理登机手续。
答案 0 :(得分:21)
它正在工作,但是很难触发显着的位置更改 - 通常在设备更换手机信号塔时发生 - 我不认为可以使用模拟器。
你可能不得不乘坐自行车/汽车,并且行驶至少几公里。
你可以使用一个技巧,这将触发重大的位置变化:
反复按几秒间隔打开和关闭iPhone中的Airplaine模式,它应该诱使设备认为它改变了手机信号塔并触发了显着的位置变化。
答案 1 :(得分:3)
在您的模拟器中,转到Debug-> Location-> Custom并更改位置,然后测试它。
答案 2 :(得分:0)
注意: 当设备距其先前的通知500米或更多距离时,应用程序会期望收到通知。它不应期望通知的频率超过每五分钟一次。如果设备能够从网络检索数据,则位置管理器更有可能及时传递通知。 startMonitoringSignificantLocationChanges()
答案 3 :(得分:0)
在模拟器中,选择功能->位置->高速公路驱动器
稍等startMonitoringSignificantLocationChanges
来触发didUpdateLocations
。