iOS8中的定位服務

來源:互聯網
上載者:User

標籤:des   style   blog   http   color   io   os   使用   ar   

iOS8中的定位服務

My app that worked fine in iOS 7 doesn‘t work with the iOS 8 SDK.

CLLocationManager doesn‘t return a location, and I don‘t see my app under Settings > Location Services either. I did a Google search on the issue but nothing came up, what could be wrong?

我的應用程式在iOS7上運行得好好的,但是到了iOS8上就出問題了.

在iOS8中,CLLocationManager並沒有返回一個地址,而且,我也沒有在Settings>Location Services中找到該服務,在Google上也沒查到類似的問題,請問到底哪裡出問題了?

 

I ended up solving my own problem.

Apparently in iOS 8 SDK, requestAlwaysAuthorization (for background location) or requestWhenInUseAuthorization (location only when foreground) call on CLLocationManager is needed before starting location updates.

There also needs to be NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription key in Info.plist with a message to be displayed in the prompt. Adding these solved my problem.

Hope it helps someone else.

最後我解決了這個問題.

在iOS8的sdk中,你需要額外的requestAlwaysAuthorization(用作後台定位)以及requestWhenInUseAuthorization(用作前台定位),在調用CLLocationManager更新地址之前你都需要調用上述兩個方法.你還需要在Info.plist檔案中額外的添加NSLocationAlwaysUsageDescription以及NSLocationWhenInUseUsageDescription,這麼做了之後才解決了我的問題.

 

According to the Apple docs:

https://developer.apple.com/library/prerelease/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/index.html#//apple_ref/occ/instm/CLLocationManager/requestWhenInUseAuthorization

As of iOS 8, the presence of a NSLocationWhenInUseUsageDescription or a NSLocationAlwaysUsageDescription key value in your app‘s Info.plist file is required. It‘s then also necessary to request permission from the user prior to registering for location updates, either by calling [self.myLocationManager requestWhenInUseAuthorization] or [self.myLocationManager requestAlwaysAuthorization] depending on your need. The string you entered into the Info.plist will then be displayed in the ensuing dialog.

If the user grants permission, it‘s business as usual. If they deny permission, then the delegate is not informed of location updates.

根據蘋果的官方文檔:

https://developer.apple.com/library/prerelease/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/index.html#//apple_ref/occ/instm/CLLocationManager/requestWhenInUseAuthorization

在iOS8上,你需要在你的plist檔案中添加新的索引值NSLocationWhenInUseUsageDescription或者NSLocationAlwaysUsageDescription.然後,每次定位的時候你都需要擷取使用者的授權資訊,通過調用[self.myLocationManager requestWhenInUseAuthorization] 或者[self.myLocationManager requestAlwaysAuthorization],至於調用哪一個,這就根據你自己的需求了.那個你添加進plist檔案中的索引值資訊會在確認的資訊中有提示.

如果使用者允許你定位,你可以像往常那樣子使用定位功能.如果他們拒絕了,那麼,這個定位的更新更能永遠也不會被執行.

 

iOS8中的定位服務

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.