gpt4 book ai didi

ios - CLLocationManagerDelegate 方法不适用于 Apple Watch 扩展

转载 作者:行者123 更新时间:2023-11-29 02:03:46 28 4
gpt4 key购买 nike

我正在开发 Apple Watch 应用来检索用户位置。我已经将 CoreLocation 框架导入到 interfacecontroller 类中,并向类中添加了委托(delegate)。这是行不通的。请检查下面的代码。我错过了什么?

    import Foundation
import WatchKit
import CoreLocation


class NearByController: WKInterfaceController, CLLocationManagerDelegate {



override func awakeWithContext(context: AnyObject?) {

var locationManager = CLLocationManager()
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingLocation()

}
}

// MARK: CLLocationManagerDelegate

func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
println("Retrieved location")
}



func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
println(error)
}

最佳答案

似乎我需要将 CLLocationManager 初始化为类属性而不是内部方法。这是我的答案。它现在正在工作。

import Foundation
import WatchKit
import CoreLocation


class NearByController: WKInterfaceController, CLLocationManagerDelegate {


var locationManager = CLLocationManager()

override func awakeWithContext(context: AnyObject?) {

locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.requestWhenInUseAuthorization()

if CLLocationManager.locationServicesEnabled() {
locationManager.startUpdatingLocation()
}
}


// MARK: CLLocationManagerDelegate


func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
locationManager.stopUpdatingLocation()
println(locations)
}



func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
println(error)
}

关于ios - CLLocationManagerDelegate 方法不适用于 Apple Watch 扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30043405/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com