ios - CoreLocation 为空,应用程序在启动时崩溃

标签 ios swift core-location cllocationmanager

这是我从 Crashlytics 获得的崩溃日志

Crashed: com.apple.main-thread
0                          0x10015e794 specialized AppDelegate.updateloc(String, long : String) -> () (AppDelegate.swift:350)
1                          0x10015e900 specialized AppDelegate.locationManager(CLLocationManager, didUpdateLocations : [CLLocation]) -> () (AppDelegate.swift:281)
2                          0x10015c058 @objc AppDelegate.locationManager(CLLocationManager, didUpdateLocations : [CLLocation]) -> () (AppDelegate.swift)
3  CoreLocation                   0x18961c8b8 (null) + 21836
4  CoreLocation                   0x189618aac (null) + 5952
5  CoreLocation                   0x189612e48 (null) + 880
6  CoreFoundation                 0x18288900c __CFRUNLOOP_IS_CALLING_OUT_TO_A_BLOCK__ + 20
7  CoreFoundation                 0x182888944 __CFRunLoopDoBlocks + 308
8  CoreFoundation                 0x182886d8c __CFRunLoopRun + 1960
9  CoreFoundation                 0x1827b0d10 CFRunLoopRunSpecific + 384
10 GraphicsServices               0x184098088 GSEventRunModal + 180
11 UIKit                          0x187a85f70 UIApplicationMain + 204
12                         0x10015c5a0 main (AppDelegate.swift:25)
13 libdispatch.dylib              0x18234e8b8 (Missing)

我在 AppDelegate 中使用 CLLocationmanager 以便在应用唤醒位置更新时获取用户的位置。

  import UIKit
import FBSDKCoreKit
import FBSDKLoginKit
import Quickblox
import Fabric
import Crashlytics
import CoreLocation

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, CLLocationManagerDelegate {

    var window: UIWindow?
    var locationManager: CLLocationManager!

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {


            if var options = launchOptions {

                if var _: NSNumber = options[UIApplicationLaunchOptionsLocationKey] as? NSNumber {

                    locationManager.startUpdatingLocation()

                }
            }

        initLocationManager()


        return true
    }

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

            if  let lat = manager.location?.coordinate.latitude,
                let long = manager.location?.coordinate.longitude {

                 //sends to server to update
                updateloc(String(lat), long: String(long))
            }

        }


    func initLocationManager() {
            if (nil == locationManager) {
                locationManager = CLLocationManager()
            }
            locationManager.delegate = self
            locationManager.desiredAccuracy = kCLLocationAccuracyBest
            if #available(iOS 9.0, *) {
                locationManager.allowsBackgroundLocationUpdates = true
            }

            let status = CLLocationManager.authorizationStatus()
            if (status == CLAuthorizationStatus.AuthorizedAlways ||  status == CLAuthorizationStatus.AuthorizedWhenInUse) {
                locationManager.startUpdatingLocation()
            }

        }

可以帮我出一个吗?

谢谢。

最佳答案

您需要先调用 initLocationManager(),然后才能在 locationManager.startUpdatingLocation() 中使用它。 尝试

if var options = launchOptions {
    if options[UIApplicationLaunchOptionsLocationKey] != nil {
        initLocationManager()
        locationManager.startUpdatingLocation()
    }
}

关于ios - CoreLocation 为空,应用程序在启动时崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37107644/

相关文章:

ios - 如何获取 iOS 10 watch 导航栏的颜色和半透明度?

ios - 如何将 anchor 视角设置为 0.5 , 0.5 以获得屏幕中心的 View ? (不是场景)

ios - 从另一个类 iOS 获取动态 bool 变量

objective-c - CoreLocation , MapKit 错误

ios - 如何在 Swift 中不使用导航 Controller 的情况下将选项卡栏菜单传播到 subview

ios - UIImageView使用UICollectionView淡入淡出动画

ios - NSWindowController showWindow 是否触发嵌入式 NSView setNeedsDisplay?

ios - CocoaPods 找不到 pod "Firebase/Core” 的兼容版本 | cloud_firestore, flutter

ios - iPhone 上 Swift 中的图像缩放问题

ios - 从 UITableView 删除一行时 Swift 应用程序崩溃