我的应用在iOS 7上运行良好,但在iOS 8 SDK上却无法运行。
CLLocationManager不返回位置,我没有看到我的应用程序下设置->位置服务。我在这个问题上做了谷歌搜索,但没有任何结果。会有什么问题呢?
我的应用在iOS 7上运行良好,但在iOS 8 SDK上却无法运行。
CLLocationManager不返回位置,我没有看到我的应用程序下设置->位置服务。我在这个问题上做了谷歌搜索,但没有任何结果。会有什么问题呢?
当前回答
这是ios 8的问题 将此添加到代码中
if (IS_OS_8_OR_LATER)
{
[locationmanager requestWhenInUseAuthorization];
[locationmanager requestAlwaysAuthorization];
}
到info.plist:
<key>NSLocationUsageDescription</key>
<string>I need location</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>I need location</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>I need location</string>
其他回答
在iOS 8中,你需要做两件额外的事情来让定位工作:给你的信息添加一个键。Plist并从位置管理器请求授权,要求它启动
info.plist:
<key>NSLocationUsageDescription</key>
<string>I need location</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>I need location</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>I need location</string>
将此添加到代码中
if (IS_OS_8_OR_LATER)
{
[locationmanager requestWhenInUseAuthorization];
[locationmanager requestAlwaysAuthorization];
}
对我来说,问题是CLLocationManagerDelegate类是私有的,这阻止了所有的委托方法被调用。我想这不是很常见的情况,但我想我应该提到它,以防它能帮助到任何人。
我在iOS9中得到了类似的错误(使用Xcode 7和Swift 2): 试图启动MapKit位置更新而没有提示位置授权。必须先调用-[CLLocationManager requestWhenInUseAuthorization]或-[CLLocationManager requestAlwaysAuthorization]。 我是跟着一个教程,但导师使用的是iOS8和Swift 1.2。在Xcode 7和Swift 2中有一些变化,我发现了这段代码,它对我来说很好(如果有人需要帮助):
import UIKit
import MapKit
import CoreLocation
class MapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
// MARK: Properties
@IBOutlet weak var mapView: MKMapView!
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.delegate = self
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
self.locationManager.requestWhenInUseAuthorization()
self.locationManager.startUpdatingLocation()
self.mapView.showsUserLocation = true
}
// MARK: - Location Delegate Methods
func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations.last
let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1))
self.mapView.setRegion(region, animated: true)
}
func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
print("Errors: " + error.localizedDescription)
}
}
最后,我把它放到info.plist中: 信息属性列表:NSLocationWhenInUseUsageDescription 取值范围:应用需要为员工提供位置服务器
我也因为同样的问题而抓狂。Xcode给出错误:
试图启动MapKit位置更新而没有提示 位置授权。必须调用-[CLLocationManager requestWhenInUseAuthorization]或-[CLLocationManager . request requestAlwaysAuthorization]。
但即使实现了上述方法之一,它也不会提示用户,除非信息中有条目。NSLocationAlwaysUsageDescription或NSLocationWhenInUseUsageDescription。
添加以下行到您的信息。Plist,其中字符串值代表您需要访问用户位置的原因
<key>NSLocationWhenInUseUsageDescription</key>
<string>This application requires location services to work</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>This application requires location services to work</string>
我认为自从我在Xcode 5中开始这个项目以来,这些条目可能已经丢失了。我猜Xcode 6可能会为这些键添加默认条目,但还没有确认。
你可以在这里找到更多关于这两个设置的信息
添加关键NSLocationWhenInUseUsageDescription或NSLocationAlwaysUsageDescription(后台GPS使用)字符串要求使用GPS在每个信息。Plist从每个目标。 通过运行命令获得许可: (自我initLocationManager: locationManager);
其中“initLocationManager”为:
// asks for GPS authorization on iOS 8
-(void) initLocationManager:(CLLocationManager *) locationManager{
locationManager = [[CLLocationManager alloc]init];
if([locationManager respondsToSelector:@selector(requestAlwaysAuthorization)])
[locationManager requestAlwaysAuthorization];
}
记住,如果键不是在每个信息上。Plist为每个目标,应用程序将不会询问用户。if提供了与iOS 7的兼容性,respondsToSelector:方法保证了未来的兼容性,而不仅仅是解决iOS 7和8的问题。