Ios 暂停,直到CLLocation不是nil

Ios 暂停,直到CLLocation不是nil,ios,swift,admob,cllocationmanager,Ios,Swift,Admob,Cllocationmanager,我已在应用程序中安装AdMob,并已将其配置为正常工作。我遇到的一个问题是我的CLLocationManager在启动时有点滞后 我已将我的CLLocationManager设置为一个单例类,在AppDelegate.swift中使用选项完成加载,如下所示: LocationManager.sharedInstance.startUpdatingLocation() 在我的应用程序的初始viewController中,我创建了一个加载广告的方法,该方法在viewDidAppear中调用。我把它

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

我已将我的
CLLocationManager
设置为一个单例类,在
AppDelegate.swift
中使用选项完成加载,如下所示:

LocationManager.sharedInstance.startUpdatingLocation()
在我的应用程序的初始viewController中,我创建了一个加载广告的方法,该方法在
viewDidAppear
中调用。我把它放在那里,所以如果用户离开该选项卡,当他们返回该选项卡时,将加载一个新的广告

两个问题:

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

2) 如果启用了位置服务,但由于启动延迟,currentLocation为零,则可以将
currentLocation
存储在
NSUserDefaults
中并将该位置拉入吗

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)
}
func loadAd(){
//谷歌横幅
打印(“谷歌移动广告SDK版本:+GADRequest.sdkVersion())
//TODO:需要为生产更新此选项
bannerView.adUnitID=“ca-app-pub-3940256099942544/2934735716”
bannerView.rootViewController=self
如果CLLocationManager.locationServicesEnabled(){
//添加此if语句以避免滞后问题。
如果LocationManager.sharedInstance.currentLocation!=nil{
currentLocation=LocationManager.sharedInstance.currentLocation
adRequest.SETLOCATIONWITHLATIONE(CGFloat((当前位置?坐标.纬度)!),
经度:CGFloat((当前位置?坐标.经度)!),
精度:CGFloat((当前位置?水平精度)!)
打印(“loadAd()的当前位置为\(LocationManager.sharedInstance.currentLocation)”)
}
}否则{
打印(“未启用位置服务”)
}
如果NSUserDefaults.standardUserDefaults().valueForKey(“用户性别”)!=nil{
如果NSUserDefaults.standardUserDefaults().stringForKey(“用户性别”)=“男性”{
adRequest.性别=.男性
}否则{
adRequest.性别=.女性
}
打印(“性别设置为\(adRequest.gender)”)
}
如果NSUserDefaults.standardUserDefaults().valueForKey(“用户生日”)!=nil{
将birthDate=NSUserDefaults.standardUserDefaults().valueForKey(“用户生日”)设为!NSDate
现在让我们=NSDate()
let calendar=NSCalendar(标识符:NSCalendarIdentifierGregorian)
让组件=日历?组件([.Month、.Day、.Year],fromDate:birthDate)
让ageComponents=calendar?.components(NSCalendarUnit.Year,fromDate:生日,toDate:现在,选项:[]))
让年龄:NSInteger=(年龄成分?.year)!
如果年龄<13岁{
adRequest.TAGFORCHILDEDIRECTED治疗(真)
}否则{
adRequest.TAG儿童指导治疗(假)
}
打印(“用户的年龄是\(年龄)”)
adRequest.birth=NSCalendar.currentCalendar().dateFromComponents(components!)
}
bannerView.loadRequest(adRequest)
}

我想出了一个解决方案,可以解决除首次发布之外的所有场景

步骤1:设置
currentLocation
时,在my
LocationManager
singleton类中创建
NSUserDefault

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")
    }

不要将最近的位置存储在
NSUserDefaults
中,而是查看
CLLocationManager().location
属性是否满足您的需要。谢谢。问题是它的更新速度不够快。我希望能够在发布时加载按地理位置定制的广告
CLLocationManager()。由于启动和初始
viewController
加载之间的时间间隔较短,因此加载此
viewController
时,location
为零。再过一秒钟左右,就有了一个可用的位置,但是除了第一次发射外,NSUserDefaults路径为每种情况都提供了一个值。Ohhhhh。。。好啊是的。你在说什么样的准确性?你也许可以从其他东西中提取出类似的东西。就像我脑海中的一个例子,比如时区。