2015-12-14 17 views
0

我已經在應用程序中安裝了AdMob,並且已將其配置爲正常工作。我遇到的一個問題是我的CLLocationManager在啓動時有點遲緩。暫停,直到CLLocation不爲零

我有我的CLLocationManager設置爲這就是所謂的AppDelegate.swiftdidFinishLoadingWithOptions與下面的行一個單例類:

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) 
} 
+0

而不是將最近的位置存儲在'NSUserDefaults'中,看看'CLLocationManager()。location'屬性是否能滿足您的需求。文檔[這裏](https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLLocationManager_Class/index.html#//apple_ref/occ/instp/CLLocationManager/location) – caseynolan

+0

謝謝。問題是它沒有足夠快地更新。我希望能夠在發佈後加載地理定製的廣告。由於啓動和初始的'viewController'加載之間的時間間隔短,''viewController'加載時CLLocationManager()。location'爲零。另一秒左右,有一個可用的位置,但'NSUserDefaults'路線爲我除了第一次啓動以外的每一種情況都獲得了價值。 – Adrian

+0

Ohhhhh ...好的。是的。你在談論什麼樣的準確性?你可能可以從其他東西中汲取可比的東西。就像我的頭頂上的一個例子,像時區。 – caseynolan

回答

0

我想出了一個解決方案,將解決所有情況,除了初始的啓動...

第1步:創建我LocationManager單例類的NSUserDefaultcurrentLocation被設置:

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