gpt4 book ai didi

ios - 在 iOS 中找不到位置并自动关闭警报

转载 作者:行者123 更新时间:2023-11-28 15:14:16 26 4
gpt4 key购买 nike

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

@IBOutlet weak var mapKit: MKMapView!

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[0]
let span: MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01)
let myLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
let region: MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
mapKit.setRegion(region, animated: true)
self.mapKit.showsUserLocation = true
}

override func viewDidLoad() {
super.viewDidLoad()
let manager = CLLocationManager()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestAlwaysAuthorization()
manager.startUpdatingLocation()
}
}

这是我的源代码。我正在使用 Xcode 9 和 swift 4.0

当我构建并运行时,调试区域打印此“无法从角 4 插入合法属性”,并使用 GPS 警报自动关闭。所以我不能接受使用 GPS 允许。

我也是这样完成info.plist设置的。

隐私 - 位置始终和使用时使用说明隐私 - 位置始终使用说明隐私 - 使用时的位置使用说明。

问题是什么?

最佳答案

将 CLLocationManager 设为全局实例变量 -

    @IBOutlet weak var mapKit: MKMapView!
let manager = CLLocationManager()

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[0]
let span: MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01)
let myLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
let region: MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
mapKit.setRegion(region, animated: true)
self.mapKit.showsUserLocation = true
}


override func viewDidLoad() {
super.viewDidLoad()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestAlwaysAuthorization()
manager.startUpdatingLocation()
}

这将防止警报自动解除。

关于ios - 在 iOS 中找不到位置并自动关闭警报,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47154936/

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