暂停直到 CLLocation 不为零
Pausing until CLLocation is not nil
我已经在一个应用程序中安装了 AdMob,并且已将其配置为正常运行。我遇到的一个问题是我的 CLLocationManager
在启动时有点迟钝。
我将 CLLocationManager
设置为单例 class,在 didFinishLoadingWithOptions
中的 AppDelegate.swift
上使用以下行调用:
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 步:在设置 currentLocation
时在我的 LocationManager
单例 class 中创建一个 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")
}
我已经在一个应用程序中安装了 AdMob,并且已将其配置为正常运行。我遇到的一个问题是我的 CLLocationManager
在启动时有点迟钝。
我将 CLLocationManager
设置为单例 class,在 didFinishLoadingWithOptions
中的 AppDelegate.swift
上使用以下行调用:
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 步:在设置 currentLocation
时在我的 LocationManager
单例 class 中创建一个 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")
}