我已經在應用程序中安裝了AdMob,並且已將其配置爲正常工作。我遇到的一個問題是我的CLLocationManager
在啓動時有點遲緩。暫停,直到CLLocation不爲零
我有我的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)
}
而不是將最近的位置存儲在'NSUserDefaults'中,看看'CLLocationManager()。location'屬性是否能滿足您的需求。文檔[這裏](https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/index.html#//apple_ref/occ/instp/CLLocationManager/location) – caseynolan
謝謝。問題是它沒有足夠快地更新。我希望能夠在發佈後加載地理定製的廣告。由於啓動和初始的'viewController'加載之間的時間間隔短,''viewController'加載時CLLocationManager()。location'爲零。另一秒左右,有一個可用的位置,但'NSUserDefaults'路線爲我除了第一次啓動以外的每一種情況都獲得了價值。 – Adrian
Ohhhhh ...好的。是的。你在談論什麼樣的準確性?你可能可以從其他東西中汲取可比的東西。就像我的頭頂上的一個例子,像時區。 – caseynolan