標籤: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中的定位服務