我已经在应用中安装了AdMob,并且已对其进行配置以使其正常运行。我遇到的一个问题是我的CLLocationManager在启动时有点慢。

我将我的CLLocationManager设置为一个单例类,在AppDelegate.swift中的didFinishLoadingWithOptions上使用以下代码行对其进行了调用:

LocationManager.sharedInstance.startUpdatingLocation()


在应用程序的初始viewController中,我创建了一种方法来加载viewDidAppear中调用的广告。我将其放在此处,因此,如果用户离开该标签,则当他们回到该标签时,将加载新广告。

两个问题:

1)是否有更好的方法来处理更新CLLocationManager中的滞后?

2)如果启用了位置服务,但可以将currentLocation存储在NSUserDefaults中并拉入该位置,但由于启动滞后currentLocation为nil,可以吗?

func loadAd() {

    // Google Banner
    print("Google Mobile Ads SDK version: " + GADRequest.sdkVersion())

    // TODO: need to update this for production
    bannerView.adUnitID = "ca-app-pub-3940256099942544/2934735716"
    bannerView.rootViewController = self

    if CLLocationManager.locationServicesEnabled() {
        // Added this if statement to get around laggyness issue.
        if LocationManager.sharedInstance.currentLocation != nil {
            currentLocation = LocationManager.sharedInstance.currentLocation
            adRequest.setLocationWithLatitude(CGFloat((currentLocation?.coordinate.latitude)!),
                longitude: CGFloat((currentLocation?.coordinate.longitude)!),
                accuracy: CGFloat((currentLocation?.horizontalAccuracy)!))
            print("loadAd()'s current location is \(LocationManager.sharedInstance.currentLocation)")
        }
    } else {
        print("Location services not enabled")
    }

    if NSUserDefaults.standardUserDefaults().valueForKey("userGender") != nil {
        if NSUserDefaults.standardUserDefaults().stringForKey("userGender") == "male" {
            adRequest.gender = .Male
        } else {
            adRequest.gender = .Female
        }
        print("gender is set to \(adRequest.gender)")
    }

    if NSUserDefaults.standardUserDefaults().valueForKey("userBirthday") != nil {
        let birthDate = NSUserDefaults.standardUserDefaults().valueForKey("userBirthday") as! NSDate
        let now = NSDate()
        let calendar = NSCalendar(identifier: NSCalendarIdentifierGregorian)
        let components = calendar?.components([.Month, .Day, .Year], fromDate: birthDate)
        let ageComponents = calendar?.components(NSCalendarUnit.Year, fromDate: birthDate, toDate: now, options: [])
        let age: NSInteger = (ageComponents?.year)!

        if age < 13 {
            adRequest.tagForChildDirectedTreatment(true)
        } else {
            adRequest.tagForChildDirectedTreatment(false)
        }

        print("The user's age is \(age)")

        adRequest.birthday = NSCalendar.currentCalendar().dateFromComponents(components!)
    }

    bannerView.loadRequest(adRequest)
}

最佳答案

我想出了一个解决方案,可以解决除首次启动以外的所有情况。

步骤1:设置NSUserDefault时,在我的LocationManager单例类中创建一个currentLocation

var currentLocation: CLLocation? {
    didSet {
        // Store coordinates as a dictionary in NSUserDefaults
        let savedLatitude: CGFloat = CGFloat((self.currentLocation?.coordinate.latitude)!)
        let savedLongitude: CGFloat = CGFloat((self.currentLocation?.coordinate.longitude)!)
        let userLocation: [String: NSNumber] = ["latitude": savedLatitude, "longitude": savedLongitude]
        NSUserDefaults.standardUserDefaults().setObject(userLocation, forKey: "savedLocation")
    }
}


步骤2:调整loadAd()方法,如下所示:

    // If location services are enabled...
    if CLLocationManager.locationServicesEnabled() {
        // check if currentLocation has a value
        if LocationManager.sharedInstance.currentLocation != nil {
            currentLocation = LocationManager.sharedInstance.currentLocation
            adRequest.setLocationWithLatitude(CGFloat((currentLocation?.coordinate.latitude)!),
                longitude: CGFloat((currentLocation?.coordinate.longitude)!),
                accuracy: CGFloat((currentLocation?.horizontalAccuracy)!))
            print("loadAd()'s current location is \(LocationManager.sharedInstance.currentLocation)")
        } else {
            // if there's no value stored, tough luck
            if NSUserDefaults.standardUserDefaults().objectForKey("savedLocation") == nil {
                print("No location stored")
            } else {
                // if there IS a stored value, use it...
                print("Using stored location from NSUserDefaults")
                let userLocation = NSUserDefaults.standardUserDefaults().objectForKey("savedLocation")
                adRequest.setLocationWithLatitude(CGFloat(userLocation!.objectForKey("latitude")! as! NSNumber),
                    longitude: CGFloat(userLocation!.objectForKey("longitude")! as! NSNumber),
                    accuracy: CGFloat(3000))
                print("User location is \(userLocation)")
            }
        }
    } else {
        print("Location services not enabled")
    }


Me upon figuring it out

关于ios - 暂停直到CLLocation不为零,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34273439/

10-12 01:50