Swift 2和watchOS2完全改变了Apple Watch扩展的工作方式,现在我必须重新创建它们.最初,我曾以正常方式请求手表上的位置:
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestAlwaysAuthorization()
self.locationManager.beginUpdatingLocation()
Run Code Online (Sandbox Code Playgroud)
现在在watchOS2中,您只能使用"locationManager.requestLocation()"一次请求一个位置.它应该返回一个位置.这是我如何使用它:
override func awakeWithContext(context: AnyObject?) {
super.awakeWithContext(context)
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestAlwaysAuthorization()
self.locationManager.requestLocation() //crashes right here
}
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print(error)
}
Run Code Online (Sandbox Code Playgroud)
它每次都崩溃,我无法弄清楚原因.我已经清理了构建,确保它正在模拟一个位置(startUpdatingLocation()在父应用程序上正常工作).我该怎么办?
编辑:这是堆栈跟踪:
2015-10-06 12:39:39.196 MyApp WatchKit Extension[11486:2233547] *** Assertion failure in -[CLLocationManager requestLocation], /BuildRoot/Library/Caches/com.apple.xbs/Sources/CoreLocationFramework_Sim/CoreLocation-1861.1.12/Framework/CoreLocation/CLLocationManager.m:818
2015-10-06 12:39:39.198 MyApp WatchKit Extension[11486:2233547] *** Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: 'Delegate must respond to locationManager:didFailWithError:' …Run Code Online (Sandbox Code Playgroud) 我让用户用UIImagePickerController拍照,我需要将它保存到应用程序中,以便在以后需要查看时显示.我怎么能做到这一点?
我听说NSUserDefaults会出错.我需要保存的只是一张图片,而不是更多.