gpt4 book ai didi

ios - NSLocation 不等我点击允许

转载 作者:行者123 更新时间:2023-11-29 01:56:59 26 4
gpt4 key购买 nike

当我运行代码时,弹出窗口请求使用位置的权限,但几乎立即消失,用户没有机会单击“允许”。有没有办法在继续之前强制执行此操作?

import UIKit
import MapKit
import CoreLocation

class MapViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate{

var map:MKMapView?
var manager:CLLocationManager!

convenience init(frame:CGRect){
self.init(nibName: nil, bundle: nil)
self.view.frame = frame

self.map = MKMapView(frame: frame)
self.map!.delegate = self

self.view.addSubview(self.map!)

}

override func viewDidLoad() {
super.viewDidLoad()

// Core Location
manager = CLLocationManager()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest

}

func locationManager(manager: CLLocationManager!,
didChangeAuthorizationStatus status: CLAuthorizationStatus){

print("The authorization status of location services is changed to: ")

switch CLLocationManager.authorizationStatus(){
case .Denied:
println("Denied")
case .NotDetermined:
println("Not determined")
case .Restricted:
println("Restricted")
default:
println("Authorized")
}

}

func displayAlertWithTitle(title: String, message: String){
let controller = UIAlertController(title: title,
message: message,
preferredStyle: .Alert)

controller.addAction(UIAlertAction(title: "OK",
style: .Default,
handler: nil))

presentViewController(controller, animated: true, completion: nil)

}

override func viewDidAppear(animated: Bool) {
super.viewDidAppear(animated)

if CLLocationManager.locationServicesEnabled(){
switch CLLocationManager.authorizationStatus(){
case .Denied:
displayAlertWithTitle("Not Determined",
message: "Location services are not allowed for this app")
case .NotDetermined:
manager.requestWhenInUseAuthorization()
manager.startUpdatingLocation()
case .Restricted:
displayAlertWithTitle("Restricted",
message: "Location services are not allowed for this app")
default:
println("Default")
}

} else {
println("Location services are not enabled")
}

}


func locationManager(manager:CLLocationManager, didUpdateLocations locations:[AnyObject]) {

var userLocation:CLLocation = locations[0] as! CLLocation
var latitude:CLLocationDegrees = userLocation.coordinate.latitude
var longitude:CLLocationDegrees = userLocation.coordinate.longitude
var latDelta:CLLocationDegrees = 1.0
var lonDelta:CLLocationDegrees = 1.0

var span:MKCoordinateSpan = MKCoordinateSpanMake(latDelta, lonDelta)
var location:CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
var region:MKCoordinateRegion = MKCoordinateRegionMake(location, span)
map!.setRegion(region, animated: true)
manager.stopUpdatingLocation()

}

最佳答案

请试试这个:

import UIKit
import GoogleMaps
import CoreLocation

class StartViewController: UIViewController,CLLocationManagerDelegate,GMSMapViewDelegate {

var locationManager: CLLocationManager = CLLocationManager()

override func viewDidLoad() {
super.viewDidLoad()

setupLocationManager()

}

func setupLocationManager() {
locationManager.desiredAccuracy = kCLLocationAccuracyBest
locationManager.delegate = self
locationManager.requestWhenInUseAuthorization()
}

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
locationHandler()
}

func locationHandler() {

if CLLocationManager.locationServicesEnabled() == true {

if (CLLocationManager.authorizationStatus() == .denied) {
// The user denied authorization

} else if (CLLocationManager.authorizationStatus() == .authorizedAlways) {
// The user accepted authorization

} else if (CLLocationManager.authorizationStatus() == .notDetermined){
// The user not determiend authorization

}else if (CLLocationManager.authorizationStatus() == .authorizedWhenInUse){
// In use

}else{ }
}else{
//Access to user location permission denied!

}


}



}//class

成功。

关于ios - NSLocation 不等我点击允许,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30737530/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com