暂停直到CLLocation不为零

时间:2015-12-14 17:48:50

标签: ios swift admob cllocationmanager

我已在应用中安装了AdMob,并且我已将其配置为正常运行。我遇到的一个问题是我的CLLocationManager在启动时有点滞后。

我已将CLLocationManager设置为单个类,并在AppDelegate.swift中使用以下行调用didFinishLoadingWithOptions

LocationManager.sharedInstance.startUpdatingLocation()

在我的应用初始viewController中,我创建了一种方法来加载viewDidAppear中调用的广告。我把它放在那里,如果用户导航离开该标签,则当他们返回此标签时会加载新广告。

两个问题:

1)在更新CLLocationManager时是否有更好的方法来处理延迟?

2)将currentLocation存储在NSUserDefaults并在启用位置服务时拉出该位置是否可以,但由于启动滞后,currentLocation为零?

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 个答案:

答案 0 :(得分:0)

我找到了一个解决方案,它将解决除初始启动之外的所有情况......

第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