2015-10-29 21 views
1

在此找到多個帖子,但仍然無法將它拼湊在一起。解析geoPointForCurrentLocationInBackground Swift示例

我正在使用分析檢索用戶的當前位置。文檔使它看起來非常簡單,但似乎缺少了幾件事情。 https://parse.com/docs/ios/guide#geopoints-getting-the-user-39-s-current-location

首先,我的代碼:

class TableViewController: UITableViewController, CLLocationManagerDelegate { 

override func viewDidLoad() { 
    super.viewDidLoad() 

    print("Get GPS") 
    PFGeoPoint.geoPointForCurrentLocationInBackground { (geoPoint, error) -> Void in 
     if error == nil { 
      print("Got geoPoint") //Never reaches this 
      print(geoPoint) 
     } else { 
      print(error) //No error either 
     } 
    } 
  • 我已經更新了NSLocationWhenInUseUsageDescription在我的info.plist
  • 嘗試模擬器和兩個真正的設備。
  • 新增CLLocationManagerDelegate

我試圖避免這複雜得多,它需要的。

我也試過使用CLLocationManager示例,它似乎也沒有工作。

我正在使用最新版本的所有內容......昨天開始使用Parse!

我從來沒有被授權使用我的位置的挑戰。試着用CLLocationManager的例子!

非常感謝一些指導/支持。

回答

0

在對Parse的位置更新進行處理之前,你是錯過了一些東西。

首先,將位置管理器屬性添加到您的班級。

class TableViewController: UITableViewController, CLLocationManagerDelegate { 

    var locationManager = CLLocationManager() 

設置它的委託,並用它來從用戶請求授權:

override func viewDidLoad() { 
    super.viewDidLoad() 

    locationManager.delegate = self 
    locationManager.requestWhenInUseAuthorization() 

    print("Get GPS") 
    PFGeoPoint.geoPointForCurrentLocationInBackground { (geoPoint, error) -> Void in 
     if error == nil { 
      print("Got geoPoint") //Never reaches this 
      print(geoPoint) 
     } else { 
      print(error) //No error either 
     } 
    } 
} 

在你的Info.plist,你需要添加一個關鍵NSLocationWhenInUseUsageDescription,並給它一個消息到一個字符串值用戶在授權請求期間。

授權請求和位置更新應在上述更改後生效。

+0

如何更新位置,geoPointForCurrentLocationInBackground檢索到的'geoPoint'?換句話說,如果用戶打開應用程序並調用此方法,那麼用戶將移動到新位置,如何將geoPoint設置爲新位置? – dnadri