2015-11-17 48 views
1

我試圖在我的WatchOS2應用程序中檢索我的當前位置的Lat Lat。 該應用程序已通過身份驗證,iPhone應用程序正常工作。
info.plist:NSLocationWhenInUseUsageDescription已設置。WatchOS2上的位置始終爲零

我用下面的代碼willActivate()

 locationManager.delegate = self 
     locationManager.desiredAccuracy = kCLLocationAccuracyBest 
     if (CLLocationManager.authorizationStatus() != CLAuthorizationStatus.AuthorizedWhenInUse) 
     { 
      locationManager.requestWhenInUseAuthorization() 
      print("Logon: Location not authorized1") 
     } 

    } 

     locationManager.requestLocation() 

locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])我運行這段代碼:

if locationManager.location != nil 
    { 
     self.userLat = String(self.locationManager.location!.coordinate.latitude) 
     self.userLong = String(self.locationManager.location!.coordinate.longitude) 

     print("Lat\(userLat) Long \(userLong)") 
    } 
    else 
    { 
     print("An error occurred") <- It always prints this error 
    } 
} 


locationManager(manager: CLLocationManager, didFailWithError error: NSError) 

不會被調用

+0

你的緯度和LNG檢查時,應用程序在iPhone上的背景是什麼? – RichAppz

+0

@zappiDev這是一個沒有連接到正在運行的手機應用程序的watchos2應用程序?或者你是什麼意思? – Egghead

+0

好吧,我還沒有與原生WatchOS合作過,上次我手錶依賴手機,所以不得不在後臺請求位置服務。 – RichAppz

回答

0

我找到了解決我的問題的庫。我不知道究竟發生了什麼,但它現在起作用了。

我用OneShotLocationManger by icanzilb

要在WatchOS2應用程序使用此我有,你就必須改變startUpdatingLocation()requestLocation(),因爲WatchOS不允許繼續更新。
像這樣:

//location authorization status changed 
func locationManager(manager: CLLocationManager, didChangeAuthorizationStatus status: CLAuthorizationStatus) { 

    switch status { 
    case .AuthorizedWhenInUse: 
     self.locationManager!.requestLocation() 
    case .Denied: 
     _didComplete(nil, error: NSError(domain: self.classForCoder.description(), 
      code: OneShotLocationManagerErrors.AuthorizationDenied.rawValue, 
      userInfo: nil)) 
    default: 
     break 
    } 
} 

給予信貸,信貸是由於 -

+0

請將此問題標記爲已回答,因爲您已提供答案。 –